in reply to Simple question about foreach and my.
Well, you should always declare your variables using the appropriate mechanism (usually my) or otherwise your script won't run under use strict.
The choice is not, therefore, between using or not using my, rather where's the best place to put the my. You can either do
foreach my $var (@array)
or
my $var; foreach $var (@array)
The first has the advantage that the scope of the variable is restricted to your foreach loop and in the second example the variable scope is the scope that contains the loop. However, the first syntax was introduced relatively recently (5.005 or thereabouts), so if you are running on older versions of Perl (and you shouldn't be) then you'll be forced to use the second method.
--
"Perl makes the fun jobs fun
and the boring jobs bearable" - me
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re (tilly) 2: Simple question about foreach and my.
by tilly (Archbishop) on Mar 27, 2001 at 19:24 UTC | |
|
Re: Re: Simple question about foreach and my.
by voyager (Friar) on Mar 27, 2001 at 20:25 UTC | |
by davorg (Chancellor) on Mar 27, 2001 at 20:29 UTC |