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

Hey guys, after a lot o work thinking in threads, forks etc, to do a nice progress bar to a download script, i made using Term::ProgressBar with content_cb of lwp module. But now i dont know how save these file to my HD, becouse i cant store using content_file at same time use content_cb...

A piece of code:

$progress = Term::ProgressBar->new({count => $total_size, ETA => 'l +inear'}); $progress->max_update_rate(1); my $response = $ua->get($url, ':content_cb' => \&callback, ); $progress->update($total_size); sub callback { my ($data, $response, $protocol) = @_; $final_data .= $data; $next_update = $progress->update(length($final_data))if length($fin +al_data) >= $next_update; }
Well i know lwp have a progress bar, but i want use term:progress bar.... any help? Thanks Guys.

Replies are listed 'Best First'.
Re: LWP - content_cb
by Eliya (Vicar) on Feb 15, 2012 at 01:13 UTC
    But now i dont know how save these file to my HD

    My first attempt would be to write $data (in the callback routine) to a file.  Have you tried that?

      I already tried using

      open $data, ">downloaded.pdf"; close $data;

      A downloaded.pdf its created but appear be empty.

        This doesn't work because (if anything) $data becomes a file handle, but nothing is printed to it.

        The idea would be to open a file handle outside of the callback routine, e.g.

        open my $fh, ">", "downloaded.pdf" or die $!; binmode $fh; # for when you're on a system (like Windows) where line +feeds are translated

        and then within the callback routine, successively print the data to it

        sub callback { my ($data, $response, $protocol) = @_; print $fh $data; ... }
Re: LWP - content_cb
by Anonymous Monk on Feb 15, 2012 at 19:16 UTC

    Well i know lwp have a progress bar, but i want use term:progress bar.... any help? Thanks Guys.

    LWP::UserAgent::ProgressBar already does it, though not by overriding sub LWP::UserAgent::progress