root/examples/cashiers.pl

Revision 11293, 0.9 kB (checked in by Darren_Duncan, 2 years ago)

replaced all 'use v6;' lines with 'use v6-alpha;' in 330 files (examples/, ext/, t/, t_disabled/) ... more remain to do

  • Property svn:mime-type set to text/plain; charset=UTF-8
  • Property svn:eol-style set to native
Line 
1# Demo of the state() variable declaration.
2# This is also a neat way of doing OO without actually having OO available.
3#
4# Please remember to update t/examples/examples.t and rename
5# examples/output/cashiers if you rename/move this file.
6
7use v6-alpha;
8
9sub 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;
13
14    # One could add my() variables here, which correspond to instance variables.
15    # These would not be shared.
16
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    };
23}
24
25my $drawer;
26$drawer[$_] = gen_cashier() for 1..3;
27
28$drawer[1]<add>( 59 );
29$drawer[2]<del>( 17 );
30say $drawer[3]<bal>();  # This should say "42"
Note: See TracBrowser for help on using the browser.