Well, I am red-faced with embarassment. Having worked around Perl for several years, I found one of those "traps" that must frustrate "newbies" but that I should've seen coming given my time with Perl.

A construct that I (and I suspect otheres) have used is the following:

foreach my $line (@text){ chomp($line); next if($line =~ /^\s*#/); #...do something with the non-commented lines }

The surprise was not in the basic construct, but rather that I forgot that $line is just an alias for each of the actual elements in the array @text. Of course the array could be anything (doesn't have to be text) and the processing in the loop could be anything.

The important thing is that anything (e.g., chomp($line) in this case) that alters the loop variable (e.g., $line) is actually operating on and affecting the actual values in the array.

Plenty of documentation reminds (and even warns us) of that fact...but I am almost continually forgetting it. I am amazed that this doesn't bit me more frequently...maybe if it did, I'd do a better job of remembering it...he says with a wry grin.

It just now bit me on a little test case I had quickly coded up as I looked at one of the Q&A's about 'arrays' when I went to print out the value of @text after processing it in the above loop and then push()'ing the result into an ouput array which I then went on to print out for comparison.

Much to my dismay, when I printed out @text after the loop I found that all of the new-lines were gone!

I had to stop and fret over it and then I remembered! In chomp()'ing the loop variable, I was actually chomping the array's member! Duhhh! Well, color me 'embarassed'!

I think it is really powerful that the loop variable is an alias and I have actually used that to good advantage in several instnaces. But it must be really mysterious to those new to Perl that using common constructs like the comment-removal code I showed above can result in some pretty mysterious behavior.

Just an observation regarding the power and, sometimes, mystery of Perl.

ack Albuquerque, NM

In reply to I should've known better! by ack

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.