Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

wget style progress bar

by tachyon (Chancellor)
on Mar 16, 2003 at 09:34 UTC ( #243454=snippet: print w/replies, xml ) Need Help??
Description: Print a wget style progress bar. Inputs should be self explanatory
do{ print progress_bar( $_, 100, 25, '=' ); sleep 1 } for 1..100;

# get a progress bar string, inputs should be self explanatory otherwi
+se just RTFS!
sub progress_bar {
    my ( $got, $total, $width, $char ) = @_;
    $width ||= 25;
    $char  ||= '=';
    my $num_width = length $total;
    sprintf "|%-${width}s| Got %${num_width}s bytes of %s (%.2f%%)\r",
        $char x (($width-1)*$got/$total). '>', $got, $total, 100*$got/
|==========>              | Got  42 bytes of 100 (42.00%)
Replies are listed 'Best First'.
Re: wget style progress bar
by morbus (Sexton) on May 28, 2003 at 02:37 UTC
    Incidentally, for this to work under strict/warnings, change "(%.2f%)" to "(%.2f%%)" (escaping the literal % is necessary).
Re: wget style progress bar
by ikegami (Pope) on Oct 06, 2004 at 02:51 UTC

    Doesn't display anything for the longuest time because STDOUT is not being flushed. Setting STDOUT to outflush or adding a call to flush (defined below) after the print does the trick.

    sub flush { my $h = select($_[0]); my $a=$|; $|=1; $|=$a; select($h); }
      $|++   ;#)
Re: wget style progress bar
by artist (Parson) on Mar 16, 2003 at 17:01 UTC
    Very neat application tachyon.
    Now How I can integrate it to see the progress of my subroutine ?
    my routine = sub { open OUT, ">file"; foreach (1..200){ print OUT "HelloWorld\n" }; sleep 1; }; print_progress_bar($routine);


      I'm not sure what you're asking for. This, perhaps?
      sub foo { open my $fh, ">", "file"; foreach (1..200){ print OUT "HelloWorld\n"; print progress_bar($_, 200, 25, '='); } sleep 1; }

      Makeshifts last the longest.

        Almost similar, but I am looking for functionality where I can call and mesure the progress for the routine as it is executed. So the line 'print progress_bar' should be outside the 'foo' here. I should have independent way of measuring the progress of the subroutine. I guess the only way to achive that is to implement the call back routines or implement threads .


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://243454]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2020-10-20 14:38 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (210 votes). Check out past polls.