[英]How can I set the output file for Perl's File::Fetch?
I am using File::Fetch in a Perl script. 我在Perl脚本中使用File :: Fetch 。 How can I get File::Fetch to store a file to be downloaded in a specified file? 如何获取File :: Fetch以将要下载的文件存储在指定文件中?
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 after download, but is it possible to specify the file name immediately? 解决方法是下载后重命名文件,但是可以立即指定文件名吗?
Test platform: 测试平台:
ActiveState Perl 64 bit. ActiveState Perl 64位。 From perl -v
: 从perl -v
:
v5.10.0 built for MSWin32-x64-multi-thread Binary build 1004 [287188] provided by ActiveState.
Looking at the source code, the only way seems to be making a subclass and overriding the output_file
method. 查看源代码,唯一的方法似乎是制作一个子类并覆盖output_file
方法。 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. 我想作者会接受一个使output_file
成为读/写属性的补丁,以便可以在运行时对其进行更改。
I looked at the File::Fetch 0.22 code to see what you would need to do for this. 我查看了File :: Fetch 0.22代码,以了解需要执行的操作。 There's a bug that prevents you from subclassing (unless you want to override new()
and create()
to fix it). 有一个错误阻止您进行子类化(除非您想覆盖new()
和create()
进行修复)。 Update The File::Fetch
developers have applied my patch and a new release should be out shortly. 更新 File::Fetch
开发人员已应用了我的补丁程序,并且很快就会发布新版本。
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. 最简单的方法可能是将文件提取到一个临时目录,使用output_file
查找名称,然后将其重命名为最终位置。
Lukáš thinks I'm wrong, but I don't think he's actually tried it like I had. 卢卡什(Lukáš)认为我错了,但我不认为他实际上像我一样尝试过。 Creating a subclass and overriding output_file
has no effect: 创建一个子类并覆盖output_file
无效:
#!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. 运行此命令时,最终在当前目录中找到Fetch.pm 。 The script does not die because it never invoked my output_file
because the object is not actually in my subclass. 该脚本不会死,因为它从未调用过我的output_file
因为该对象实际上不在我的子类中。 This is a bug in File::Fetch
which I've filed as RT 53427 . 这是File::Fetch
的错误,我已将其归档为RT 53427 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.