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