Why doesn't perl warn when overriding a variable in the inner scope?

As a little perl newbie, I just ran into an error where I accidentally did something like (e.g. 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 override the Perl foreach control variable? I understand that this behavior is expected; I should not receive a warning if the re-announcement is not in the same area.

However, I cannot understand why this is so. Why doesn't perl issue a warning here? It seems to me that this would be a probable cause of errors and, as a rule, was not intended. Is there a common case when this is a normal programming style, so the warning will be annoying?

+7
source share
1 answer

Perl has no tendency to warn about style issues, but it's hard for me to believe that someone intentionally wants to have the same var name at different depths.

The only time it can come in handy 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.

+7
source

All Articles