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.