Assignment inside Perl ternary conditional operator problems
问题 This snippet of Perl code in my program is giving the wrong result. $condition ? $a = 2 : $a = 3 ; print $a; No matter what the value of $condition is, the output is always 3, how come? 回答1: This is explained in the Perl documentation. Because of Perl operator precedence the statement is being parsed as ($condition ? $a= 2 : $a ) = 3 ; Because the ?: operator produces an assignable result, 3 is assigned to the result of the condition. When $condition is true this means ($a=2)=3 giving $a=3