NAME¶
Term::ProgressBar::Simple - simpler progress bars
SYNOPSIS¶
# create some things to loop over
my @things = (...);
my $number_of_things = scalar @things;
# create the progress bar object
my $progress = Term::ProgressBar::Simple->new( $number_of_things );
# loop
foreach my $thing (@things) {
# do some work
$thing->do_something();
# increment the progress bar object to tell it a step has been taken.
$progress++;
}
# See also use of '$progress += $number' later in pod
DESCRIPTION¶
Progress bars are handy - they tell you how much work has been done, how much is
left to do and estimate how long it will take.
But they can be fiddly!
This module does the right thing in almost all cases in a really convenient way.
FEATURES¶
Lots - does all the best practice:
Wraps Term::ProgressBar::Quiet so there is no output unless the code is running
interactively - lets you put them in cron scripts.
Deals with minor updates - only refreshes the screen when it will change what
the user sees so it is efficient.
Completes the progress bar when the progress object is destroyed (explicitly or
by going out of scope) - no more '99%' done.
METHODS¶
new¶
# Either...
my $progress = Term::ProgressBar::Simple->new($count);
# ... or
my $progress = Term::ProgressBar::Simple->new(
{
count => $count, #
name => 'descriptive text',
}
);
Create a new progress bar. Either just pass in the number of things to do, or a
config hash. See Term::ProgressBar for details.
increment ( ++ )¶
$progress++;
Incrementing the object causes the progress display to be updated. It is smart
about checking to see if the display needs to be updated.
increment ( += )¶
$progress += $number_done;
Sometimes you'll have done more than one step between updates. A good example is
processing logfiles, where the time taken is relative to the size of the file.
In this case code like this would give a better feel for the progress made:
# Get the total size of all the files
my $total_size = sum map { -s } @filenames;
# Set up object with total size as steps to do
my $progress = Term::ProgressBar::Simple->new($total_size);
# process each file and increment by the size of each file
foreach my $filename (@filenames) {
process_the_file($filename);
$progress += -s $filename;
}
message¶
$progress->message('Copying $filename');
Output a message. This is very much like print, but we try not to disturb the
terminal.
SEE ALSO¶
Term::ProgressBar & Term::ProgressBar::Quiet
GOTCHAS¶
Not all operators are overloaded, so things might blow up in interesting ways.
Patches welcome.
THANKS¶
Martyn J. Pearce for the orginal and great Term::ProgressBar.
Leon Brocard for doing the hard work in Term::ProgressBar::Quiet, and for
submitting a patch with the code for "+="..
YAPC::EU::2008 for providing the venue and coffee whilst the first version of
this module was written.
AUTHOR¶
Edmund von der Burg "<evdb@ecclestoad.co.uk>".
<
http://www.ecclestoad.co.uk/>
BUGS¶
There are no tests - there should be. The smart way would be to trap the output
and check it is right.
LICENCE AND COPYRIGHT¶
Copyright (c) 2008, Edmund von der Burg
"<evdb@ecclestoad.co.uk>". All rights reserved.
This module is free software; you can redistribute it and/or modify it under the
same terms as Perl itself.