This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate change #9108 from maintperl to mainline.
[perl5.git] / t / op / anonsub.t
CommitLineData
282f25c9
JH
1#!./perl
2
3chdir 't' if -d 't';
4@INC = '../lib';
5$Is_VMS = $^O eq 'VMS';
6$Is_MSWin32 = $^O eq 'MSWin32';
7$ENV{PERL5LIB} = "../lib" unless $Is_VMS;
8
9$|=1;
10
11undef $/;
12@prgs = split "\n########\n", <DATA>;
13print "1..", scalar @prgs, "\n";
14
15$tmpfile = "asubtmp000";
161 while -f ++$tmpfile;
17END { if ($tmpfile) { 1 while unlink $tmpfile; } }
18
19for (@prgs){
20 my $switch = "";
21 if (s/^\s*(-\w+)//){
22 $switch = $1;
23 }
24 my($prog,$expected) = split(/\nEXPECT\n/, $_);
25 open TEST, ">$tmpfile";
26 print TEST "$prog\n";
27 close TEST;
28 my $results = $Is_VMS ?
29 `MCR $^X "-I[-.lib]" $switch $tmpfile 2>&1` :
30 $Is_MSWin32 ?
31 `.\\perl -I../lib $switch $tmpfile 2>&1` :
32 `./perl $switch $tmpfile 2>&1`;
33 my $status = $?;
34 $results =~ s/\n+$//;
35 # allow expected output to be written as if $prog is on STDIN
36 $results =~ s/runltmp\d+/-/g;
37 $results =~ s/\n%[A-Z]+-[SIWEF]-.*$// if $Is_VMS; # clip off DCL status msg
38 $expected =~ s/\n+$//;
39 if ($results ne $expected) {
40 print STDERR "PROG: $switch\n$prog\n";
41 print STDERR "EXPECTED:\n$expected\n";
42 print STDERR "GOT:\n$results\n";
43 print "not ";
44 }
45 print "ok ", ++$i, "\n";
46}
47
48__END__
49sub X {
50 my $n = "ok 1\n";
51 sub { print $n };
52}
53my $x = X();
54undef &X;
55$x->();
56EXPECT
57ok 1
58########
59sub X {
60 my $n = "ok 1\n";
61 sub {
62 my $dummy = $n; # eval can't close on $n without internal reference
63 eval 'print $n';
64 die $@ if $@;
65 };
66}
67my $x = X();
68undef &X;
69$x->();
70EXPECT
71ok 1
72########
73sub X {
74 my $n = "ok 1\n";
75 eval 'sub { print $n }';
76}
77my $x = X();
78die $@ if $@;
79undef &X;
80$x->();
81EXPECT
82ok 1
83########
84sub X;
85sub X {
86 my $n = "ok 1\n";
87 eval 'sub Y { my $p = shift; $p->() }';
88 die $@ if $@;
89 Y(sub { print $n });
90}
91X();
92EXPECT
93ok 1