如何在不使用:content_file选项的情况下将响应作为文件处理?

时间:2010-03-02 19:22:27

标签: perl file lwp lwp-useragent

示例代码:

my $ua = LWP::UserAgent->new;  
my $response = $ua->get('http://example.com/file.zip');
if ($response->is_success) {
    # get the filehandle for $response->content
    # and process the data
}
else { die $response->status_line }

我需要将内容作为文件打开,而不事先将其保存到磁盘。你会怎么做?

2 个答案:

答案 0 :(得分:6)

你可以打开一个指向标量的假文件句柄。如果file参数是标量引用,Perl会将标量的内容视为文件数据而不是文件名。

open my $fh, '<', $response->content_ref;

while( <$fh> ) { 
    # pretend it's a file
}

答案 1 :(得分:0)

不是一个文件,但这是一个相关的SO问题:What is the easiest way in pure Perl to stream from another HTTP resource?