Somebody asked on the Chatterbox on how one can use LWP::Simple to retrieve an URL, and get an indication on how progress is going. At first I thought of hacking a copy of LWP::Simple's getstore(), but that even isn't necessary: just pass a sub reference for the $file parameter. Here's just an example.
#!/usr/bin/perl -w use LWP::Simple; use Fatal 'open'; open HTML, ">monastery_gates.html"; my $total = 0; print STDERR "\nconnecting..."; getstore "http://www.perlmonks.org", sub { my($chunk) = @_; print STDERR "\r", " " x 20 unless $total; $total += length $chunk; print STDERR "\r$total bytes"; print HTML $chunk; }; print STDERR "\n";

Replies are listed 'Best First'.
Re: Using LWP::Simple with a callback (progress indicator)
by barrd (Canon) on Sep 16, 2003 at 11:32 UTC
    Update: After bart's reply I have edited my original post

    Hi bart,
    Cool, but a couple of points:

    1. No use strict;... tsk tsk ;)
    2. opening a filehandle without checking i.e.
      open HTML, ">monastery_gates.html" or die "Can't open HTML: $!";

      -- bart added use Fatal 'open';
    3. While we're on the subject you haven't closed it either
      close HTML or die "Can't close HTML: $!";
    I think these are important enough to warrant pointing them out.

    No offence meant.

    ~barrd

      Heh, odd... I do have the error checking in the version here at home. I'll edit that. (Done — in a slightly unusual manner :))

      As for the rest... *waves hand* this is just a snippet. You have to know when this stuff matters.

        Okey cokey bart,
        Sorry, didn't realise it was intended as a snippet. Please accept my apologies but I was just trying to point out some common errors for any 'newbies' who may have read it. I am not of course implying that you are a 'newbie' :).

        Peace, ~barrd

        Update:

        (Done — in a slightly unusual manner :))
        Indeed, I've never seen it done "that" way before ;)