代码之家  ›  专栏  ›  技术社区  ›  Eugene Yarmash

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

  •  2
  • Eugene Yarmash  · 技术社区  · 15 年前

    示例代码:

    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 回复  |  直到 14 年前
        1
  •  6
  •   friedo    15 年前

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

    open my $fh, '<', $response->content_ref;
    
    while( <$fh> ) { 
        # pretend it's a file
    }
    
        2
  •  0
  •   Arkadiy    7 年前

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