in reply to question 1st - undefined behaviour

If you run your code under a perl built with -DDEBUGGING, which can show each instruction being executed and what's on the stack after each execution, things may become clearer.

Here's the output of perl -Dst -e'my $x=5; $x=$x + ++$x + $x++;' with some annotations. I've skipped the first assign statement.

The padsv op pushes $x (not a copy) onto the perl stack; $x is currently an integer value (IV) with value 5: (-e:1) padsv($x) => IV(5) $x is pushed onto the stack again: (-e:1) padsv($x) => IV(5) IV(5) preinc increments the variable $x; note that both values on the stack have now incremented (because they are both $x, not copies) (-e:1) preinc => IV(6) IV(6) the add op takes the top two values on the stack and replaces them with a new value: (-e:1) add => IV(12) push $x again. Since it was preinc()ed earlier, it now has the value 6: (-e:1) padsv($x) => IV(12) IV(6) postinc takes the variable $x off the stack, makes a copy and pushes that copy back on the stack, then increments $x. If we had any other direct $x's on the stack (which we don't), their values would appear incremented: (-e:1) postinc => IV(12) IV(6) the final add does the obvious thing. (-e:1) add => IV(18)
Note that this is what currently *happens* to happen. Code should not rely on this behaviour.

Dave.