Jeff 'japhy' Pinyan wrote:
On Jul 15, Randy W. Sims said:


[I see this a lot on these groups. I know most of the time it is just
used for examples, but I thought I would point out that it is a bad habit.]

Anytime you make a direct assignment to the special variable $_, you
should local-ize it. This code demonstrates the problem:


I have "better" code that demonstrates the problem:

Thanks. That's a much better and less contrived example.

Using the <FH> operator implicitly assigns to $_, without localizing it.
Therefore, if you're going to use 'while (<FH>)', please local()ize $_
first.

That's got me wondering: are there any more operators or constructs that clobber $_ ? A few experiments show that C<for>, C<map> all localize $_. Am I missing any?


Not that I ever run into this anyway as I generally use an explicit variable:

foreach my $elem (@array)

or my version of the while (<FH>) in your example would probably be something like:

while (defined( my $line = <FH> ))

Randy.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to