Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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

by gaal (Parson)
on Aug 23, 2004 at 06:18 UTC ( [id://385014]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Playing with non-localized $_ in nested loops.
in thread Playing with non-localized $_ in nested loops.

for, map, and grep: these three constructs localize $_. But in fact, map and grep are more vulnerable than foreach, because they don't give you the choice of using a lexical as your aliased iterator (iterant?). I did mention this in the talk though I guess the emphasis is stronger when you have more than the slides :)

Replies are listed 'Best First'.
Re^4: Playing with non-localized $_ in nested loops.
by Aristotle (Chancellor) on Aug 23, 2004 at 10:19 UTC

    There is a difference between localizing $_ and aliasing it to something else. Do not confuse the two.

    Note also that there is no construct which implicitly localizes $_, but there are several which alias it to something else.

    Obviously, while(<FH>) cannot alias the variable to anything, because one would have to be able to expect that modifying $_ would actually modify the file. Instead, readline returns a copy of the data found in the file.

    It really is obvious and very consistent if you think about it. Perl just DWIMs well enough in so many cases that it's easy to overlook some of the ramifications of any single operation.

    Makeshifts last the longest.

      The aliasing is local to the scope of these constructs, so why is it confusing things to say that the constructs localize $_?

        Because it isn't localizing, which creates an entirely isolated new thing for $_ to refer to. It's binding, in which $_ takes on the role of another scalar. The results of modifying $_ inside an aliasing construct persist past that construct, even if not in $_. When you localize it, the result is forever lost after the block. local creates an isolated reality; the aliasing constructs do not.

        Makeshifts last the longest.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-03-19 07:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found