Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Re: Re: When is $_ local and when is it not?

by chipmunk (Parson)
on Jul 13, 2001 at 18:24 UTC ( #96407=note: print w/replies, xml ) Need Help??


in reply to Re: Re: When is $_ local and when is it not?
in thread When is $_ local and when is it not?

One possible reason for why while(<>) does not implicitly localize $_ as part of its magic is that sometimes you want to access the last value of $_ outside the loop. For example:
while (<>) { chomp; last if /\S/; } print "You said: $_\n";
If $_ were localized, you would have to copy the value to another variable:
my $in; while (local $_ = <>) { chomp; $in = $_, last if /\S/; } print "You said: $in\n";
Although it may just be as Abigail said, that foreach always assigns to a variable as part of the loop, whereas while can have anything in the conditional and (<>) is just a special case.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://96407]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2017-02-24 20:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?






    Results (363 votes). Check out past polls.