Assignment inside Perl ternary conditional operator problems

前端 未结 5 1598
一生所求
一生所求 2021-02-05 00:57

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

5条回答
  •  伪装坚强ぢ
    2021-02-05 01:20

    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

    When $condition is false this means $a=3 giving $a=3

    The correct way to write this is

    $a = $condition ? 2 : 3;
    

    In general, you should really get out of the habit of using conditionals to do assignment, as in the original example -- it's the sort of thing that leads to Perl getting a reputation for being write-only.

    A good rule of thumb is that conditionals are only for simple values, never expressions with side effects. When you or someone else needs to read this code eight months from now, would you prefer it to read like this?

    $x < 3 ? foo($x) : bar($y);
    

    Or like this?

    if ($x < 3) {
      $foo($x);
    } else {
      $bar($y);
    }
    

提交回复
热议问题