As in C, ?: is the only trinary operator. It's often called the conditional operator because it works much like an if-then-else, except that, since it's an expression and not a statement, it can be safely embedded within other expressions and functions calls. As a trinary operator, its two parts separate three expressions:
If the condition COND is true, only the THEN expression is evaluated, and the value of that expression becomes the value of the entire expression. Otherwise, only the ELSE expression is evaluated, and its value becomes the value of the entire expression.COND ? THEN : ELSE
Scalar or list context propagates downward into the second or third argument, whichever is selected. (The first argument is always in scalar context, since it's a conditional.)
You'll often see the conditional operator embedded in lists of values to format with printf, since nobody wants to replicate the whole statement just to switch between two related values.$a = $ok ? $b : $c; # get a scalar @a = $ok ? @b : @c; # get an array $a = $ok ? @b : @c; # get a count of an array's elements
Conveniently, the precedence of ?: is higher than a comma but lower than most operators you'd use inside (such as == in this example), so you don't usually have to parenthesize anything. But you can add parentheses for clarity if you like. For conditional operators nested within the THEN parts of other conditional operators, we suggest that you put in line breaks and indent as if they were ordinary if statements:printf "I have %d camel%s.\n", $n, $n == 1 ? "" : "s";
For conditionals nested within the ELSE parts of earlier conditionals, you can do a similar thing:$leapyear = $year % 4 == 0 ? $year % 100 == 0 ? $year % 400 == 0 ? 1 : 0 : 1 : 0;
but it's usually better to line up all the COND and THEN parts vertically:$leapyear = $year % 4 ? 0 : $year % 100 ? 1 : $year % 400 ? 0 : 1;
Lining up the question marks and colons can make sense of even fairly cluttered structures:$leapyear = $year % 4 ? 0 : $year % 100 ? 1 : $year % 400 ? 0 : 1;
You can assign to the conditional operator[5] if both the second and third arguments are legal lvalues (meaning that you can assign to them), and both are scalars or both are lists (otherwise, Perl won't know which context to supply to the right side of the assignment):printf "Yes, I like my %s book!\n", $i18n eq "french" ? "chameau" : $i18n eq "german" ? "Kamel" : $i18n eq "japanese" ? "\x{99F1}\x{99DD}" : "camel"
Bear in mind that the conditional operator binds more tightly than the various assignment operators. Usually this is what you want (see the $leapyear assignments above, for example), but you can't have it the other way without using parentheses. Using embedded assignments without parentheses will get you into trouble, and you might not get a parse error because the conditional operator can be parsed as an lvalue. For example, you might write this:($a_or_b ? $a : $b) = $c; # sets either $a or $b to have the value of $c
But that would be parsed like this:$a % 2 ? $a += 10 : $a += 2 # WRONG
(($a % 2) ? ($a += 10) : $a) += 2
[5] This is not necessarily guaranteed to contribute to the readability of your program. But it can be used to create some cool entries in an Obfuscated Perl contest.
Copyright © 2001 O'Reilly & Associates. All rights reserved.