Reputation: 7145
I am using the following script to download a file over http and a utility method to get the current date.
use File::Fetch;
use DateTime qw();
sub getDateUtility{
return DateTime->now->strftime('%m/%d/%Y')
}
sub fetchFile{
my $fileHandler = File::Fetch->new(uri => 'http://sample.com/file2fetch.txt');
my $placeFile = $fileHandler->fetch() or die $fileHandler->error;
}
Now I want to save the file with the current date as name of the file. How can I replace the file name with the current date in above code?
Upvotes: 1
Views: 1315
Reputation: 3608
There is no easy way to do that. The best you can do is to make a temporary directory, fetch the file there using the default name, then rename it to the name you need. Use File::Temp::tempdir
:
use File::Fetch;
use DateTime qw();
use File::Temp qw( tempdir );
sub getDateUtility {
return DateTime->now->strftime('%m_%d_%Y')
}
sub fetchFile {
my ($url, $destination) = @_;
my $tempdir = tempdir( CLEANUP => 1 );
my $fetcher = File::Fetch->new(
uri => $url,
to => $tempdir,
);
my $fetched = $fetcher->fetch() or die $fetcher->error;
rename($fetched, $destination);
}
fetchFile('http://ec2-54-169-147-210.ap-southeast-1.compute.amazonaws.com/export.php?test=141121_5N_39140a1bc54a1cf78c9b131ed52f8654', getDateUtility() . '.txt');
Please note that I changed your getDateUtility
function to use underscores _
as a separators, not slashes /
, as you cannot use slashes in a file name.
See also: this question.
Upvotes: 1