【问题标题】:How do I process the response as a file without using the :content_file option?如何在不使用 :content_file 选项的情况下将响应作为文件处理?
【发布时间】:2010-03-02 19:22:27
【问题描述】:

示例代码:

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 }

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

【问题讨论】:

    标签: perl file lwp lwp-useragent


    【解决方案1】:

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

    open my $fh, '<', $response->content_ref;
    
    while( <$fh> ) { 
        # pretend it's a file
    }
    

    【讨论】:

    • 或者,更有效的是open my $fh, '&lt;', $response-&gt;content_ref;
    • 酷,我不知道content_ref。我会更新答案。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2018-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-31
      • 2017-07-18
      • 2020-03-10
      • 1970-01-01
      相关资源
      最近更新 更多