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

I'm running v5.8.5. Will $_ be auto-localized for
while (<>) { .. }
in the manner of
while (local $_ = <>) { .. }
in future versions? If not, what are the impediments?

Reference: Sins of Perl Revisited

Replies are listed 'Best First'.
Re: Auto-localization of $_ in while(<>)
by ysth (Canon) on Feb 05, 2008 at 06:23 UTC
    In perl 5.10.0, you can say
    while (my $_ = <>) { }
    and avoid some of the problems that can happen with local when the previous $_ is aliased to something that objects to being localized.

    But, no, I don't see while() changing to no longer have the modified $_ available after the loop.

Re: Auto-localization of $_ in while(<>)
by jwkrahn (Abbot) on Feb 05, 2008 at 06:00 UTC

    It isn't localized in current versions so I don't think that it will be changed in future versions because of backward compatibility.

    john@perl:~$ perl -e' my $lines = qq[one two three four ]; open F, "<", \$lines or die "open: $!"; while ( <F> ) { last if /h/; } print $_; ' three
Re: Auto-localization of $_ in while(<>)
by kyle (Abbot) on Feb 05, 2008 at 03:55 UTC

    I suppose the impediment is compatibility with older code. Code that was written with a dependency on the current (somewhat icky) behavior would break.