Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
Retiring, March 2020 - sorry, you have missed our final public course.
The Coronavirus situation has lead us to suspend public training - which was on the cards anyway, with no plans to resume

Please ask about private 'maintenance' training for Python, Tcl, Perl, PHP, Lua, etc
Happily continuing private consultancy / programming work
Progress Bar Techniques - Perl

Have you ever sat there and wondered "is this program nearly done ... is it still running ... how is it getting on" and wished you had a progress bar. But then have you ever watched a jerky progress bar and felt that it's more fiction than fact?

We were discussing these aspects on today's private Perl Programming Course (though on our public schedule, this topic is left until Perl for Larger Projects). And we came to the conclusion that progress bars, when carefully implemented, are an excellent idea. So how do you carefully implement them in Perl?

Here is a sample program that I wrote:

select STDERR;
$| = 1;
for (1..14) {
   sleep 1;
   $sofar = $_/14;
   printf "At %4.2f of 1.0\r",$sofar;
print "\n";
$| = 0;
select STDOUT;

And here is a LONG set of notes on a very short program!

Firstly ... the "sleep" represents the work being done and the calculation of "$sofar" represents the calculation of progress so far; it's designed to be awkward and come up with a number that's sometimes a long decimal and at other times is short.

Notes ...

1. The output of the progress bar is to STDERR and not to STDOUT; this is done so that any redirection to file doesn't cause the progress bar data to be dumperd into the file as well, but rather to remain on the screen

2. I've used $| to set autoflush mode so that the progress data is actually output and isn't simply buffered. Normally, Perl buffers up to a \n character but we haven't used any of those right up to the end of the script, so without the setting of $| we would see no report until a sudden flush.

3. \r has been used to return the cursor to the start of the current line rather that to the next line - that way, one progress report overwrites the previous one repeatedly, rather than having them scroll down the screen

4. We've used printf for our output, and used fixed width fields which are plenty long enough for values that are going to be displayed. This means that the line length will NOT change as the program runs, so won't leave "droppings" at the right hand end when a shorter line is displayed over a longer one.

5. There's an indication of just what number we're going up to with our progress bar; we indicate (for example) 0.42 of 1.0 to clearly show that we're 42% of the way through and not just 4.2% or even 0.42%.

And - but it's not explicitly illustrated in this program - I advocate that you should NOT complete one phase of a program that shows a progress bar for that phase, then go onto another section that has its own progress bar ... progress should be reported as to how you're doing through the complete job. I don't know about you, but I have had some VERY frustrating experiences where a long-waited progress bar has finished and joy has turned to dismay as another bar has appeared for some further phase that I didn't even know existed ...

Source code of the example - here
(written 2008-12-03, updated 2008-12-10)

Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P667 - Perl - Handling Huge Data
  [3375] How to interact with a Perl program while it is processing data - (2011-07-31)
  [3374] Speeding up your Perl code - (2011-07-30)
  [2834] Teaching examples in Perl - third and final part - (2010-06-27)
  [2806] Macho matching - do not do it! - (2010-06-13)
  [2805] How are you getting on? - (2010-06-13)
  [2376] Long job - progress bar techniques (Perl) - (2009-08-26)
  [1924] Preventing ^C stopping / killing a program - Perl - (2008-12-05)
  [1397] Perl - progress bar, supressing ^C and coping with huge data flows - (2007-10-20)
  [975] Answering ALL the delegate's Perl questions - (2006-12-09)
  [762] Huge data files - what happened earlier? - (2006-06-15)
  [639] Progress bars and other dynamic reports - (2006-03-09)

Back to
Previous and next
Horse's mouth home
Forward to
Romeo and Julie
Some other Articles
Making it all worthwhile
Flurinci knows Raby Lae PHP and Jeve
Romeo and Julie
Progress Bar Techniques - Perl
Perl Socket Programming Examples
Out of memory during array extend - Perl
Why I remember East Grafton
Quietly putting prices up
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page

This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2020: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/1920_Pro ... -Perl.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat