Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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

by Aristotle (Chancellor)
on Aug 23, 2004 at 10:19 UTC ( [id://385040]=note: print w/replies, xml ) Need Help??


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

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.

Replies are listed 'Best First'.
Re^5: Playing with non-localized $_ in nested loops.
by gaal (Parson) on Aug 23, 2004 at 11:35 UTC
    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.

        Ah, that's a good explanation. Thanks!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-03-19 11:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found