Ouch. I just got some mail from the San Francisco Perl Mongers list*, to which I cross-posted my dilemma. In a nutshell, the problem--which the poster, David Schweitzer, correctly guessed without seeing the rest of my code!--is that I was calling the subroutine enclosing the snippet from within another foreach loop. I had tacitly assumed that $_ would be localized within a subroutine, but it isn't so, because $_ and other punctuation variables are global. Hence my subroutine was clobbering its caller's idea of $_ and vice versa, leading to a most confusing situation.

The situation, interestingly enough, is to localize $_ within the called subroutine. I haven't gotten this to work yet, but David pointed me at an article by Mark-Jason Dominus called <cite> Seven Useful Uses of local</cite> that explains the solution. Pretty neat (or pretty horrible in that it's not default behavior--depends on how you look at it). ;)

*Randal, we miss you! You should come and give another talk sometime when you're in the City on a fourth Tuesday. :)


In reply to It was a $_ scoping issue! by Anonymous Monk
in thread foreach gotcha! by Q*bert

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.