In Perl, does using 'my' within a foreach loop have any effect? It seems that the index variable is always local whether or not 'my' is used. So can you drop the 'my' within the foreach loop and still have private scope within the body of the loop?
As can be seen, using the 'for' loop there is a difference between using / not using 'my':
use strict;
use warnings;
my ($x, $y) = ('INIT', 'INIT');
my $temp = 0;
for ($x = 1; $x < 10; $x++) {
$temp = $x+1;
}
print "This is x: $x\n"; # prints 'This is x: 10'.
for (my $y = 1; $y < 10; $y++) {
$temp = $y+1;
}
print "This is y: $y\n"; # prints 'This is y: INIT'.
But on foreach it does not seem to have an effect:
my ($i, $j) = ('INIT', 'INIT');
foreach $i (1..10){
$temp = $i+1;
}
print "\nThis is i: $i\n"; # prints 'This is i: INIT'.
foreach my $j (1..10){
$temp = $j+1;
}
print "\nThis is j: $j\n"; # prints 'This is j: INIT'.