This has nothing to do with Moose. In
$x + 32 - 8
the evaluation order is equivalent to
($x + 32) - 8
(i.e. +
and -
have same precedence level and are left-associative). As a tree:
(-)
/ \
(+) 8
/ \
$x 32
No part of that syntax tree has only constant nodes: $x + 32
isn't constant, and PREVIOUS_PART - 8
isn't constant either. Therefore, the constant folding (which only operates at this tree level, and can't reorder parts of the tree) doesn't see any chances for optimizations.
You do get the optimization of you reorder to 32 - 8 + $x
.
The perlguts document constant folding, and specifically state that it operates by substituting parts of the tree.