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


in reply to Re: Adding 2 + 2 (stack machine)
in thread Adding 2 + 2

The parent assumed the accumulator was initially zero. That's not likely in a real-life scenario, so this removes the assumption:
my @stack; my $acc = rand; sub PUSH() { push(@stack, $acc); } sub POP() { pop(@stack); } sub INC() { $acc++; } sub ADD() { $acc += POP; } sub SUB() { $acc -= POP; } PUSH; SUB; INC; INC; PUSH; ADD; print("$acc\n");