[t/spec] A few wilder tests for nesting.
[pugs.git] / examples / cashiers.pl
blob115a1afa2ccfd4b8466374fbf316264e06a516f1
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.
7 use v6;
9 sub gen_cashier () {
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.
18 return {
19 add => { $cash_in_store += $^amount },
20 del => { $cash_in_store -= $^amount },
21 bal => { $cash_in_store },
22 };
25 my @drawer;
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"