Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^5: Conditional Operator Confusion

by ikegami (Patriarch)
on Nov 30, 2006 at 17:12 UTC ( [id://587014]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Conditional Operator Confusion
in thread Conditional Operator Confusion

The conditional operator has higher precedence than the assignment operator, so
$c?$x='TRUE':$y='FALSE'
is the same as
($c?$x='TRUE':$y)='FALSE'

The addition operator has higher precedence than the conditional operator, so
$c?$x+2:$y+3
is the same as
$c?$x+2:($y+3)

If you want to do assignments, use if or add parens. Think of as the conditional operator as an operator that returns something.

Replies are listed 'Best First'.
Re^6: Conditional Operator Confusion
by Melly (Chaplain) on Nov 30, 2006 at 17:25 UTC

    Thanks - I think I've finally got it (with some help from the chatterbox as well).

    All of which goes to confirm something a coworker once told me..

    Me: What's the precedence in <lang> for these operators?
    CW: Who cares? Use brackets - it's easier to read and
        you won't get screwed...
    
    Tom Melly, pm@tomandlu.co.uk

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-25 09:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found