in reply to Re^6: Short form (ternary) if else
in thread Short form (ternary) if else

operator precedence; ? : binds tighter than =. So

1 ? $x=1 : $x=0

really means

(1 ? $x=1 : $x)=0

which is then the same as

($x=1)=0

which is why $x becomes 0.
You can use assignments within ? :, you just need to parenthesize them

1 ? $x=1 : ($x=0)

Replies are listed 'Best First'.
Re^8: Short form (ternary) if else
by Riales (Hermit) on Feb 08, 2012 at 23:43 UTC
    Ah, perfect explanation. I knew it had something to do with operator precedence but I just couldn't work out quite where it was happening. Thank you, good sir!