1 # Demo of the state() variable declaration.
2 # This is also a neat way of doing OO without actually having OO available.
4 # Please remember to update t/examples/examples.t and rename
5 # examples/output/cashiers if you rename/move this file.
10 # This variable corresponds to a class variable.
11 # It is shared across all "instances" of gen_cashier().
12 state $cash_in_store = 0;
14 # One could add my() variables here, which correspond to instance variables.
15 # These would not be shared.
17 # Finally, we return a hashref which maps method names to code.
19 add
=> { $cash_in_store += $^amount
},
20 del
=> { $cash_in_store -= $^amount
},
21 bal
=> { $cash_in_store },
26 @drawer[$_] = gen_cashier
() for 1..3;
28 @drawer[1]<add
>( 59 );
29 @drawer[2]<del
>( 17 );
30 say @drawer[3]<bal
>(); # This should say "42"