Pathologically Eclectic Rubbish Lister | |
PerlMonks |
Re^2: Warning while using ternary operatorby kcott (Archbishop) |
on Dec 16, 2013 at 15:19 UTC ( [id://1067344]=note: print w/replies, xml ) | Need Help?? |
This is bad information. The expression
is only "# WRONG" because of operator precedence (see perlop: Operator Precedence and Associativity). Here's how it is parsed:
This is explained in perlop: Conditional Operator. Correcting the precedence with parentheses:
I do not dispute that '$var = $expression ? "foo" : "bar";' is a better way to write '$expression ? ($var = "foo") : ($var = "bar");'. In fact, the documentation also recommends this. Once corrected as indicated, here's how your "# WRONG" code behaves:
Output:
-- Ken
In Section
Seekers of Perl Wisdom
|
|