2 # tests state variables
15 ok( ! defined state $uninit, q(state vars are undef by default) );
24 return ($x++, $y++, $z++, $t++);
27 my ($x, $y, $z, $t) = stateful();
28 is( $x, 0, 'uninitialized state var' );
29 is( $y, 1, 'initialized state var' );
30 is( $z, 2, 'lexical' );
31 is( $t, 3, 'initialized state var, list syntax' );
33 ($x, $y, $z, $t) = stateful();
34 is( $x, 1, 'incremented state var' );
35 is( $y, 2, 'incremented state var' );
36 is( $z, 2, 'reinitialized lexical' );
37 is( $t, 4, 'incremented state var, list syntax' );
39 ($x, $y, $z, $t) = stateful();
40 is( $x, 2, 'incremented state var' );
41 is( $y, 3, 'incremented state var' );
42 is( $z, 2, 'reinitialized lexical' );
43 is( $t, 5, 'incremented state var, list syntax' );
50 { state $bar = 12; $t = ++$bar }
56 is( $x, 11, 'outer state var' );
57 is( $y, 13, 'inner state var' );
60 is( $x, 12, 'outer state var' );
61 is( $y, 14, 'inner state var' );
67 # we use $outer to generate a closure
68 sub { ++$outer; ++state $x }
72 is( $f1->(), 1, 'generator 1' );
73 is( $f1->(), 2, 'generator 1' );
75 is( $f2->(), 1, 'generator 2' );
76 is( $f1->(), 3, 'generator 1 again' );
77 is( $f2->(), 2, 'generator 2 once more' );
83 sub TIESCALAR {bless {}};
84 sub FETCH { ++$fetchcount; 18 };
85 tie my $y, "countfetches";
86 sub foo { state $x = $y; $x++ }
87 ::is( foo(), 18, "initialisation with tied variable" );
88 ::is( foo(), 19, "increments correctly" );
89 ::is( foo(), 20, "increments correctly, twice" );
90 ::is( $fetchcount, 1, "fetch only called once" );
93 # state variables are shared among closures
97 state $cash_in_store = 0;
99 add => sub { $cash_in_store += $amount },
100 del => sub { $cash_in_store -= $amount },
101 bal => sub { $cash_in_store },
105 gen_cashier(59)->{add}->();
106 gen_cashier(17)->{del}->();
107 is( gen_cashier()->{bal}->(), 42, '$42 in my drawer' );
109 # stateless assignment to a state variable
113 (state $reinitme, my $foo) = (42, 'bar');
116 is( stateless(), 43, 'stateless function, first time' );
117 is( stateless(), 43, 'stateless function, second time' );
127 my $xsize = stateful_array();
128 is( $xsize, 0, 'uninitialized state array' );
130 $xsize = stateful_array();
131 is( $xsize, 1, 'uninitialized state array after one iteration' );
133 sub stateful_array_init {
139 $xsize = stateful_array_init();
140 is( $xsize, 2, 'initialized state array' );
142 $xsize = stateful_array_init();
143 is( $xsize, 3, 'initialized state array after one iteration' );
152 my $xhval = stateful_hash();
153 is( $xhval, 0, 'uninitialized state hash' );
155 $xhval = stateful_hash();
156 is( $xhval, 1, 'uninitialized state hash after one iteration' );
158 sub stateful_hash_init {
159 state %hx = (foo => 10);
163 $xhval = stateful_hash_init();
164 is( $xhval, 10, 'initialized state hash' );
166 $xhval = stateful_hash_init();
167 is( $xhval, 11, 'initialized state hash after one iteration' );
169 # state declaration with a list
172 # note that this should be a state assignment, while (state $lager, state $stout) shouldn't
173 state($lager, $stout) = (11, 22);
179 my $ls = statelist();
180 is($ls, "12/23", 'list assignment to state scalars');
182 is($ls, "13/24", 'list assignment to state scalars');
185 state($sherry, $bourbon) = (1 .. 2);
192 is($ls, "2/3", 'list assignment to state scalars');
194 is($ls, "3/4", 'list assignment to state scalars');
200 state $recursed_state = 123;
201 is($recursed_state, 123, "state kept through recursion ($level)");
202 noseworth($level - 1) if $level;
206 # Assignment return value
208 sub pugnax { my $x = state $y = 42; $y++; $x; }
210 is( pugnax(), 42, 'scalar state assignment return value' );
211 is( pugnax(), 43, 'scalar state assignment return value' );