welchavw has asked for the wisdom of the Perl Monks concerning the following question:

Why doesn't the angle operator localize its use of $_ within a conditional while loop? The implicit localization of $_ by for wonderfully helps tersely coded for-loops avoid incurring "globals side-effect hell." I would guess that the motivation for the for-localization goes something like "the behavior is what most would expect", given that most would expect the effects on loop variables not to persist beyond the scope of the loop. However, I (perhaps naively) think this would apply to the use of $_ by the angle operator within while as well. This is not a rant - I merely wonder if this form of implicit localization was considered and "shot-down" for other reasons and, if so, what those reasons were?

Here is illustrative code...

sub f { $_++ for @_; } use FileHandle; sub angle { my $fh = FileHandle->new(q(echo hi |)) or die "$cmd failed\:$!"; while (<$fh>) {}; #nop } $_ = 1; @a = qw(0 1 2); local $" = ':'; print "original values:\n"; print "\t\$_=>$_\n"; print "\t\@a=>@a\n"; f @a; print "for has modified \@a, using a localized \$_\n"; print "\t\$_=>$_\n"; print "\t\@a=>@a\n"; angle; print "angle has modified a non-localized \$_\n"; print "\t\$_=>$_\n";

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

    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


      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

Re: Why doesn't the angle operator localize $_
by Abigail-II (Bishop) on Oct 15, 2003 at 15:58 UTC
    It just is. If there were pressing design issues, they have been forgotten over time. The non-localizing does have its uses though, if allows you do stuff like last if /something interesting/ and have the interesting line in $_ at the end of the loop.

    But whatever the reason was at the time, it's now too late to change.

    Abigail