From the section on context in perldoc perldata
:
Assignment is a little bit special in that it uses its left argument to determine the context for the right argument. Assignment to a scalar evaluates the right-hand side in scalar context, while assignment to an array or hash evaluates the righthand side in list context. Assignment to a list (or slice, which is just a list anyway) also evaluates the right-hand side in list context.
(emphasis added)
And from perldoc perlop
:
Conditional Operator
[...]
Scalar or list context propagates downward into the 2nd or 3rd argument, whichever is selected.
$a = $ok ? $b : $c; # get a scalar
@a = $ok ? @b : @c; # get an array
$a = $ok ? @b : @c; # oops, that's just a count!
As for precedence, ||
has a higher precedence than the conditional operator, so
my $result = $foo || $bar ? 0 : 1;
is equivalent to
my $result = ($foo || $bar) ? 0 : 1;
Note that the parentheses do not create list context, which is a common misconception. They aren't necessary in this case, but they do improve readability, so I would recommend using them.
Again, in
my $result = $foo || ($bar ? 0 : 1);
parentheses do not create list context. With ||
Scalar or list context propagates down to the right operand if it is evaluated.
so ($bar ? 0 : 1)
is evaluated in scalar context (assuming $foo
is a true value; if not, ||
short-circuits and the RHS isn't evaluated at all).