in reply to Subroutine for showing the end user the progress of the program

If you're up for something truly perlish, handy and clever, use Smart::Comments:
use Smart::Comments; for (my $j=500; $j>0; $j--) { ### Compiling===[%] done select undef, undef, undef, 0.01; }
The trick lies in the ### comment, turning into a progress bar at runtime.
  • Comment on Re: Subroutine for showing the end user the progress of the program
  • Download Code

Replies are listed 'Best First'.
Re^2: Subroutine for showing the end user the progress of the program
by ghenry (Vicar) on Feb 02, 2005 at 15:18 UTC

    Again, how do I integrate this?

    Just getting into the best language ever... Fancy a yourname@perl.me.uk? Just ask!!!
      The basic usage is to call a subroutine, once every loop, through your long process. Like:
      #!/usr/bin/perl use strict; use warnings; my $spinner_pos = 0; sub spin { my $spinner = '|\-/'; print STDERR substr($spinner, $spinner_pos++%length($spinner), 1)."\r" +; } while (1) { qx(cat /etc/termcap); #simulate doing important stuff spin(); }
      So you have to design your application, and progress indicator together. There is no 1 "foolproof way" to use them. So what type of proram are you running? If you show us, we can help you with placing an indicator somewhere in the code. It's "fun" and educational to experiment yourself. Take the snippets you've received here, and play with them. You can't break anything. You will get the idea of how they work eventually.

      In some "event driven programs" like Tk, you can make your indicator an object, and just do things like

      $progressindicator->start; $progressindicator->stop;

      here is another widely used style of indicator used by wget

      #!/usr/bin/perl $|=1; do{ print progress_bar( $_, 100, 25, '=' ); sleep 1 } for 1..100; sub progress_bar { my ( $got, $total, $width, $char ) = @_; $width ||= 25; $char ||= '='; $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/$ +total; }

      I'm not really a human, but I play one on earth. flash japh

        Great, thanks. I actually found a good writeup of how to use the progressbar module here at perlmonks.org. This place is very handy!!!

        Walking the road to enlightenment... I found a penguin and a camel on the way.....
        Fancy a yourname@perl.me.uk? Just ask!!!