in reply to foreach gotcha!

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. :)

Replies are listed 'Best First'.
RE: It was a $_ scoping issue!
by merlyn (Sage) on Sep 22, 2000 at 05:17 UTC
    *Randal, we miss you! You should come and give another talk sometime when you're in the City on a fourth Tuesday. :)
    Hey, I'll be in your fair city 30 Oct to 3 Nov... maybe we can set something up! (Whomever you are!)

    -- Randal L. Schwartz, Perl hacker