Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
Re^2: Playing with non-localized $_ in nested loops.by davido (Cardinal) |
on Aug 23, 2004 at 02:13 UTC ( [id://385000]=note: print w/replies, xml ) | Need Help?? |
gaal, Also commenting on your slides (not having heard the lecture), foreach is the exception as you said, but this is because foreach works in an entirely different way than while(). With respect to foreach, it is the loop construct itself that acts upon $_, in a very special way. We even write it in a very special way (if we write it out longhand):
...that's how foreach deparses with B::Deparse. Your slides mention that foreach is the exception. While it is an exception, it is not the only one. map also is a looping mechanism where $_ is localized. Consider the following code:
As you can see, the use of $_ inside of map works a lot like the iterator of a foreach loop, in that it serves as an alias to the elements of the input list (ok, my example doesn't demonstrate this, but it's true), and in that it is localized (my snippet shows this to be the case). Just a few observations and additional meditations... ;) Dave
In Section
Seekers of Perl Wisdom
|
|