Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

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

by Hofmator (Curate)
on Jul 13, 2001 at 17:42 UTC ( #96391=note: print w/replies, xml ) Need Help??

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

Good explanation, Abigail. I'm just curious, is there a good reason why while (<>) doesn't implicitely localize $_?

-- Hofmator

Replies are listed 'Best First'.
Re: Re: Re: When is $_ local and when is it not?
by chipmunk (Parson) on Jul 13, 2001 at 18:24 UTC
    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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://96391]
[Tux]: None work for me. My dead key is AltGr (right Alt)
[Tux]: And I just tested, neither do other Tk apps :) (95% of my Tk Apps is either just monitoring (no input) or input of just numbers). Something to investigate then
[choroba]: Maybe something like this?
[choroba]: I vaguely remember we had problems with dead keys in Tk at $work - 2, but I forgot all the details
[choroba]: but it was worse on MSWin than on Linux
[choroba]: (and OSX didn't exist yet)

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (9)
As of 2017-09-25 20:45 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (290 votes). Check out past polls.