in reply to scope of an autovivified variable?
From perlsyn:
NOTE: The behaviour of a my statement modified with a statement modifier conditional or loop construct (e.g. my $x if ... ) is undefined. The value of the my variable may be undef, any previously assigned value, or possibly anything else. Don't rely on it. Future versions of perl might do something different from the version of perl you try it out on. Here be dragons.
I think that's clear enough. And perlsyn is far from obscure documentation.
Update:
Perl doesn't natively complain to you if you use such a construct. However, there's a tool that I have in my arsenal which is so simple to use that I've managed to make it a habit as I work on scripts. Start with Perl::Critic. Next, create a Perl script based on the synopsis of Perl::Critic's documentation, as follows:
use Perl::Critic; my $file = shift; my $critic = Perl::Critic->new(); my @violations = $critic->critique( $file ); print @violations;
Now, name that script 'criticize', and put it in your favorite toolbox bin directory. Finally, as you're composing scripts, periodically pass it through the complaint department like this:
criticize mytest.pl
And the output for the specific 'violation' we're discussing:
Variable declared in conditional statement at line 9, column 1. Declar +e variables outside of the condition.
Dave
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: scope of an autovivified variable?
by rmcgowan (Sexton) on May 11, 2011 at 20:06 UTC | |
by davido (Cardinal) on May 11, 2011 at 20:23 UTC |