++ for demonstrating the problem in a simple self-contained piece of code. One advantage of reducing a problem to the smallest set of instructions that replicates the problem, is that it allows you to debug it using your Eyeball Mk I (and maybe a pencil and a piece of paper).

Using your sample code, let's Eyeball Debug it, starting at the top and following along with the flow of the code.

I don't know how you pronounce $_ — maybe you call it "dollar underscore" whatever, but I call it "it". That's right. So if ($_ == 3), to me, reads "if it equals 3", and @foo = grep { defined $_ } @bar; I would pronounce as "at-foo now is ... grep-if it is-defined ... from at-bar".

Why do I share this? Because I feel it really helps to think of $_ as being "it". Let's go:

STEP CODE MEANING + IT IS NOW... 1 &main(); call 'main', no arguments + undef 2 my @mylist = qw(a b c); create and define @mylist + undef 3 foreach(@mylist) iterate over @mylist + undef 3a iteration 1 + it is now "a" 4 &fun($_); &fun(it) + "a" 5 open(STATUS, "echo d-e-f|"); echo "d-e-f" into new fh STATU +S "a" 6 while (<STATUS>) sequentially read lines + "a" 6a iteration 1: read it. + it is now "d-e-f" 7 if (/d-e-f/) if it matches (and it does), + "d-e-f" 8 close(STATUS) close the FH + "d-e-f" 9 return; go back to whence we came + "d-e-f" 10 print "changed: \$_ $_\n" print it + "d-e-f" 3b iteration 2 + it is now "b" ...snip...

This clearly shows where, how, and why it (or dollar-underscore) got changed.

Solutions have been given elsewhere in this thread. I just wanted to show you how I would've debugged it. The code, that is, not the value of $_. Or maybe both.


In reply to Re: Very curious Problem... by muba
in thread Very curious Problem... by anaconda_wly

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.