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 | |
by welchavw (Pilgrim) on Oct 16, 2003 at 15:12 UTC | |
|
Re: Why doesn't the angle operator localize $_
by Abigail-II (Bishop) on Oct 15, 2003 at 15:58 UTC |