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


In reply to Re: Term::ProgressBar won't update by kcott
in thread Term::ProgressBar won't update by cormanaz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.