in reply to Re: Why does the Perl conditional operator not do what I expect?
in thread Why does the Perl conditional operator not do what I expect?
say you wanted to doIt would be one way to do it with a ternary operator. Another way is shown below. Both are correct (i.e., compile without error). Which is 'advisable' depends on circumstances; I tend to prefer the ternary form below, but
$condition ? $a=2 : $b=3
would
($condition) ? ($a=2) : ($b=3)
then be the correct/advisible way to do it?
>perl -wMstrict -le "my $cond = shift; $a = $b = 99; ($cond ? $a : $b) = ($cond ? 2 : 3); print qq{a $a b $b}; " 0 a 99 b 3 >perl -wMstrict -le "my $cond = shift; $a = $b = 99; ($cond ? $a : $b) = ($cond ? 2 : 3); print qq{a $a b $b}; " 1 a 2 b 99
|
|---|