in reply to Re: A curious case of of my()
in thread A curious case of of my()

Thanks, I forgot about scope-exit behavior...

Anyway I'm wondering why a post-fix if doesn't force a new scope, IMHO that would solve the problem in a consistent way:

use strict; use warnings; $a=0; if ($a) { my $z=666; } my $y=666 if $a; print $y; # -> nothing print $z; # -> Global symbol "$z" requires explicit package na +me

of course treating short circuit and in the same way could cause more complications.

$a and my $x =666; print $x; # -> nothing

At least this "it's a new scope" logic could be used to detect "my"-problems at compile-time and throw warnings.

Cheers Rolf

UPDATE: short-circuit and

Replies are listed 'Best First'.
Re^3: A curious case of of my()
by ikegami (Patriarch) on Apr 05, 2011 at 16:13 UTC

    It wouldn't fix the following:

    foo() and my $x; foo() or my $x; foo() && my $x; foo() || my $x; foo() // my $x; foo() ? my $x : 1;

    Not to mention that some code relies on the current behaviour.

      > Not to mention that some code relies on the current behaviour.

      unlikely, without a new scope this would lead to warnings like

      "my" variable $x masks earlier declaration

      all these cases should be treated alike, (also see update in former post)

      AFAIK post-fix if is always internally translated into a short-circuit and.

      Cheers Rolf

        No, it doesn't, but it does warn "Deprecated use of my() in false conditional" now.

        AFAIK post-fix if is always internally translated into a short-circuit and.

        The compiled form of if and unless always use one of the and or or opcodes. There is no if or unless opcode.

        $ perl -MO=Concise,-exec -e'if (f()) { g() }' 1 <0> enter 2 <;> nextstate(main 3 -e:1) v:{ 3 <0> pushmark s 4 <$> gv(*f) s/EARLYCV 5 <1> entersub[t1] sKS/TARG,1 6 <|> and(other->7) vK/1 7 <0> pushmark s 8 <$> gv(*g) s/EARLYCV 9 <1> entersub[t2] vKS/TARG,1 a <@> leave[1 ref] vKP/REFC -e syntax OK $ perl -MO=Concise,-exec -e'if (!f()) { g() }' 1 <0> enter 2 <;> nextstate(main 3 -e:1) v:{ 3 <0> pushmark s 4 <$> gv(*f) s/EARLYCV 5 <1> entersub[t1] sKS/TARG,1 6 <|> or(other->7) vK/1 7 <0> pushmark s 8 <$> gv(*g) s/EARLYCV 9 <1> entersub[t2] vKS/TARG,1 a <@> leave[1 ref] vKP/REFC -e syntax OK