You could take a look at Smart::Comments - this might do what you want (it doesn't require a MAX counter). Here's a quick commandline test you can run:

$ perl -Mstrict -Mwarnings -e ' use Smart::Comments; for (1 .. 500) { ### Running... done qx{ls -l > /dev/null 2>&1}; } '

Here's my take on a Roll-Your-Own spinner. This has 6 tests: the first 3 are expected to fail; the next three have fast, medium and slow spin rates. The spinner becomes an asterisk if the process completes successfully.

#!/usr/bin/env perl use strict; use warnings; my $CMD = q{ls -l > /dev/null}; for ([], [q{}], [q{dummy}], [$CMD, 10], [$CMD], [$CMD, 50]) { eval { run_with_progress(@$_); }; warn $@ if $@; } sub run_with_progress { print q{-} x 60, qq{\nStarting ...\n}; my ($cmd, $skip) = @_; die q{Nothing to run!} unless $cmd; $skip ||= 25; # Arbitrary my @sails = map { "\b$_" } qw{- \ | /}; my $sail = 0; { local $| = 1; print q{Running: }; for (0 .. 1000) { system $cmd and die qq{'$cmd' failed! $? : $!}; print $sails[$sail++ % @sails] if not $_ % $skip; } } print qq{\b*\nFinished.\n}; }

Output:

$ pm_text_spinner_ryo.pl ------------------------------------------------------------ Starting ... Nothing to run! at ./pm_text_spinner_ryo.pl line 20. ------------------------------------------------------------ Starting ... Nothing to run! at ./pm_text_spinner_ryo.pl line 20. ------------------------------------------------------------ Starting ... Running: Can't exec "dummy": No such file or directory at ./pm_text_s +pinner_ryo.pl line 32. 'dummy' failed! -1 : No such file or directory at ./pm_text_spinner_ry +o.pl line 32. ------------------------------------------------------------ Starting ... Running: * Finished. ------------------------------------------------------------ Starting ... Running: * Finished. ------------------------------------------------------------ Starting ... Running: * Finished.

Update: I revisited this and reworked run_with_progress() to use Smart::Comments but with a spinner instead of the normal progress bar. The messages are the same as shown above and the spin rates vary in the same manner; the formatting is different.

sub run_with_progress { use Smart::Comments; ### Starting ... my ($cmd, $skip) = @_; die q{Nothing to run!} unless $cmd; $skip ||= 25; # Arbitrary my @sails = map { ($_) x $skip } qw{- \ | /}; my $sail = 0; for (0 .. 1000) { ### Running: $sails[$sail++ % @sails] system $cmd and die qq{'$cmd' failed! $? : $!}; } ### Finished. no Smart::Comments; return; }

This code is a lot cleaner; however, I'm not a big fan of the output.

$ pm_text_spinner_sc_spin.pl ### Starting ... Nothing to run! at ./pm_text_spinner_sc_spin.pl line 22. ### Starting ... Nothing to run! at ./pm_text_spinner_sc_spin.pl line 22. ### Starting ... Running: - Can't exec "dummy": No such file or + directory at ./pm_text_spinner_sc_spin.pl line 29. 'dummy' failed! -1 : No such file or directory at ./pm_text_spinner_sc +_spin.pl line 29. ### Starting ... ### Finished. ### Starting ... ### Finished. ### Starting ... ### Finished.

Update 2: I also reworked run_with_progress() to use Term::Spinner. Messages, spin rates, etc. work the same as the other two; the output is almost identical to the Roll-Your-Own version. This would be my favourite ... at least until I find something better. :-)

sub run_with_progress { print q{-} x 60, qq{\nStarting ...\n}; my ($cmd, $skip) = @_; die q{Nothing to run!} unless $cmd; $skip ||= 25; # Arbitrary use Term::Spinner; my $spinner = Term::Spinner::->new(); print q{Running: }; for (0 .. 1000) { system $cmd and die qq{'$cmd' failed! $? : $!}; $spinner->advance() if not $_ % $skip; } $spinner->finish(); print qq{\nFinished.\n}; undef $spinner; return; }

-- Ken


In reply to Re: Progress Bar in Perl script by kcott
in thread Progress Bar in Perl script by slayedbylucifer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.