in reply to my (0?$a:$b): a koan

I think my(EXPR?$x:$y) = 3; is equivalent to:
my $x = 3 if EXPR; my $y = 3 if !EXPR;
which is a known issue, and may do unexpected things if EXPR is false (although there has been code out there that (mis)uses this). Since 5.10 there has been a warning on
my $x if EXPR;
is EXPR can be determined to be false at compile time (constant folded to 0).

I wouldn't worry about my(0?$x:$y) too much. It's not something people will be do by accident - it isn't documented to work, so people who do it shouldn't be surprised if their code no longer works after upgrading Perl.

Replies are listed 'Best First'.
Re^2: my (0?$a:$b): a koan
by educated_foo (Vicar) on May 05, 2011 at 14:42 UTC
    I wouldn't worry about my(0?$x:$y) too much.
    Oh, I'm not "worried" -- there's no need to either deprecate or document it. I just think: (1) it is surprising that it parses at all, since I thought "my" took a list of variables; and (2) it shows the compile- and run-time effects of "my" in an interesting way.

      it is surprising that it parses at all, since I thought "my" took a list of variables

      I suspect that it takes any expression, then checks if the expression is valid.

      >perl -e"my ( f($x, $y.) );" syntax error at -e line 1, near ".) " Execution of -e aborted due to compilation errors.

      The catch here is that that constant folding happens before the check.

      Another allowed expression:

      >perl -e"my(my(my(my $sharona)))" >
        I suspect that it takes any expression, then checks if the expression is valid.
        Yeah, but even then, it will reject most expressions:
        $perl -we 'my(f($x))' Can't declare subroutine entry in "my" at -e line 1, at EOF Execution of -e aborted due to compilation errors.
        Now, my does return an lvalue, and ?: can, but that isn't enough in itself for it to be accepted by my:
        $ perl -we 'my($x = 3)' Can't declare scalar assignment in "my" at -e line 1, at EOF Execution of -e aborted due to compilation errors.
        What the reason is that my(my $x) and my(0?$x:$y) are accept isn't clear to me.

        This one is interesting:

        $ perl -we 'my(local $x)' Can't localize lexical variable $x at -e line 1. $ perl -we 'perl -we 'local(my $x)' Can't localize lexical variable $x at -e line 1. $
        Regardless of the nesting of local and my, we get the same error message.

        Mixing state and my:

        $ perl -wE 'sub f {state (my $x); say ++$x} f; f;' 1 1 $ perl -wE 'sub f {my (state $x); say ++$x} f; f;' 1 2 $ perl -wE 'sub f {state my $x; say ++$x} f; f;' No such class my at -e line 1, near "{state my" Execution of -e aborted due to compilation errors.
        Mixing our and local:
        $ perl -wE 'sub f {our local $x; say ++$x} f; f;' No such class local at -e line 1, near "{our local" Execution of -e aborted due to compilation errors. $ perl -wE 'sub f {our (local $x); say ++$x} f; f;' 1 1 $ perl -wE 'sub f {local our $x; say ++$x} f; f;' 1 1 $
        So we can have our (local $x), local our $x, but our local $x doesn't parse.