http://qs1969.pair.com?node_id=860102


in reply to Pre vs Post Incrementing variables

My guess: the entire right hand expression is evaluated before assignments are made. Reasonable except when you want something else, but in that case, what should the rule be instead?

Replies are listed 'Best First'.
Re^2: Pre vs Post Incrementing variables
by ikegami (Patriarch) on Sep 14, 2010 at 19:26 UTC

    My guess: the entire right hand expression is evaluated before assignments are made.

    Of course it does, but it doesn't explain anything. The question can be phrased as "Why is the output of the first snippet different from the output of the following snippets in the following code?"

    $ perl -E'$i=0; ($j, $k) = (++$i, ++$i); say "j:$j k:$k";' j:2 k:2 $ perl -E'$i=1; ($j, $k) = ($i++, $i++); say "j:$j k:$k";' j:1 k:2 $ perl -E'$i=0; ($j, $k) = (++$i+0, ++$i+0); say "j:$j k:$k";' j:1 k:2

    The answer is here.

Re^2: Pre vs Post Incrementing variables
by JavaFan (Canon) on Sep 14, 2010 at 15:58 UTC
    My guess: the entire right hand expression is evaluated before assignments are made.
    Good guess. Doesn't really answer the question though, as any assignment will evaluate the RHS before doing the assignment. (If it didn't, what would it assign? Purple monkeys?)

      Pink. Purple is on Thursdays.

      --MidLifeXis

        Except at night.

      Actually, it does. Evaluating that right hand increments $i twice. This occurs before either of the assignments.