tied hashes
[p5sagit/p5-mst-13.2.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 ?
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 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
61 $results =~ s/tmp\d+/-/g;
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 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