\$m = 20; print ++\$m + \$m++; ##```## \$m = 20; print noop(++\$m) + \$m++; sub noop{ return shift } ##``````## # This is an attempt at emulating ++\$m with preInc(\$m) sub preInc { \$_[0] = \$_[0] + 1; # Increment input argument (the side effect), return \$_[0]; # and return the new, incremented value. } # And this is an attempt at emulating \$m++ with postInc(\$m) sub postInc { my \$temp = shift; # Remember original value, \$_[0] = \$_[0] + 1; # increment input argument (the side effect), return \$temp; # and return the old, un-incremented value. } my \$m = 20; print preInc(\$m) + \$postInc(\$m); # This prints 42. # The final value is \$m is 22. ##``````## my \$m=20; print ++\$m + \$m++; # This prints 43 ! # The final value is \$m is still 22. ##``````## # Print values and addresses of passed argument and \$m. sub look { print "look was passed '" . \$_[0] . "' at . \\$_[0] . ".\n"; print "while \\$m is '" . \$m . "' at " . \\$m . ".\n"; return \$_[0]; } my \$m = 20; my \$p = look(++\$m) + look(\$m++); print \$p; ##``````## sub noop { # do nothing return shift; } my \$m=20; print noop(++\$m) + \$m++; # This prints 42 ! ##``````## ==== increment weirdness: ++\$m + \$m++ ========== m = 20 at 0x80ab23c p = ++m + m++ *** inc 0x80ab23c : 20 --> 21 m is 21 at 0x80ab23c *** copy 0x80ab23c --> 0x804c120 m is 21 at 0x80ab23c *** inc 0x804c120 : 21 --> 22 m is 22 at 0x804c120 *** add : 22 at 0x804c120 + 21 at 0x80ab23c = 43 at 0x80ab11c m is 22 at 0x804c120 p = 43 at 0x80ab11c ```