in reply to "if" in a my declaration statement causes problem

Um, Don't Do That Then?

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
    Thanks very much, ysth for the explanation.
Re^2: "if" in a my declaration statement causes problem
by mikeraz (Friar) on Feb 22, 2006 at 00:40 UTC

    My dense skull isn't penetrating this issue. The original poster wrote ...

    Update: No, the original poster did not write what I claimed. The first response did.

    my $x; # one statement $x = $y if {condition;} # two statement
    the perlsyn you quoted is different
    my $x if ... presuming my $x = $y if {condition;}
    in his case. These seem to be different. You are holding up the perlsyn snip as an example says the two cases are synonyms. Why?

    In my testing of this, it works (perhaps version dependent?):

    sunorccws04 ~$ cat tpl ; ./tpl #!/usr/bin/perl sub showme; foreach $y ( 1..10) { $foo = $y; print showme(), "is the x value\n"; } sub showme { my $x; $x = $y if ($foo %2); # might as well mix up some T/F } 1 is the x value 0 is the x value 3 is the x value 0 is the x value 5 is the x value 0 is the x value 7 is the x value 0 is the x value 9 is the x value 0 is the x value
    getting strict "breaks" the behavior,
    sunorccws04 ~$ cat tpl ; ./tpl #!/usr/bin/perl use strict; my ($foo, $y); sub showme; foreach $y ( 1..10) { $foo = $y; print showme(), " is the x value\n"; } sub showme { my $x; $x = $y if ($foo %2); # might as well mix up some T/F } is the x value 0 is the x value is the x value 0 is the x value is the x value 0 is the x value is the x value 0 is the x value is the x value 0 is the x value
    predefining x as in  my $x = 0;, doesn't change the results. Changing the declaration of $y, $foo from my to our causes the code to behave like the non-strict version. But in all cases the results are consistant and predictable.

    ??

    Be Appropriate && Follow Your Curiosity
      My dense skull isn't penetrating this issue. The original poster wrote
      my $x; # one statement $x = $y if {condition;} # two statement
      Both when I posted and now, the OP reads: my $x = $y if ( <some condition> ); Are you looking at someone else's reply?

      With the my $x separate and unconditional, the warning in perlsyn doesn't apply.

      The output from your strict version doesn't make sense to me, offhand.