in reply to Re^2: What's wrong with this local() ?
in thread What's wrong with this local() ?
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.
>perl -MO=Concise -e"local $x = $y 6 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 5 <2> sassign vKS/2 ->6 - <1> ex-rv2sv sK/1 ->4 3 <#> gvsv[*y] s ->4 - <1> ex-rv2sv sKRM*/129 ->5 4 <#> gvsv[*x] s/LVINTRO ->5 -e syntax OK
(Child has higher precedence than parent. LVINTRO = local)
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.
|
|---|