Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: Short form (ternary) if else

by gg48gg (Sexton)
on Feb 08, 2012 at 20:29 UTC ( [id://952575]=note: print w/replies, xml ) Need Help??


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

Yes, that works beautifully! Thank you! I realize the scope issue as well. So, what I understand is that ternary form is only for use after an assignment (=) operator? Is that correct?

Replies are listed 'Best First'.
Re^3: Short form (ternary) if else
by tobyink (Canon) on Feb 09, 2012 at 11:18 UTC

    No, it doesn't have to be used in conjunction with assignment. Consider:

    printf("X is %s\n", defined $X ? $X : 'undefined');

    Or:

    sub get_name { # ... return wantarray ? ($firstname, $middlename, $lastname) : "$firstname $lastname"; }
Re^3: Short form (ternary) if else
by DStaal (Chaplain) on Feb 09, 2012 at 21:40 UTC

    Yes and no... It is typically at it's most useful after an assignment, and it's use is that it generates a value, but that doesn't necessarily mean it needs to be used in an assignment. Others have given examples, but the essential thing to remember is that the ternary operator returns a value. if does not: It is a flow-control operator.

    So, where it does need to be used is where Perl would expect a value. Assignments are one common case of that.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-20 04:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found