2

示例代码:

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 }

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

4

2 回答 2

6

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

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

while( <$fh> ) { 
    # pretend it's a file
}
于 2010-03-02T19:34:40.707 回答
0

不完全是一个文件,但这是一个相关的 SO 问题:纯 Perl 从另一个 HTTP 资源流式传输的最简单方法是什么?

于 2010-03-02T19:38:12.360 回答