This has been well-known for years. People have used it for things like:
sub increment {
my $counter = 0 if 0;
++$counter
}
It works the same way as:
{
my $counter = 0;
sub increment { ++$counter }
}
It's actually a combination of various bugs that makes it work, so you shouldn't rely on the behavior. But since people have been relying on the behavior for years, it probably won't go away any time soon.
Perl 5.10 has a keyword ("state") for declaring this sort of variable explicitly, though, so there's no reason to use this pattern in new code.
Perl 5.10 has a keyword ("state") for declaring this sort of variable explicitly, though, so there's no reason to use this pattern in new code.