2 # tests state variables
14 # Before loading feature.pm, test it with CORE::
15 ok eval 'CORE::state $x = 1;', 'CORE::state outside of feature.pm scope';
21 ok( ! defined state $uninit, q(state vars are undef by default) );
30 return ($x++, $y++, $z++, $t++);
33 my ($x, $y, $z, $t) = stateful();
34 is( $x, 0, 'uninitialized state var' );
35 is( $y, 1, 'initialized state var' );
36 is( $z, 2, 'lexical' );
37 is( $t, 3, 'initialized state var, list syntax' );
39 ($x, $y, $z, $t) = stateful();
40 is( $x, 1, 'incremented state var' );
41 is( $y, 2, 'incremented state var' );
42 is( $z, 2, 'reinitialized lexical' );
43 is( $t, 4, 'incremented state var, list syntax' );
45 ($x, $y, $z, $t) = stateful();
46 is( $x, 2, 'incremented state var' );
47 is( $y, 3, 'incremented state var' );
48 is( $z, 2, 'reinitialized lexical' );
49 is( $t, 5, 'incremented state var, list syntax' );
56 { state $bar = 12; $t = ++$bar }
62 is( $x, 11, 'outer state var' );
63 is( $y, 13, 'inner state var' );
66 is( $x, 12, 'outer state var' );
67 is( $y, 14, 'inner state var' );
73 # we use $outer to generate a closure
74 sub { ++$outer; ++state $x }
78 is( $f1->(), 1, 'generator 1' );
79 is( $f1->(), 2, 'generator 1' );
81 is( $f2->(), 1, 'generator 2' );
82 is( $f1->(), 3, 'generator 1 again' );
83 is( $f2->(), 2, 'generator 2 once more' );
89 sub TIESCALAR {bless {}};
90 sub FETCH { ++$fetchcount; 18 };
91 tie my $y, "countfetches";
92 sub foo { state $x = $y; $x++ }
93 ::is( foo(), 18, "initialisation with tied variable" );
94 ::is( foo(), 19, "increments correctly" );
95 ::is( foo(), 20, "increments correctly, twice" );
96 ::is( $fetchcount, 1, "fetch only called once" );
99 # state variables are shared among closures
103 state $cash_in_store = 0;
105 add => sub { $cash_in_store += $amount },
106 del => sub { $cash_in_store -= $amount },
107 bal => sub { $cash_in_store },
111 gen_cashier(59)->{add}->();
112 gen_cashier(17)->{del}->();
113 is( gen_cashier()->{bal}->(), 42, '$42 in my drawer' );
115 # stateless assignment to a state variable
118 state $reinitme = 42;
121 is( stateless(), 43, 'stateless function, first time' );
122 is( stateless(), 44, 'stateless function, second time' );
132 my $xsize = stateful_array();
133 is( $xsize, 0, 'uninitialized state array' );
135 $xsize = stateful_array();
136 is( $xsize, 1, 'uninitialized state array after one iteration' );
138 sub stateful_init_array {
139 state @x = qw(a b c);
141 return join(",", @x);
144 is stateful_init_array(), "a,b,c,x";
145 is stateful_init_array(), "a,b,c,x,x";
146 is stateful_init_array(), "a,b,c,x,x,x";
155 my $xhval = stateful_hash();
156 is( $xhval, 0, 'uninitialized state hash' );
158 $xhval = stateful_hash();
159 is( $xhval, 1, 'uninitialized state hash after one iteration' );
161 sub stateful_init_hash {
162 state %x = qw(a b c d);
164 return join(",", map { ($_, $x{$_}) } sort keys %x);
167 is stateful_init_hash(), "a,b,c,d,foo,1";
168 is stateful_init_hash(), "a,b,c,d,foo,2";
169 is stateful_init_hash(), "a,b,c,d,foo,3";
171 # declarations with attributes
174 skip "no attributes in miniperl", 3, if is_miniperl;
179 state $b :shared = 3;
181 state @d :shared = qw(a b c);
183 state %f :shared = qw(a b c d);
190 return join(",", $a, $b, join(":", @c), join(":", @d), join(":", %e),
191 join(":", map { ($_, $f{$_}) } sort keys %f));
195 is stateful_attr(), "1,4,x,a:b:c:x,e:1,a:b:c:d:e:1";
196 is stateful_attr(), "2,5,x:x,a:b:c:x:x,e:2,a:b:c:d:e:2";
197 is stateful_attr(), "3,6,x:x:x,a:b:c:x:x:x,e:3,a:b:c:d:e:3";
205 state $recursed_state = 123;
206 is($recursed_state, 123, "state kept through recursion ($level)");
207 noseworth($level - 1) if $level;
211 # Assignment return value
213 sub pugnax { my $x = state $y = 42; $y++; $x; }
215 is( pugnax(), 42, 'scalar state assignment return value' );
216 is( pugnax(), 43, 'scalar state assignment return value' );
220 # Test various blocks.
222 foreach my $x (1 .. 3) {
224 is ($y, 1, "foreach $x");
227 for (my $x = 1; $x < 4; $x ++) {
229 is ($y, 1, "for $x");
234 is ($y, 1, "while $x");
241 is ($y, 1, "until $x");
251 is ($z, $y, "bare block $y");
259 my @simpsons = qw [Homer Marge Bart Lisa Maggie];
261 my $next = shift @simpsons;
262 state $simpson = $next;
263 is $simpson, 'Homer', 'goto 1';
264 goto again if @simpsons;
268 goto Elvis unless $vi;
269 state $calvin = ++ $vi;
270 Elvis: state $vile = ++ $vi;
271 redo unless defined $calvin;
272 is $calvin, 2, "goto 2";
273 is $vile, 1, "goto 3";
276 my @presidents = qw [Taylor Garfield Ford Arthur Monroe];
278 my $next = shift @presidents;
279 state $president = $next;
280 goto &president if @presidents;
283 my $president_answer = $presidents [0];
284 is president, $president_answer, '&goto';
286 my @flowers = qw [Bluebonnet Goldenrod Hawthorn Peony];
287 foreach my $f (@flowers) {
288 goto state $flower = $f;
289 ok 0, 'computed goto 0'; next;
290 Bluebonnet: ok 1, 'computed goto 1'; next;
291 Goldenrod: ok 0, 'computed goto 2'; next;
292 Hawthorn: ok 0, 'computed goto 3'; next;
293 Peony: ok 0, 'computed goto 4'; next;
294 ok 0, 'computed goto 5'; next;
300 my @apollo = qw [Eagle Antares Odyssey Aquarius];
301 my @result1 = map {state $x = $_;} @apollo;
302 my @result2 = grep {state $x = /Eagle/} @apollo;
305 is "@result1", $apollo [0] x @apollo, "map";
306 is "@result2", "@apollo", "grep";
310 # Reference to state variable.
312 sub reference {\state $x}
313 my $ref1 = reference;
314 my $ref2 = reference;
315 is $ref1, $ref2, "Reference to state variable";
318 # Pre/post increment.
320 foreach my $x (1 .. 3) {
323 is $y, $x, "state pre increment";
324 is $z, $x, "state post increment";
331 my $tintin = "Tin-Tin";
332 my @thunderbirds = qw [Scott Virgel Alan Gordon John];
333 my @thunderbirds2 = qw [xcott xxott xxxtt xxxxt xxxxx];
334 foreach my $x (0 .. 4) {
335 state $c = \substr $tintin, $x, 1;
336 my $d = \substr ((state $tb = $thunderbirds [$x]), $x, 1);
339 is $tintin, "xin-Tin", "substr";
340 is $tb, $thunderbirds2 [$x], "substr";
347 my @spam = qw [spam ham bacon beans];
348 foreach my $spam (@spam) {
349 no warnings 'experimental::smartmatch';
350 given (state $spam = $spam) {
351 when ($spam [0]) {ok 1, "given"}
352 default {ok 0, "given"}
363 is $x, "two", "masked"
366 # normally closureless anon subs share a CV and pad. If the anon sub has a
367 # state var, this would mean that it is shared. Check that this doesn't
372 push @f, sub { state $x; ++$x } for 1..2;
378 # each copy of an anon sub should get its own 'once block'
381 my $x; # used to force a closure
383 push @f, sub { $x=0; state $s = $_[0]; $s } for 1..2;
393 foreach my $forbidden (<DATA>) {
395 skip_if_miniperl("miniperl can't load attributes.pm", 1)
396 if $forbidden =~ /:shared/;
402 qr/Initialization of state variables in list currently forbidden/,
403 "Currently forbidden: $forbidden";
407 # [perl #49522] state variable not available
411 local $SIG{__WARN__} = sub { push @warnings, $_[0] };
429 is $@, '', "eval f_49522";
430 # shouldn't be any 'not available' or 'not stay shared' warnings
431 ok !@warnings, "suppress warnings part 1 [@warnings]";
435 is $f->(), 88, "state var closure 1";
436 is g_49522(), 88, "state var closure 2";
437 ok !@warnings, "suppress warnings part 2 [@warnings]";
442 h_49522(); # initialise $t
443 is $f->(), 99, "state var closure 3";
444 ok !@warnings, "suppress warnings part 3 [@warnings]";
450 # [perl #117095] state var initialisation getting skipped
451 # the 'if 0' code below causes a call to op_free at compile-time,
452 # which used to inadvertently mark the state var as initialised.
456 foo($f) if 0; # this calls op_free on padmy($f)
457 ok(defined $f, 'state init not skipped');
460 # [perl #121134] Make sure padrange doesn't mess with these
467 is($z, $expect, "State variable is correct");
481 is($z, $expect, "State variable is correct");
490 # [perl #123029] regression in "state" under PERL_NO_COW
497 ok(rt_123029(), "state variables don't surprisingly disappear when accessed");
499 # make sure multiconcat doesn't break state
503 is($s, "-1-", "state with multiconcat pass $_");
509 (state @a :shared) = 1;
511 (state %a :shared) = ();
516 state (@a) :shared = 1;
517 (state (@a) :shared) = 1;
520 state (%a) :shared = ();
521 (state (%a) :shared) = ();
522 state (undef, $a) = ();
523 (state (undef, $a)) = ();
524 state (undef, @a) = ();
525 (state (undef, @a)) = ();
526 state ($a, undef) = ();
527 (state ($a, undef)) = ();
529 (state ($a, $b)) = ();
530 state ($a, $b) :shared = ();
531 (state ($a, $b) :shared) = ();
533 (state ($a, @b)) = ();
534 state ($a, @b) :shared = ();
535 (state ($a, @b) :shared) = ();
536 state (@a, undef) = ();
537 (state (@a, undef)) = ();
539 (state (@a, $b)) = ();
540 state (@a, $b) :shared = ();
541 (state (@a, $b) :shared) = ();
543 (state (@a, @b)) = ();
544 state (@a, @b) :shared = ();
545 (state (@a, @b) :shared) = ();
546 (state $a, state $b) = ();
548 (state $a, my $b) = ();
549 (state $a, state @b) = ();
550 (state $a, local @b) = ();
551 (state $a, undef, state $b) = ();
552 state ($a, undef, $b) = ();