in reply to Tk::ProgressBar and system cp

Hi tame1,

A couple of things.

You only need to construct your progress bar once.  You then manage it by making changes to the variable (in your case, $percent_done).

I had never used Tk::ProgressBar before (I usually just construct my own), so I just tried it, and it's quite easy.

Here's an example that's pretty close to yours, except that it doesn't try to examine files from an array (like @SOURCE), but rather, for purposes of example, just changes the value of $percent_done:

#!/usr/bin/perl -w + use strict; use warnings; + use Tk; use Tk::ProgressBar; + my $percent_done = 0; + my $mw = new MainWindow(-title => 'Progress Bar Demo'); my $top = $mw->Frame()->pack(-expand => 1, -fill => 'both'); my $pb = $top->ProgressBar( -width => 20, -height => 200, -from => 0, -to => 100, -blocks => 10, -colors => [0, 'green', 50, 'yellow', 80, 'red'], -variable => \$percent_done ); $pb->pack(); $mw->after(100 => \&main_loop); MainLoop; + sub main_loop { for (my $i = 0; $i < 100; $i++) { $percent_done = $i; $mw->update(); select(undef, undef, undef, 0.1); } }

The delay loop is caused by the code select(undef, undef, undef, 0.1);.

It's important to issue an update to the main window with $mw->update();, otherwise you may never see the meter updating.

All you need to do is put your code in the subroutine main_loop (or whatever you change its name to), and make sure that you update the percentage variable $percent_done in a meangful way.  For example, if you're going by number of files processed $nprocessed, out of a total number of file $total, then $percent_done should be calculated something like:  $percent_done = 100 * $nprocessed / $total.

A final note:  In my experience, you may get a smoother progress meter if you go by total bytes rather than total files, especially in cases where some files are tiny, and some are huge.  The way to do this is make $total equal to the total bytecount (from all the files), and $nprocessed the running count of bytes processed.  Then the same formula should apply.

Good luck!


s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/