This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
debugger goof
[perl5.git] / lib / subs.t
1 #!./perl 
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = '../lib';
6     $ENV{PERL5LIB} = '../lib';
7 }
8
9 $| = 1;
10 undef $/;
11 my @prgs = split "\n########\n", <DATA>;
12 print "1..", scalar @prgs, "\n";
13
14 my $Is_VMS = $^O eq 'VMS';
15 my $Is_MSWin32 = $^O eq 'MSWin32';
16 my $Is_NetWare = $^O eq 'NetWare';
17 my $tmpfile = "tmp0000";
18 my $i = 0 ;
19 1 while -f ++$tmpfile;
20 END {  if ($tmpfile) { 1 while unlink $tmpfile} }
21
22 for (@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     }
46     open TEST, ">$tmpfile";
47     print TEST $prog,"\n";
48     close TEST;
49     my $results = $Is_VMS ?
50                   `./perl $switch $tmpfile 2>&1` :
51                   $Is_MSWin32 ?
52                   `.\\perl -I../lib $switch $tmpfile 2>&1` :
53                   $Is_NetWare ?
54                   `perl -I../lib $switch $tmpfile 2>&1` :
55                   `./perl $switch $tmpfile 2>&1`;
56     my $status = $?;
57     $results =~ s/\n+$//;
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
61 # bison says 'parse error' instead of 'syntax error',
62 # various yaccs may or may not capitalize 'syntax'.
63     $results =~ s/^(syntax|parse) error/syntax error/mig;
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
84 Fred 1,2 ;
85 sub Fred {}
86 EXPECT
87 Number found where operator expected at - line 3, near "Fred 1"
88         (Do you need to predeclare Fred?)
89 syntax error at - line 3, near "Fred 1"
90 Execution of - aborted due to compilation errors.
91 ########
92
93 # Error - not predeclaring a sub in time
94 Fred 1,2 ;
95 use subs qw( Fred ) ;
96 sub Fred {}
97 EXPECT
98 Number found where operator expected at - line 3, near "Fred 1"
99         (Do you need to predeclare Fred?)
100 syntax error at - line 3, near "Fred 1"
101 BEGIN not safe after errors--compilation aborted at - line 4.
102 ########
103
104 # AOK
105 use subs qw( Fred) ;
106 Fred 1,2 ;
107 sub Fred { print $_[0] + $_[1], "\n" }
108 EXPECT
109 3
110 ########
111
112 # override a built-in function
113 use subs qw( open ) ;
114 open 1,2 ;
115 sub open { print $_[0] + $_[1], "\n" }
116 EXPECT
117 3
118 ########
119
120 # override a built-in function, call after definition
121 use subs qw( open ) ;
122 sub open { print $_[0] + $_[1], "\n" }
123 open 1,2 ;
124 EXPECT
125 3
126 ########
127
128 # override a built-in function, call with ()
129 use subs qw( open ) ;
130 open (1,2) ;
131 sub open { print $_[0] + $_[1], "\n" }
132 EXPECT
133 3
134 ########
135
136 # override a built-in function, call with () after definition
137 use subs qw( open ) ;
138 sub open { print $_[0] + $_[1], "\n" }
139 open (1,2) ;
140 EXPECT
141 3
142 ########
143
144 --FILE-- abc
145 Fred 1,2 ;
146 1;
147 --FILE--
148 use subs qw( Fred ) ;
149 require "./abc" ;
150 sub Fred { print $_[0] + $_[1], "\n" }
151 EXPECT
152 3
153 ########
154
155 # check that it isn't affected by block scope
156 {
157     use subs qw( Fred ) ;
158 }
159 Fred 1, 2;
160 sub Fred { print $_[0] + $_[1], "\n" }
161 EXPECT
162 3