Hi,
I have a small conceptual problem. I have been
told that doing:
my $item;
foreach $item (@arr) { ... }
is more efficient than:
foreach my $item (@arr) { ... }
Because it does not reallocate memory each time.
This means that the scope of $item in the second
example is actually after the { }. Right ?
Then can someone explains why the following code:
#---Begin Code---
use strict;
my $par="50";
print "$par\n";
my @arr = ('first', 'second', 'third');
foreach $par (@arr)
{
print "$par\n";
}
print "$par\n";
#---End Code-----
produces the following output:
50
first
second
third
50
This means that the $par in the foreach loop is
NOT the same as the $par outside of the loop. However,
I use strict and I do not even do a "my $par". How can
this be possible ?
However, when I use the print "\$par", I see that it
allocates a different address on each iteration of
the loop, even without the par. So what does on
anyway ?
-Robin