in reply to •Re: Re: •Re: Re: •Re: Seeking an idiom for localizing $/
in thread Seeking an idiom for localizing $/

To amplify what merlyn said, the relevant quote from perlop is:

Ordinarily you must assign the returned value to a variable, but there is one situation where an automatic assignment happens. If and only if the input symbol is the only thing inside the conditional of a "while" statement (even if disguised as a "for(;;)" loop), the value is automatically assigned to the global variable $_, destroying whatever was there previously. (This may seem like an odd thing to you, but you'll use the construct in almost every Perl script you write.) The $_ variable is not implicitly localized. You'll have to put a "local $_;" before the loop if you want that to happen.

However, I can not seem to find in the perl manual pages the explicit example from the Llama book (2nd edition):

Remember that this special magic requires a while loop. If you use the input operator anywhere else, you must assign the result explicitly if you want to keep the value:
if (<STDIN>) { print; } # WRONG, prints old value of $_ if ($_ = <STDIN>) { print; } # okay

It does feel weird to be quoting merlyn in a comment by merlyn. :)

  • Comment on Re: •Re: Re: •Re: Re: •Re: Seeking an idiom for localizing $/
  • Download Code