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