in reply to Why doesn't the angle operator localize $_

I think maybe a better question would be, "why does for (LIST) {} localize $_". The answer would be that $_ (and in fact whatever iterator var is used) is aliased to value in the list. This allows all kind of funkyness, but also effectively means that the var used is localized (otherwise it couldnt be an alias temporarily.)

Contrast this to a three argument for loop (which is really a while loop in disguise, or perhaps its the other way around ;-), which doesnt alias, and doesnt localize.

Going back to the IO operator, how could we alias it? The data comes from a file. Theres nothing to alias to. A change to the var that we read in won't change the file. Wheras a change to the var in a for loop does change the thing we are iterating over. Both of these behaviours are sensible and useful, but you have to ask the right question.

E:\>perl -le "$x=10; @a=(0..5); for $x (@a) { print $x; $x=int(rand 10 +0); } print $x; print qq(@a)" 0 1 2 3 4 5 10 41 44 77 55 23 70

BTW, of course Abigail-II is right that by now theres no way to tell, (at least not without lots of research, and even still it might come down to an on the spot decision by Larrry that he may or may not remember), but i would guess that aliasing is part of it.


---
demerphq

    First they ignore you, then they laugh at you, then they fight you, then you win.
    -- Gandhi


Replies are listed 'Best First'.
Re: Re: Why doesn't the angle operator localize $_
by welchavw (Pilgrim) on Oct 16, 2003 at 15:12 UTC

    Thanks to all for the helpful, insightful replies. I would, however, like to comment a bit on the relationship of localization to aliasing. It seems to me (without knowing the implementation details) that localization in such a while loop need not imply aliasing as with for. The angle operator semantics are already different than for with regards to $_, so I wouldn't weep for consistency. Instead of aliasing, $_ could simply be localized to a temporary value that could repeatedly be assigned the results of IO. Therefore, I don't understand why the inappropriateness of aliasing would have ever precluded while being written in this way in the first place. I thoroughly understand, however, why the weight of history precludes a change. Nevertheless, I find myself a bit disappointed in this difference between for and while when I consider that these operators are often treated as completely interchangeable, by the naive.

    ,welchavw