nevafuse has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to find some quick, simple code that will buffer an http response.

I'm trying to download a large file from a website. And I want to update my user with the progress. I realize I could keep checking the size of the file on my disk, but I'd rather just keep count of the bytes read. Something similar to this but with an http response...
while (my $bytesread = read($fh, $buffer, 1024)) { $totalbytes += $bytesread; print $buffer; }
Thanks.

Replies are listed 'Best First'.
Re: HTTP response buffer
by Corion (Patriarch) on Feb 23, 2009 at 20:31 UTC

    Why not just use LWP::UserAgent, and the ->get method with a :content_cb callback? That way you can keep the user updated while you still get a full-blown HTTP client.

      That sorta works. The file is a wav audio file, and when I print out the $data from the callback to a new file, it isn't opening. Do you think I need to convert the $data into a different format?
      $browser = LWP::UserAgent->new(); my $resp = $browser->get($url,':content_cb' => sub { my($data, $resp) = @_; open FILE, ">>1.wav" or die $!; print FILE $data; close FILE; return; },':read_size_hint' => 1024);

        You should binmode the file after opening it. I wouldn't reopen the file on every run through:

        open my $fh, '>', $filename or die "Couldn't create '$filename': $!"; binmode $fh; $browser = LWP::UserAgent->new(); my $resp = $browser->get($url,':content_cb' => sub { my($data, $resp) = @_; print $fh $data; return; },':read_size_hint' => 1024); close $fh;