my @working = (1 .. 10); # globally scoped anonymous coderef my $push = sub { push @working, shift }; my $pop = sub { pop @working }; while(1) { my $input = input(); check_input('type', $input); } ... # sub check_input if ($input eq 'p') { $pop->(); }