in reply to Re: What's wrong with this local() ?
in thread What's wrong with this local() ?

I think it may have been designed that way. When I look at the operator precedence table, I notice that the '=' is two spots higher in the table than list operators when viewed from the right (local). I also see that '=' evaluates it's right side 1st. So this seems to be a case of behavior matching documentation.

Replies are listed 'Best First'.
Re^3: What's wrong with this local() ?
by ikegami (Patriarch) on Jan 31, 2008 at 18:37 UTC

    When I look at the operator precedence table, I notice that the '=' is two spots higher in the table

    Precedence is irrelevant here. And local has higher precedence.

    It wouldn't work otherwise

    Give local higher precedence >perl -le"$x=2; { (local $x)=$x; print $x; $x=3; print $x } print $x" 2 3 2 Give assignment higher precedence >perl -le"$x=2; { local ($x=$x); print $x; $x=3; print $x } print $x" 2 3 3 <-- XXX

    I also see that '=' evaluates it's right side 1st.

    Where did you see this? Operand evaluation order determines which operand is evaluated first. The operand evaluation order for = and other Perl binary operators is not documented anywhere and should probably be considered undefined.

    Perhaps you looked up the associativity of the = operator. Associativity doesn't define which operand is evaluated first, it determines which operator is evaluated first when two operators of the same precedence are chained.

    However, you are right that this feature of my and local is implemented by choosing a right to left operand evaluation order for assignment operators.

    >perl -le"local(${print('a'), 'x'}) = ${print('b'), 'x'}; b a

    Updated: Updated code into "precedence is irrelevant" reply.