4 # Original written by Ulrich Pfeifer on 2 Jan 1997.
5 # Greatly extended by Tom Phoenix <rootbeer@teleport.com> on 28 Jan 1997.
19 print ((&{$_[0]})?"ok $test\n":"not ok $test\n");
24 sub foo { $i = shift if @_; $i }
31 # closure: lexical outside sub
32 my $foo = sub {$i = shift if @_; $i };
33 my $bar = sub {$i = shift if @_; $i };
37 # did the lexical change?
38 test { foo == 3 and $i == 3};
39 # did the second closure notice?
42 # closure: lexical inside sub
45 sub { $i = shift if @_; $i }
60 sub {$i = shift if @_; $i };
63 sub {$i = shift if @_; $i };
75 test {&$foo(11)-1 == &$bar()};
80 $foo[$_] = sub {$i = shift if @_; $i };
105 for (qw(0 1 2 3 4)) {
107 $foo[$_] = sub {$i = shift if @_; $i };
114 &{$foo[0]}() == 0 and
115 &{$foo[1]}() == 1 and
116 &{$foo[2]}() == 2 and
117 &{$foo[3]}() == 3 and
126 &{$foo[0]}() == 4 and
127 &{$foo[1]}() == 3 and
128 &{$foo[2]}() == 2 and
129 &{$foo[3]}() == 1 and
133 # test if closures get created in optimized for loops
136 for my $n ('A'..'E') {
137 $foo{$n} = sub { $n eq $_[0] };
149 $foo[$n] = sub { $n == $_[0] };
162 # no intervening reference to $n here
185 # Additional tests by Tom Phoenix <rootbeer@teleport.com>.
191 my($debugging, %expected, $inner_type, $where_declared, $within);
192 my($nc_attempt, $call_outer, $call_inner, $undef_outer);
193 my($code, $inner_sub_test, $expected, $line, $errors, $output);
194 my(@inners, $sub_test, $pid);
195 $debugging = 1 if defined($ARGV[0]) and $ARGV[0] eq '-debug';
197 # The expected values for these tests
199 'global_scalar' => 1001,
200 'global_array' => 2101,
201 'global_hash' => 3004,
205 'sub_scalar' => 7001,
211 # Our innermost sub is either named or anonymous
212 for $inner_type (qw!named anon!) {
213 # And it may be declared at filescope, within a named
214 # sub, or within an anon sub
215 for $where_declared (qw!filescope in_named in_anon!) {
216 # And that, in turn, may be within a foreach loop,
217 # a naked block, or another named sub
218 for $within (qw!foreach naked other_sub!) {
220 # Here are a number of variables which show what's
221 # going on, in a way.
222 $nc_attempt = 0+ # Named closure attempted
223 ( ($inner_type eq 'named') ||
224 ($within eq 'other_sub') ) ;
225 $call_inner = 0+ # Need to call &inner
226 ( ($inner_type eq 'anon') &&
227 ($within eq 'other_sub') ) ;
228 $call_outer = 0+ # Need to call &outer or &$outer
229 ( ($inner_type eq 'anon') &&
230 ($within ne 'other_sub') ) ;
231 $undef_outer = 0+ # $outer is created but unused
232 ( ($where_declared eq 'in_anon') &&
233 (not $call_outer) ) ;
235 $code = "# This is a test script built by t/op/closure.t\n\n";
237 $code .= <<"DEBUG_INFO" if $debugging;
238 # inner_type: $inner_type
239 # where_declared: $where_declared
241 # nc_attempt: $nc_attempt
242 # call_inner: $call_inner
243 # call_outer: $call_outer
244 # undef_outer: $undef_outer
247 $code .= <<"END_MARK_ONE";
249 BEGIN { \$SIG{__WARN__} = sub {
253 $code .= <<"END_MARK_TWO" if $nc_attempt;
254 return if index(\$msg, 'will not stay shared') != -1;
255 return if index(\$msg, 'may be unavailable') != -1;
258 $code .= <<"END_MARK_THREE"; # Backwhack a lot!
259 print "not ok: got unexpected warning \$msg\\n";
265 my \$result = &{\$_[0]};
266 print "not " unless \$result;
267 print "ok \$test\\n";
272 # some of the variables which the closure will access
273 \$global_scalar = 1000;
274 \@global_array = (2000, 2100, 2200, 2300);
275 %global_hash = 3000..3009;
277 my \$fs_scalar = 4000;
278 my \@fs_array = (5000, 5100, 5200, 5300);
279 my %fs_hash = 6000..6009;
283 if ($where_declared eq 'filescope') {
285 } elsif ($where_declared eq 'in_named') {
288 my $sub_scalar = 7000;
289 my @sub_array = (8000, 8100, 8200, 8300);
290 my %sub_hash = 9000..9009;
293 } elsif ($where_declared eq 'in_anon') {
296 my $sub_scalar = 7000;
297 my @sub_array = (8000, 8100, 8200, 8300);
298 my %sub_hash = 9000..9009;
302 die "What was $where_declared?"
305 if ($within eq 'foreach') {
307 my \$foreach = 12000;
308 my \@list = (10000, 10010);
309 foreach \$foreach (\@list) {
311 } elsif ($within eq 'naked') {
312 $code .= " { # naked block\n" # }
313 } elsif ($within eq 'other_sub') {
314 $code .= " sub inner_sub {\n" # }
316 die "What was $within?"
320 @inners = ( qw!global_scalar global_array global_hash! ,
321 qw!fs_scalar fs_array fs_hash! );
322 push @inners, 'foreach' if $within eq 'foreach';
323 if ($where_declared ne 'filescope') {
324 push @inners, qw!sub_scalar sub_array sub_hash!;
326 for $inner_sub_test (@inners) {
328 if ($inner_type eq 'named') {
329 $code .= " sub named_$sub_test "
330 } elsif ($inner_type eq 'anon') {
331 $code .= " \$anon_$sub_test = sub "
333 die "What was $inner_type?"
336 # Now to write the body of the test sub
337 if ($inner_sub_test eq 'global_scalar') {
338 $code .= '{ ++$global_scalar }'
339 } elsif ($inner_sub_test eq 'fs_scalar') {
340 $code .= '{ ++$fs_scalar }'
341 } elsif ($inner_sub_test eq 'sub_scalar') {
342 $code .= '{ ++$sub_scalar }'
343 } elsif ($inner_sub_test eq 'global_array') {
344 $code .= '{ ++$global_array[1] }'
345 } elsif ($inner_sub_test eq 'fs_array') {
346 $code .= '{ ++$fs_array[1] }'
347 } elsif ($inner_sub_test eq 'sub_array') {
348 $code .= '{ ++$sub_array[1] }'
349 } elsif ($inner_sub_test eq 'global_hash') {
350 $code .= '{ ++$global_hash{3002} }'
351 } elsif ($inner_sub_test eq 'fs_hash') {
352 $code .= '{ ++$fs_hash{6002} }'
353 } elsif ($inner_sub_test eq 'sub_hash') {
354 $code .= '{ ++$sub_hash{9002} }'
355 } elsif ($inner_sub_test eq 'foreach') {
356 $code .= '{ ++$foreach }'
358 die "What was $inner_sub_test?"
362 if ($inner_type eq 'anon') {
366 $sub_test++; # sub name sequence number
368 } # End of foreach $inner_sub_test
370 # Close up $within block # {
373 # Close up $where_declared block
374 if ($where_declared eq 'in_named') { # {
376 } elsif ($where_declared eq 'in_anon') { # {
380 # We may need to do something with the sub we just made...
381 $code .= "undef \$outer;\n" if $undef_outer;
382 $code .= "&inner_sub;\n" if $call_inner;
384 if ($where_declared eq 'in_named') {
385 $code .= "&outer;\n\n";
386 } elsif ($where_declared eq 'in_anon') {
387 $code .= "&\$outer;\n\n"
391 # Now, we can actually prep to run the tests.
392 for $inner_sub_test (@inners) {
393 $expected = $expected{$inner_sub_test} or
394 die "expected $inner_sub_test missing";
396 # Named closures won't access the expected vars
398 substr($inner_sub_test, 0, 4) eq "sub_" ) {
402 # If you make a sub within a foreach loop,
403 # what happens if it tries to access the
404 # foreach index variable? If it's a named
405 # sub, it gets the var from "outside" the loop,
406 # but if it's anon, it gets the value to which
407 # the index variable is aliased.
409 # Of course, if the value was set only
410 # within another sub which was never called,
411 # the value has not been set yet.
413 if ($inner_sub_test eq 'foreach') {
414 if ($inner_type eq 'named') {
415 if ($call_outer || ($where_declared eq 'filescope')) {
424 if ($inner_type eq 'anon') {
425 $code .= "test { &\$anon_$test == $expected };\n"
427 $code .= "test { &named_$test == $expected };\n"
432 if ($Config{d_fork} and $^O ne 'VMS' and $^O ne 'MSWin32' and $^O ne 'NetWare') {
433 # Fork off a new perl to run the tests.
434 # (This is so we can catch spurious warnings.)
435 $| = 1; print ""; $| = 0; # flush output before forking
436 pipe READ, WRITE or die "Can't make pipe: $!";
437 pipe READ2, WRITE2 or die "Can't make second pipe: $!";
438 die "Can't fork: $!" unless defined($pid = open PERL, "|-");
440 # Child process here. We're going to send errors back
441 # through the extra pipe.
444 open STDOUT, ">&WRITE" or die "Can't redirect STDOUT: $!";
445 open STDERR, ">&WRITE2" or die "Can't redirect STDERR: $!";
446 exec './perl', '-w', '-'
447 or die "Can't exec ./perl: $!";
449 # Parent process here.
455 $output = join '', <READ>;
456 $errors = join '', <READ2>; }
461 # No fork(). Do it the hard way.
462 my $cmdfile = "tcmd$$"; $cmdfile++ while -e $cmdfile;
463 my $errfile = "terr$$"; $errfile++ while -e $errfile;
464 my @tmpfiles = ($cmdfile, $errfile);
465 open CMD, ">$cmdfile"; print CMD $code; close CMD;
466 my $cmd = (($^O eq 'VMS') ? "MCR $^X"
467 : ($^O eq 'MSWin32') ? '.\perl'
468 : ($^O eq 'MacOS') ? $^X
469 : ($^O eq 'NetWare') ? 'perl'
471 $cmd .= " -w $cmdfile 2>$errfile";
472 if ($^O eq 'VMS' or $^O eq 'MSWin32' or $^O eq 'NetWare') {
473 # Use pipe instead of system so we don't inherit STD* from
474 # this process, and then foul our pipe back to parent by
475 # redirecting output in the child.
476 open PERL,"$cmd |" or die "Can't open pipe: $!\n";
477 { local $/; $output = join '', <PERL> }
480 my $outfile = "tout$$"; $outfile++ while -e $outfile;
481 push @tmpfiles, $outfile;
482 system "$cmd >$outfile";
483 { local $/; open IN, $outfile; $output = <IN>; close IN }
486 printf "not ok: exited with error code %04X\n", $?;
487 $debugging or do { 1 while unlink @tmpfiles };
490 { local $/; open IN, $errfile; $errors = <IN>; close IN }
491 1 while unlink @tmpfiles;
494 print STDERR $errors;
495 if ($debugging && ($errors || $? || ($output =~ /not ok/))) {
497 for $line (split '\n', $code) {
498 printf "%3d: %s\n", ++$lnum, $line;
501 printf "not ok: exited with error code %04X\n", $? if $?;
502 print "-" x 30, "\n" if $debugging;
504 } # End of foreach $within
505 } # End of foreach $where_declared
506 } # End of foreach $inner_type