How can I set the output file for Perl's File::Fetch?
I am using File::Fetch in a Perl script. How can I get File::Fetch to store a file to be downloaded in a specified file?
I have tried:
use File::Fetch;
my $ff = File::Fetch->new(uri => 'http://stackoverflow.com/users/63550');
#my $where = $ff->fetch() or die $ff->error; # Creates a file named "63550".
my $where = $ff->fetch(to => 'SO.html'); # Creates a subfolder named SO.html with a file named "63550" in it.
A workaround would be to rename the file aft开发者_运维问答er download, but is it possible to specify the file name immediately?
Test platform:
ActiveState Perl 64 bit. From
perl -v
:v5.10.0 built for MSWin32-x64-multi-thread Binary build 1004 [287188] provided by ActiveState.
- Windows XP.
Looking at the source code, the only way seems to be making a subclass and overriding the output_file
method. I guess the author would accept a patch that makes output_file
a read/write property, so that it can be changed at run-time.
I looked at the File::Fetch 0.22 code to see what you would need to do for this. There's a bug that prevents you from subclassing (unless you want to override new()
and create()
to fix it). Update The File::Fetch
developers have applied my patch and a new release should be out shortly.
The easiest thing is probably to fetch the file to a temporary directory, discover the name with output_file
, and rename it to its final location.
Lukáš thinks I'm wrong, but I don't think he's actually tried it like I had. Creating a subclass and overriding output_file
has no effect:
#!perl
use strict;
use warnings;
BEGIN {
package File::Fetch::MyOutput;
use parent qw(File::Fetch);
sub output_file { die "I was called!" }
}
my $ff = File::Fetch::MyOutput->new(
uri => 'http://search.cpan.org/~bingos/File-Fetch-0.22/lib/File/Fetch.pm'
);
$ff->fetch( to => '.' );
When I run this, I end up with Fetch.pm in the current directory. The script does not die because it never invoked my output_file
because the object is not actually in my subclass. This is a bug in File::Fetch
which I've filed as RT 53427.
精彩评论