This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Retract #11166 (and #11237).
[perl5.git] / lib / subs.t
CommitLineData
8ebc5c01 1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
20822f61 5 @INC = '../lib';
8ebc5c01 6 $ENV{PERL5LIB} = '../lib';
7}
8
9$| = 1;
10undef $/;
11my @prgs = split "\n########\n", <DATA>;
12print "1..", scalar @prgs, "\n";
13
44a8e56a 14my $Is_VMS = $^O eq 'VMS';
68dc0745 15my $Is_MSWin32 = $^O eq 'MSWin32';
2986a63f 16my $Is_NetWare = $^O eq 'NetWare';
8ebc5c01 17my $tmpfile = "tmp0000";
18my $i = 0 ;
191 while -f ++$tmpfile;
44a8e56a 20END { if ($tmpfile) { 1 while unlink $tmpfile} }
8ebc5c01 21
22for (@prgs){
23 my $switch = "";
24 my @temps = () ;
25 if (s/^\s*-\w+//){
26 $switch = $&;
27 }
28 my($prog,$expected) = split(/\nEXPECT\n/, $_);
29 if ( $prog =~ /--FILE--/) {
30 my(@files) = split(/\n--FILE--\s*([^\s\n]*)\s*\n/, $prog) ;
31 shift @files ;
32 die "Internal error test $i didn't split into pairs, got " .
33 scalar(@files) . "[" . join("%%%%", @files) ."]\n"
34 if @files % 2 ;
35 while (@files > 2) {
36 my $filename = shift @files ;
37 my $code = shift @files ;
38 push @temps, $filename ;
39 open F, ">$filename" or die "Cannot open $filename: $!\n" ;
40 print F $code ;
41 close F ;
42 }
43 shift @files ;
44 $prog = shift @files ;
45 }
44a8e56a 46 open TEST, ">$tmpfile";
47 print TEST $prog,"\n";
8ebc5c01 48 close TEST;
44a8e56a 49 my $results = $Is_VMS ?
f0963acb 50 `./perl $switch $tmpfile 2>&1` :
68dc0745 51 $Is_MSWin32 ?
52 `.\\perl -I../lib $switch $tmpfile 2>&1` :
2986a63f
JH
53 $Is_NetWare ?
54 `perl -I../lib $switch $tmpfile 2>&1` :
648cac19 55 `./perl $switch $tmpfile 2>&1`;
8ebc5c01 56 my $status = $?;
8ebc5c01 57 $results =~ s/\n+$//;
44a8e56a 58 # allow expected output to be written as if $prog is on STDIN
59 $results =~ s/tmp\d+/-/g;
60 $results =~ s/\n%[A-Z]+-[SIWEF]-.*$// if $Is_VMS; # clip off DCL status msg
f0ec1f9a 61# bison says 'parse error' instead of 'syntax error',
d91e2bdb 62# various yaccs may or may not capitalize 'syntax'.
2a8ee232 63 $results =~ s/^(syntax|parse) error/syntax error/mig;
8ebc5c01 64 $expected =~ s/\n+$//;
65 my $prefix = ($results =~ s/^PREFIX\n//) ;
66 if ( $results =~ s/^SKIPPED\n//) {
67 print "$results\n" ;
68 }
69 elsif (($prefix and $results !~ /^\Q$expected/) or
70 (!$prefix and $results ne $expected)){
71 print STDERR "PROG: $switch\n$prog\n";
72 print STDERR "EXPECTED:\n$expected\n";
73 print STDERR "GOT:\n$results\n";
74 print "not ";
75 }
76 print "ok ", ++$i, "\n";
77 foreach (@temps)
78 { unlink $_ if $_ }
79}
80
81__END__
82
83# Error - not predeclaring a sub
84Fred 1,2 ;
85sub Fred {}
86EXPECT
87Number found where operator expected at - line 3, near "Fred 1"
88 (Do you need to predeclare Fred?)
89syntax error at - line 3, near "Fred 1"
90Execution of - aborted due to compilation errors.
91########
92
93# Error - not predeclaring a sub in time
94Fred 1,2 ;
95use subs qw( Fred ) ;
96sub Fred {}
97EXPECT
98Number found where operator expected at - line 3, near "Fred 1"
99 (Do you need to predeclare Fred?)
100syntax error at - line 3, near "Fred 1"
68dc0745 101BEGIN not safe after errors--compilation aborted at - line 4.
8ebc5c01 102########
103
104# AOK
105use subs qw( Fred) ;
106Fred 1,2 ;
107sub Fred { print $_[0] + $_[1], "\n" }
108EXPECT
1093
110########
111
112# override a built-in function
113use subs qw( open ) ;
114open 1,2 ;
115sub open { print $_[0] + $_[1], "\n" }
116EXPECT
1173
118########
119
55a105fd
NC
120# override a built-in function, call after definition
121use subs qw( open ) ;
122sub open { print $_[0] + $_[1], "\n" }
123open 1,2 ;
124EXPECT
1253
126########
127
128# override a built-in function, call with ()
129use subs qw( open ) ;
130open (1,2) ;
131sub open { print $_[0] + $_[1], "\n" }
132EXPECT
1333
134########
135
136# override a built-in function, call with () after definition
137use subs qw( open ) ;
138sub open { print $_[0] + $_[1], "\n" }
139open (1,2) ;
140EXPECT
1413
142########
143
8ebc5c01 144--FILE-- abc
145Fred 1,2 ;
1461;
147--FILE--
148use subs qw( Fred ) ;
149require "./abc" ;
150sub Fred { print $_[0] + $_[1], "\n" }
151EXPECT
1523
153########
154
155# check that it isn't affected by block scope
156{
157 use subs qw( Fred ) ;
158}
159Fred 1, 2;
160sub Fred { print $_[0] + $_[1], "\n" }
161EXPECT
1623