Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Confused by Perl ternary operator

by fruiture (Curate)
on Aug 23, 2004 at 19:25 UTC ( #385190=note: print w/ replies, xml ) Need Help??


in reply to Confused by Perl ternary operator

Operator Precedence, again.

A ? B = C : D = E # resolves ( A ? ( B = C ) : D ) = E

So, no matter what A evaluates to, E is being assigned, either to "B = C" (which is the same as assigning to B) or D. In your case B and D are identical, so your ?: operator doesn't do anything useful. Solution:

@_ ? ( $param2 = shift ) : ( $param2 = "Shift" ); # and while we're at it, of course, refactor that $param2 = @_ ? shift : "Shift"

The last line demonstrates why the precedence of "?:" vs. "=" is actually sensible. HTH

--
http://fruiture.de


Comment on Re: Confused by Perl ternary operator
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2015-07-01 23:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (25 votes), past polls