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] };
160 # Additional tests by Tom Phoenix <rootbeer@teleport.com>.
166 my($debugging, %expected, $inner_type, $where_declared, $within);
167 my($nc_attempt, $call_outer, $call_inner, $undef_outer);
168 my($code, $inner_sub_test, $expected, $line, $errors, $output);
169 my(@inners, $sub_test, $pid);
170 $debugging = 1 if defined($ARGV[0]) and $ARGV[0] eq '-debug';
172 # The expected values for these tests
174 'global_scalar' => 1001,
175 'global_array' => 2101,
176 'global_hash' => 3004,
180 'sub_scalar' => 7001,
186 # Our innermost sub is either named or anonymous
187 for $inner_type (qw!named anon!) {
188 # And it may be declared at filescope, within a named
189 # sub, or within an anon sub
190 for $where_declared (qw!filescope in_named in_anon!) {
191 # And that, in turn, may be within a foreach loop,
192 # a naked block, or another named sub
193 for $within (qw!foreach naked other_sub!) {
195 # Here are a number of variables which show what's
196 # going on, in a way.
197 $nc_attempt = 0+ # Named closure attempted
198 ( ($inner_type eq 'named') ||
199 ($within eq 'other_sub') ) ;
200 $call_inner = 0+ # Need to call &inner
201 ( ($inner_type eq 'anon') &&
202 ($within eq 'other_sub') ) ;
203 $call_outer = 0+ # Need to call &outer or &$outer
204 ( ($inner_type eq 'anon') &&
205 ($within ne 'other_sub') ) ;
206 $undef_outer = 0+ # $outer is created but unused
207 ( ($where_declared eq 'in_anon') &&
208 (not $call_outer) ) ;
210 $code = "# This is a test script built by t/op/closure.t\n\n";
212 $code .= <<"DEBUG_INFO" if $debugging;
213 # inner_type: $inner_type
214 # where_declared: $where_declared
216 # nc_attempt: $nc_attempt
217 # call_inner: $call_inner
218 # call_outer: $call_outer
219 # undef_outer: $undef_outer
222 $code .= <<"END_MARK_ONE";
224 BEGIN { \$SIG{__WARN__} = sub {
228 $code .= <<"END_MARK_TWO" if $nc_attempt;
229 return if index(\$msg, 'will not stay shared') != -1;
230 return if index(\$msg, 'may be unavailable') != -1;
233 $code .= <<"END_MARK_THREE"; # Backwhack a lot!
234 print "not ok: got unexpected warning \$msg\\n";
240 my \$result = &{\$_[0]};
241 print "not " unless \$result;
242 print "ok \$test\\n";
247 # some of the variables which the closure will access
248 \$global_scalar = 1000;
249 \@global_array = (2000, 2100, 2200, 2300);
250 %global_hash = 3000..3009;
252 my \$fs_scalar = 4000;
253 my \@fs_array = (5000, 5100, 5200, 5300);
254 my %fs_hash = 6000..6009;
258 if ($where_declared eq 'filescope') {
260 } elsif ($where_declared eq 'in_named') {
263 my $sub_scalar = 7000;
264 my @sub_array = (8000, 8100, 8200, 8300);
265 my %sub_hash = 9000..9009;
268 } elsif ($where_declared eq 'in_anon') {
271 my $sub_scalar = 7000;
272 my @sub_array = (8000, 8100, 8200, 8300);
273 my %sub_hash = 9000..9009;
277 die "What was $where_declared?"
280 if ($within eq 'foreach') {
282 my \$foreach = 12000;
283 my \@list = (10000, 10010);
284 foreach \$foreach (\@list) {
286 } elsif ($within eq 'naked') {
287 $code .= " { # naked block\n" # }
288 } elsif ($within eq 'other_sub') {
289 $code .= " sub inner_sub {\n" # }
291 die "What was $within?"
295 @inners = ( qw!global_scalar global_array global_hash! ,
296 qw!fs_scalar fs_array fs_hash! );
297 push @inners, 'foreach' if $within eq 'foreach';
298 if ($where_declared ne 'filescope') {
299 push @inners, qw!sub_scalar sub_array sub_hash!;
301 for $inner_sub_test (@inners) {
303 if ($inner_type eq 'named') {
304 $code .= " sub named_$sub_test "
305 } elsif ($inner_type eq 'anon') {
306 $code .= " \$anon_$sub_test = sub "
308 die "What was $inner_type?"
311 # Now to write the body of the test sub
312 if ($inner_sub_test eq 'global_scalar') {
313 $code .= '{ ++$global_scalar }'
314 } elsif ($inner_sub_test eq 'fs_scalar') {
315 $code .= '{ ++$fs_scalar }'
316 } elsif ($inner_sub_test eq 'sub_scalar') {
317 $code .= '{ ++$sub_scalar }'
318 } elsif ($inner_sub_test eq 'global_array') {
319 $code .= '{ ++$global_array[1] }'
320 } elsif ($inner_sub_test eq 'fs_array') {
321 $code .= '{ ++$fs_array[1] }'
322 } elsif ($inner_sub_test eq 'sub_array') {
323 $code .= '{ ++$sub_array[1] }'
324 } elsif ($inner_sub_test eq 'global_hash') {
325 $code .= '{ ++$global_hash{3002} }'
326 } elsif ($inner_sub_test eq 'fs_hash') {
327 $code .= '{ ++$fs_hash{6002} }'
328 } elsif ($inner_sub_test eq 'sub_hash') {
329 $code .= '{ ++$sub_hash{9002} }'
330 } elsif ($inner_sub_test eq 'foreach') {
331 $code .= '{ ++$foreach }'
333 die "What was $inner_sub_test?"
337 if ($inner_type eq 'anon') {
341 $sub_test++; # sub name sequence number
343 } # End of foreach $inner_sub_test
345 # Close up $within block # {
348 # Close up $where_declared block
349 if ($where_declared eq 'in_named') { # {
351 } elsif ($where_declared eq 'in_anon') { # {
355 # We may need to do something with the sub we just made...
356 $code .= "undef \$outer;\n" if $undef_outer;
357 $code .= "&inner_sub;\n" if $call_inner;
359 if ($where_declared eq 'in_named') {
360 $code .= "&outer;\n\n";
361 } elsif ($where_declared eq 'in_anon') {
362 $code .= "&\$outer;\n\n"
366 # Now, we can actually prep to run the tests.
367 for $inner_sub_test (@inners) {
368 $expected = $expected{$inner_sub_test} or
369 die "expected $inner_sub_test missing";
371 # Named closures won't access the expected vars
373 substr($inner_sub_test, 0, 4) eq "sub_" ) {
377 # If you make a sub within a foreach loop,
378 # what happens if it tries to access the
379 # foreach index variable? If it's a named
380 # sub, it gets the var from "outside" the loop,
381 # but if it's anon, it gets the value to which
382 # the index variable is aliased.
384 # Of course, if the value was set only
385 # within another sub which was never called,
386 # the value has not been set yet.
388 if ($inner_sub_test eq 'foreach') {
389 if ($inner_type eq 'named') {
390 if ($call_outer || ($where_declared eq 'filescope')) {
399 if ($inner_type eq 'anon') {
400 $code .= "test { &\$anon_$test == $expected };\n"
402 $code .= "test { &named_$test == $expected };\n"
407 if ($Config{d_fork} and $^O ne 'VMS' and $^O ne 'MSWin32') {
408 # Fork off a new perl to run the tests.
409 # (This is so we can catch spurious warnings.)
410 $| = 1; print ""; $| = 0; # flush output before forking
411 pipe READ, WRITE or die "Can't make pipe: $!";
412 pipe READ2, WRITE2 or die "Can't make second pipe: $!";
413 die "Can't fork: $!" unless defined($pid = open PERL, "|-");
415 # Child process here. We're going to send errors back
416 # through the extra pipe.
419 open STDOUT, ">&WRITE" or die "Can't redirect STDOUT: $!";
420 open STDERR, ">&WRITE2" or die "Can't redirect STDERR: $!";
421 exec './perl', '-w', '-'
422 or die "Can't exec ./perl: $!";
424 # Parent process here.
430 $output = join '', <READ>;
431 $errors = join '', <READ2>; }
436 # No fork(). Do it the hard way.
437 my $cmdfile = "tcmd$$"; $cmdfile++ while -e $cmdfile;
438 my $errfile = "terr$$"; $errfile++ while -e $errfile;
439 my @tmpfiles = ($cmdfile, $errfile);
440 open CMD, ">$cmdfile"; print CMD $code; close CMD;
441 my $cmd = (($^O eq 'VMS') ? "MCR $^X"
442 : ($^O eq 'MSWin32') ? '.\perl'
444 $cmd .= " -w $cmdfile 2>$errfile";
445 if ($^O eq 'VMS' or $^O eq 'MSWin32') {
446 # Use pipe instead of system so we don't inherit STD* from
447 # this process, and then foul our pipe back to parent by
448 # redirecting output in the child.
449 open PERL,"$cmd |" or die "Can't open pipe: $!\n";
450 { local $/; $output = join '', <PERL> }
453 my $outfile = "tout$$"; $outfile++ while -e $outfile;
454 push @tmpfiles, $outfile;
455 system "$cmd >$outfile";
456 { local $/; open IN, $outfile; $output = <IN>; close IN }
459 printf "not ok: exited with error code %04X\n", $?;
460 $debugging or do { 1 while unlink @tmpfiles };
463 { local $/; open IN, $errfile; $errors = <IN>; close IN }
464 1 while unlink @tmpfiles;
467 print STDERR $errors;
468 if ($debugging && ($errors || $? || ($output =~ /not ok/))) {
470 for $line (split '\n', $code) {
471 printf "%3d: %s\n", ++$lnum, $line;
474 printf "not ok: exited with error code %04X\n", $? if $?;
475 print "-" x 30, "\n" if $debugging;
477 } # End of foreach $within
478 } # End of foreach $where_declared
479 } # End of foreach $inner_type