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.

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

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!