Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^7: Short form (ternary) if else

by wrog (Friar)
on Feb 08, 2012 at 23:32 UTC ( [id://952623]=note: print w/replies, xml ) Need Help??


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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://952623]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-03-28 08:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found