[prog] [Perl] chop URL to get filename

Jacinta Richardson jarich at perltraining.com.au
Mon Apr 30 13:18:37 UTC 2007


Chris Henderson wrote:

> system ("$wget $fetch_file");
> 
> # can't do this
> system ("$cp $fetch_file");

Probably best to use File::Copy to copy the file:

	use File::Copy qw(copy);

	copy($fetch_file, $destination) or die "Failed to copy file: $!";

File::Copy comes installed with Perl by default.

It's a good idea to ensure that your call to system succeeded.  So instead of just:

 > system ("$wget $fetch_file");

you might do:

	system ("$wget $fetch_file");
	# Check whether anything went wrong
	if( $? ) {
		die "Failed to wget file.";
	}

perldoc -f system  will tell you more about what $? contains in case of an error 
with system().  For example a value of -1 suggests the command didn't run, for 
example you have the wrong path for wget.  To get the actual return code (system 
grep for example returns 0 if it finds a matching line, 1 if there was no match 
and 2 if there was an error (file not found etc)).

All the best,

	Jacinta



More information about the Programming mailing list