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

Eug*_*ash 2 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 }
Run Code Online (Sandbox Code Playgroud)

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

fri*_*edo 6

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

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

while( <$fh> ) { 
    # pretend it's a file
}
Run Code Online (Sandbox Code Playgroud)