in reply to Term::ProgressBar won't update

G'day cormanaz,

"I don't understand what is setting $_ ..."

In the examples in the "module docs", $_ is being set in the outer loop and $i is being set in the inner loop. A quick demonstration:

$ perl -E ' for ("a" .. "b") { for my $i (1 .. 2) { say "\$_[$_] \$i[$i]"; } } ' $_[a] $i[1] $_[a] $i[2] $_[b] $i[1] $_[b] $i[2]

Using a named variable, as you've done, is much safer. $_ is a global variable: it could be changed by something in "stuff my script is actually processing"; or, some later addition to your code could affect it.

$ perl -E ' for my $f ("a" .. "b") { for my $i (1 .. 2) { say "\$_[$_] \$f[$f] \$i[$i]"; } } ' $_[] $f[a] $i[1] $_[] $f[a] $i[2] $_[] $f[b] $i[1] $_[] $f[b] $i[2]

Here's a highly contrived example of additional code changing $_:

$ perl -E ' for my $f ("a" .. "b") { for my $i (1 .. 2) { say "\$_[$_] \$f[$f] \$i[$i]"; } s/$/.bu/; } ' $_[] $f[a] $i[1] $_[] $f[a] $i[2] $_[.bu] $f[b] $i[1] $_[.bu] $f[b] $i[2]

If you'd put "use warnings;" near the top of your code, you would have seen a series of "Use of uninitialized value $_ ..." warnings. This may have alerted you to the problem you describe.

While I appreciate that the code you provided was purely for demo purposes, I thought I'd point out that "my $max = $f;" is going to be a problem with real filenames (you may already be aware of this). You get no output from:

$ perl -E ' for my $f ("a" .. "b") { my $max = $f; for my $i (1 .. $max) { say "\$f[$f] \$i[$i]"; } } '

Again, the warnings pragma points to the problem:

$ perl -E ' use warnings; for my $f ("a" .. "b") { my $max = $f; for my $i (1 .. $max) { say "\$f[$f] \$i[$i]"; } } ' Argument "a" isn't numeric in foreach loop entry at -e line 7. Argument "b" isn't numeric in foreach loop entry at -e line 7.

I strongly recommend that you include the warnings pragma in all of your code. It's lexically scoped: turn off selected warnings, in a limited scope, for special cases.

$ perl -E ' use warnings; my @rgb = qw{#ff0000 #00ff00 #0000ff}; ' Possible attempt to put comments in qw() list at -e line 4. $ perl -E ' use warnings; my @rgb; { no warnings "qw"; @rgb = qw{#ff0000 #00ff00 #0000ff}; } '

— Ken