in reply to ternary conditional help

Because of precedence

(1 ? $a = 2 : $b) = 'x';

You didn't read your "perldoc perlop" ...

Replies are listed 'Best First'.
Re^2: ternary conditional help
by opaltoot (Novice) on Nov 28, 2016 at 20:43 UTC
    ok thanks, but how is it that  ($a = 2) = 'x' makes $a = 'x' ?

      Hi opaltoot,

      See Assignment Operators:

      ... the scalar assignment operator produces a valid lvalue. Modifying an assignment is equivalent to doing the assignment and then modifying the variable that was assigned to. This is useful for modifying a copy of something, like this:

      ($tmp = $global) =~ tr/13579/24680/;

      ... Likewise,

      ($x += 2) *= 3;

      is equivalent to

      $x += 2; $x *= 3;

      Hope this helps,
      -- Hauke D

        thanks, yes that helps

        This is because the entire expression is evaluated before being passed as arguments to print. print doesn't get called iteratively for each argument. It gets its arguments, and then iterates.

        So that means $a = 2 is evaluated, then ' ', then $a = 3, and then the values of $a, ' ', and $a are passed to print. By the time the expression has been evaluated, $a contains 3 no matter how many times it appears in @_.


        Dave