First, Perl's conditional operator is a ternary operator, but it's called "conditional operator," not "ternary operator." The "conditional operator" is a specific operator; "ternary operator" describes a kind of operator.
Use the conditional operator only for conditional statements that return a single result—one scalar, list, hash, reference, etc.—and use it only for what it returns. In general, use it mostly for assigments to variables. Don't put program behavior—actions—inside the return-this parts of conditional operators. For that, use if-then-else statements instead. Your code will be more readable, more understandable, and more maintainable.
So…
my $test_result = <some test that returns a Boolean value>; print $test_result ? qw( c ) : qw( a b );
…or, better…
my $test_result = <some test that returns a Boolean value>; my @values = $test_result ? qw( c ) : qw( a b ); print @values;
In reply to Re: Ternary Operator with Subroutine
by Jim
in thread Ternary Operator with Subroutine
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |