Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Playing with non-localized $_ in nested loops.

by melora (Scribe)
on Aug 23, 2004 at 15:37 UTC ( #385111=note: print w/replies, xml ) Need Help??


in reply to Playing with non-localized $_ in nested loops.

This is highly educational for me, thanks very much for posting it. I must admit that I use $_ very sparingly, partly because I don't understand such things very well. I think it's also because of my longstanding habit of being very explicit (C, need I say more?). I'm still getting used to lines like
while (<>) { chomp; print $_, "\n"; }
because my old habits say "Where did $_ come from???" Going through such exercises as the above is helping me learn better, more Perlish ways.
I think it's fun and worthwhile to play with such things. And I'm not surprised at being confused on a Monday morning.

Replies are listed 'Best First'.
Re^2: Playing with non-localized $_ in nested loops.
by QM (Parson) on Aug 23, 2004 at 22:06 UTC
    Don't be too quick to knock your approach. I find it useful in "real" programs to use a named variable to indicate to my reader (me in most cases :) what I expect that variable to represent. This is especially useful in nested constructs where $_'s semantics change as construct boundaries are crossed.

    And if I change a foreach (...) to a while (...), the magic of $_ disappears, and I might introduce bugs similar to gaal's example.

    Sometimes using explicit variables helps you find logic errors too.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://385111]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2021-10-24 18:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (89 votes). Check out past polls.

    Notices?