This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
allow Configure -S to run non-interactively (spotted by Greg Hudson
[perl5.git] / t / op / closure.t
index 7af3abb..c691d6f 100755 (executable)
@@ -7,12 +7,12 @@
 
 BEGIN {
     chdir 't' if -d 't';
-    @INC = '../lib';
+    unshift @INC, '../lib';
 }
 
 use Config;
 
-print "1..167\n";
+print "1..171\n";
 
 my $test = 1;
 sub test (&) {
@@ -130,6 +130,58 @@ test {
   &{$foo[4]}() == 0
   };
 
+# test if closures get created in optimized for loops
+
+my %foo;
+for my $n ('A'..'E') {
+    $foo{$n} = sub { $n eq $_[0] };
+}
+
+test {
+  &{$foo{A}}('A') and
+  &{$foo{B}}('B') and
+  &{$foo{C}}('C') and
+  &{$foo{D}}('D') and
+  &{$foo{E}}('E')
+};
+
+for my $n (0..4) {
+    $foo[$n] = sub { $n == $_[0] };
+}
+
+test {
+  &{$foo[0]}(0) and
+  &{$foo[1]}(1) and
+  &{$foo[2]}(2) and
+  &{$foo[3]}(3) and
+  &{$foo[4]}(4)
+};
+
+for my $n (0..4) {
+    $foo[$n] = sub {
+                     # no intervening reference to $n here
+                     sub { $n == $_[0] }
+                  };
+}
+
+test {
+  $foo[0]->()->(0) and
+  $foo[1]->()->(1) and
+  $foo[2]->()->(2) and
+  $foo[3]->()->(3) and
+  $foo[4]->()->(4)
+};
+
+{
+    my $w;
+    $w = sub {
+       my ($i) = @_;
+       test { $i == 10 };
+       sub { $w };
+    };
+    $w->(10);
+}
+
 # Additional tests by Tom Phoenix <rootbeer@teleport.com>.
 
 {
@@ -377,7 +429,7 @@ END
            $test++;
          }
 
-         if ($Config{d_fork} and $^O ne 'VMS') {
+         if ($Config{d_fork} and $^O ne 'VMS' and $^O ne 'MSWin32') {
            # Fork off a new perl to run the tests.
            # (This is so we can catch spurious warnings.)
            $| = 1; print ""; $| = 0; # flush output before forking
@@ -411,9 +463,11 @@ END
            my $errfile = "terr$$";  $errfile++ while -e $errfile;
            my @tmpfiles = ($cmdfile, $errfile);
            open CMD, ">$cmdfile"; print CMD $code; close CMD;
-           my $cmd = ($^O eq 'VMS') ? "MCR $^X" : "./perl";
+           my $cmd = (($^O eq 'VMS') ? "MCR $^X"
+                      : ($^O eq 'MSWin32') ? '.\perl'
+                      : './perl');
            $cmd .= " -w $cmdfile 2>$errfile";
-           if ($^O eq 'VMS') {
+           if ($^O eq 'VMS' or $^O eq 'MSWin32') {
              # Use pipe instead of system so we don't inherit STD* from
              # this process, and then foul our pipe back to parent by
              # redirecting output in the child.
@@ -450,3 +504,4 @@ END
     }  # End of foreach $inner_type
 
 }
+