Loop in S_init_perllib(), only calling S_incpush*() with INCPUSH_ADD_OLD_VERS
[p5sagit/p5-mst-13.2.git] / t / op / state.t
index 7be1666..953cd26 100644 (file)
@@ -10,7 +10,7 @@ BEGIN {
 use strict;
 use feature ":5.10";
 
-plan tests => 108;
+plan tests => 130;
 
 ok( ! defined state $uninit, q(state vars are undef by default) );
 
@@ -301,17 +301,6 @@ foreach my $x (0 .. 4) {
 
 
 #
-# 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];
@@ -331,3 +320,99 @@ foreach my $spam (@spam) {
     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";
+}
+
+# [perl #49522] state variable not available
+
+{
+    my @warnings;
+    local $SIG{__WARN__} = sub { push @warnings, $_[0] };
+
+    eval q{
+       use warnings;
+
+       sub f_49522 {
+           state $s = 88;
+           sub g_49522 { $s }
+           sub { $s };
+       }
+
+       sub h_49522 {
+           state $t = 99;
+           sub i_49522 {
+               sub { $t };
+           }
+       }
+    };
+    is $@, '', "eval f_49522";
+    # shouldn't be any 'not available' or 'not stay shared' warnings
+    ok !@warnings, "suppress warnings part 1 [@warnings]";
+
+    @warnings = ();
+    my $f = f_49522();
+    is $f->(), 88, "state var closure 1";
+    is g_49522(), 88, "state var closure 2";
+    ok !@warnings, "suppress warnings part 2 [@warnings]";
+
+
+    @warnings = ();
+    $f = i_49522();
+    h_49522(); # initialise $t
+    is $f->(), 99, "state var closure 3";
+    ok !@warnings, "suppress warnings part 3 [@warnings]";
+
+
+}
+
+
+__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) = ();