in reply to "if" in a my declaration statement causes problem
http://perldoc.perl.org/perlsyn.html:
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.A short explanation of the behavior you are seeing: my has both a compile-time and run-time effect. At compile-time, it establishes a lexical variable for the remainder of the current scope, so any mention of the variable name is bound to an offset in an area known as the pad. All pads are initially set up with undefined variables. When a scope is exited, any lexicals that were actually used are reset to undef so they are ready for the next time the scope is entered. When the my is reached at run-time, it records that the lexical was used and needs to be cleared when the scope is left. By having the if there, you are preventing the my() from executing at run-time some of the time, so the scope-exit code doesn't know that the lexical was used, so it isn't reset to undef, so next time the scope is entered the previous value is still there.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: "if" in a my declaration statement causes problem
by ganeshk (Monk) on Feb 20, 2006 at 14:06 UTC | |
|
Re^2: "if" in a my declaration statement causes problem
by mikeraz (Friar) on Feb 22, 2006 at 00:40 UTC | |
by ysth (Canon) on Feb 22, 2006 at 19:15 UTC |