Long job - progress bar techniques (Perl)
Archive - Originally posted on "The Horse's Mouth" - 2009-08-26 01:16:15 - Graham EllisHere's a "Perl for Larger Projects" example --- for use in illustrating the "advanced file and directory handling" and "handling huge data set" modules.
Scenario ... I want to go through all the files and directories on a big drive, and find the largest file(s). It will take a while, so I want progress reports, and I want to be able to suppress and ^C (control C) inputs by the user so that the program can't be stopped without very specific actions.
Let's look at the code section by section.
Firstly, set up the %SIG hash so that the ^C signal runs a function called jose and the USR1 signal sets a flag to indicate that it's been received (you rarely do very much in a signal handler, as you don't know exactly when it will be run - it's usual just to get a flag).
sub jose {
print "No way!\n";
$jose_counter++;
die ("\nInsistent little toad!\n") if ($jose_counter > 2) ;
}
$SIG{"INT"} = \&jose;
$jose_counter = 0;
sub sofar {
$sof = 1;
}
$SIG{"USR1"} = \&sofar;
$sof = 0;
print "My ID is $$\n";
Set up a queue to contain a list of all the directories to be traversed. We'll push new directories we find onto it, and we'll shift ones that we've already covered off it ... so that when the queue is empty, we know the whole traversal has been completed. A queue like this is far easier to implement than a circular buffer with two pointers, which is the 'conventional' way of doing the job.
@queue = ("/Users/grahamellis");
$biggest = 0;
$occasional = 0;
By default, Perl buffers output. We want it to flush its buffers every time it write to STDOUT, so that our progress reports come out in a single stream and not spurts of 4k characters
$| = 1;
Get a directory off the list, and read all the things (symbols) it contains:
while ($current = shift (@queue)) {
opendir (DH,$current);
@things = readdir DH;
Loop through the contents ... eliminate the current directory and the parent directory (this is done to avoid an infinite loop of directories) and then check if the item is a directory. If it IS, add it to the queue to be handled later.
foreach $item (@things) {
next if ($item =~ /^\.{1,2}$/);
$filename = "$current/$item";
if (-d $filename) {
push @queue,$filename;
next;
}
Not a directory, so look at the (file) size. Because we just did a -d operation on the file, we can now just use an underscore with the -s; the _ instructs Perl to reuse the results of its last enquiry from the file system ... it will have buffered up all the stat information.
If the file is larger than any already checked, make a note of the name and size:
if (-s _ > $biggest) {
$biggest = -s _;
$bf = $filename;
}
}
Do we need to update the status line? We have chosen to do so once every 50 directories parsed, reporting the name of the latest directory and queue length. We have very strictly ensured that the report will always be the same number of characters long, so there is no chance of "droppings" being left on the end of a report line, and we have used \r to give a carriage return without a line feed.
$occasional = ($occasional + 1) % 50;
if (! $occasional) {
$cur = $current;
length($cur) > 25 and
$cur = substr($cur,length($cur)-25);
printf "Q len %4d - %30.30s\r",@queue+0,$cur;
}
If the USR1 signal has been received during the latest cycle through the loop, report now on the largest file so far, and turn the reporting back off (the signal wants to say "report once" rather than "report every cycle from now on" after all!)
if ($sof) {
print "\nSo far - $biggest, $bf\n";
$sof = 0;
}
}
And at the VERY end, print the results!
print "\nbiggest is $bf, sized $biggest\n";
Full source code [here]
Update - added 26th August I have received an email overnight from a gentleman who points out that my Perl code (above) fails to "use strict", makes poor use of file handles, uses variable names that aren't very good, and so on. He's right - it wasn't claimed to be an example of excellent practise.
The code was written during a course earlier this week, illustrating points as I wrote it, and showing the mechanisms involved without overburdening the source with extras. As usual, some of my delegates have existing code that they have to maintain, and some of the less commonly used structures in my example are there to illustrate those structures ... not because it's necessarily a good idea to use them in production code. If you follow the link to the full source code, you'll find further details as to what this example is (and is not) intended to be.