use strict;
use feature ":5.10";
-plan tests => 108;
+plan tests => 123;
ok( ! defined state $uninit, q(state vars are undef by default) );
#
-# List context reassigns, but scalar doesn't.
-#
-my @swords = qw [Stormbringer Szczerbiec Grimtooth Corrougue];
-foreach my $sword (@swords) {
- state ($s1) = state $s2 = $sword;
- is $s1, $swords [0], 'mixed context';
- is $s2, $swords [0], 'mixed context';
-}
-
-
-#
# Use with given.
#
my @spam = qw [spam ham bacon beans];
state $x = "two";
is $x, "two", "masked"
}
+
+# normally closureless anon subs share a CV and pad. If the anon sub has a
+# state var, this would mean that it is shared. Check that this doesn't
+# happen
+
+{
+ my @f;
+ push @f, sub { state $x; ++$x } for 1..2;
+ $f[0]->() for 1..10;
+ is $f[0]->(), 11;
+ is $f[1]->(), 1;
+}
+
+# each copy of an anon sub should get its own 'once block'
+
+{
+ my $x; # used to force a closure
+ my @f;
+ push @f, sub { $x=0; state $s = $_[0]; $s } for 1..2;
+ is $f[0]->(1), 1;
+ is $f[0]->(2), 1;
+ is $f[1]->(3), 3;
+ is $f[1]->(4), 3;
+}
+
+
+
+
+foreach my $forbidden (<DATA>) {
+ chomp $forbidden;
+ no strict 'vars';
+ eval $forbidden;
+ like $@, qr/Initialization of state variables in list context currently forbidden/, "Currently forbidden: $forbidden";
+}
+__DATA__
+state ($a) = 1;
+(state $a) = 1;
+state @a = 1;
+state (@a) = 1;
+(state @a) = 1;
+state %a = ();
+state (%a) = ();
+(state %a) = ();
+state ($a, $b) = ();
+state ($a, @b) = ();
+(state $a, state $b) = ();
+(state $a, $b) = ();
+(state $a, my $b) = ();
+(state $a, state @b) = ();
+(state $a, local @b) = ();
+(state $a, undef, state $b) = ();
+state ($a, undef, $b) = ();