http://www.perlmonks.org?node_id=1008855


in reply to Re: $1 not "freezing" in an addition
in thread $1 not "freezing" in an addition

Here's another fun example...

use 5.010; my $z = 0; sub xxx () { $z = 1; return 2; } sub yyy () { my $zz = $z; $z = 0; return 2 + $zz; } if (xxx + yyy == yyy + xxx) { say "Addition is commutative"; } else { say "The world has gone haywire!"; }

See addition; commutativity; etc.

Don't write functions with side-effects. And if you really must write functions with side-effects, be careful using them in expressions.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'