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
In reply to Re: Simple question about foreach and my.
by davorg
in thread Simple question about foreach and my.
by Xxaxx
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |