As a bit of a perl novice, I just ran into a bug where I accidentally did something like (example simplified):
my $i=0;
for(my $i=0;$i<10; $i++)
{
print $i;
}
print $i; # $i is zero, my code expected 9
From Why don't I get a warning when I redeclare the Perl foreach control variable? I understand that this behavior is expected; I should not get a warning unless the re-declaration is in the same scope.
However, I can not understand why this is the case. Why would perl not issue a warning here? It seems to me that it would be a likely cause of errors, and usually not intended. Is there some common case where this is normal programming style, so that the warning would be annoying?
Perl doesn't tend to warn for style issues, but I find it hard to believe that someone would intentionally want to have the same var name at different scope depths.
The only case where it might be useful that comes to mind is
{
my $x = $x;
... do some something that changes $x ...
}
# $x is restored here.
On the plus side, there is a perlcritic
rule to identify such a problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With