views:

58

answers:

2

Example code:

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 }

I need to open the content as a file without prior saving it to the disk. How would you do this?

+5  A: 

You can open a fake filehandle that points to a scalar. If the file argument is a scalar reference, Perl will treat the contents of the scalar as file data rather than a filename.

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

while( <$fh> ) { 
    # pretend it's a file
}
friedo
Or, more efficiently, `open my $fh, '<', $response->content_ref;`
cjm
Cool, I didn't know about `content_ref`. I'll update the answer.
friedo
A: 

Not quite a file, but here is a relevant SO question: http://stackoverflow.com/questions/1567895/what-is-the-easiest-way-in-pure-perl-to-stream-from-another-http-resource

Arkadiy