Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: Very curious Problem...

by muba (Priest)
on Jan 23, 2013 at 12:06 UTC ( #1014892=note: print w/replies, xml ) Need Help??

in reply to Very curious Problem...

++ 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.

Replies are listed 'Best First'.
Re^2: Very curious Problem...
by anaconda_wly (Scribe) on Jan 23, 2013 at 15:17 UTC
    Very interesting! Thanks for the share.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1014892]
and snow settles gently...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2018-05-27 05:34 GMT
Find Nodes?
    Voting Booth?