#!./perl -w
+# tests state variables
BEGIN {
chdir 't' if -d 't';
use strict;
use feature "state";
-plan tests => 23;
+plan tests => 26;
ok( ! defined state $uninit, q(state vars are undef by default) );
+# basic functionality
+
sub stateful {
state $x;
state $y = 1;
is( $y, 3, 'incremented state var' );
is( $z, 2, 'reinitialized lexical' );
+# in a nested block
+
sub nesting {
state $foo = 10;
my $t;
is( $x, 12, 'outer state var' );
is( $y, 14, 'inner state var' );
+# in a closure
+
sub generator {
my $outer;
# we use $outer to generate a closure
is( $f1->(), 3, 'generator 1 again' );
is( $f2->(), 2, 'generator 2 once more' );
+# with ties
{
package countfetches;
our $fetchcount = 0;
::is( foo(), 20, "increments correctly, twice" );
::is( $fetchcount, 1, "fetch only called once" );
}
+
+# state variables are shared among closures
+
+sub gen_cashier {
+ my $amount = shift;
+ state $cash_in_store = 0;
+ return {
+ add => sub { $cash_in_store += $amount },
+ del => sub { $cash_in_store -= $amount },
+ bal => sub { $cash_in_store },
+ };
+}
+
+gen_cashier(59)->{add}->();
+gen_cashier(17)->{del}->();
+is( gen_cashier()->{bal}->(), 42, '$42 in my drawer' );
+
+# stateless assignment to a state variable
+
+sub stateless {
+ (state $reinitme) = 42;
+ ++$reinitme;
+}
+is( stateless(), 43, 'stateless function, first time' );
+is( stateless(), 43, 'stateless function, second time' );