#!/usr/bin/perl use feature qw/say state switch/; use strict; use warnings; sub gen_cashier { my ($start) = @_; state $cash_in_store = $start // 0; return sub { my ($method, $amount) = @_; given ($method) { when (undef) { warn "Method name required\n" } when ('add') { $cash_in_store += $amount } when ('del') { $cash_in_store -= $amount } when ('bal') { return $cash_in_store } when (/^(?[a-zA-Z]+)$/) { warn "$+{name} unimplemented\n" } default { warn "$method is illegal method name\n" } } }; } my @drawer = map gen_cashier, 1..4; $drawer[0]->(add => 59); $drawer[1]->(del => 17); $drawer[2]->(steal => 20); say $drawer[3]->('bal');