1#!./perl 2 3BEGIN { 4 chdir 't' if -d 't'; 5 @INC = '../lib'; 6 $ENV{PERL5LIB} = '../lib'; 7 require './test.pl'; 8} 9 10$| = 1; 11undef $/; 12my @prgs = split "\n########\n", <DATA>; 13print "1..", scalar @prgs, "\n"; 14 15my $Is_VMS = $^O eq 'VMS'; 16my $Is_MSWin32 = $^O eq 'MSWin32'; 17my $Is_NetWare = $^O eq 'NetWare'; 18my $i = 0 ; 19 20for (@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 83Fred 1,2 ; 84sub Fred {} 85EXPECT 86Number found where operator expected at - line 3, near "Fred 1" 87 (Do you need to predeclare Fred?) 88syntax error at - line 3, near "Fred 1" 89Execution of - aborted due to compilation errors. 90######## 91 92# Error - not predeclaring a sub in time 93Fred 1,2 ; 94use subs qw( Fred ) ; 95sub Fred {} 96EXPECT 97Number found where operator expected at - line 3, near "Fred 1" 98 (Do you need to predeclare Fred?) 99syntax error at - line 3, near "Fred 1" 100BEGIN not safe after errors--compilation aborted at - line 4. 101######## 102 103# AOK 104use subs qw( Fred) ; 105Fred 1,2 ; 106sub Fred { print $_[0] + $_[1], "\n" } 107EXPECT 1083 109######## 110 111# override a built-in function 112use subs qw( open ) ; 113open 1,2 ; 114sub open { print $_[0] + $_[1], "\n" } 115EXPECT 1163 117######## 118 119# override a built-in function, call after definition 120use subs qw( open ) ; 121sub open { print $_[0] + $_[1], "\n" } 122open 1,2 ; 123EXPECT 1243 125######## 126 127# override a built-in function, call with () 128use subs qw( open ) ; 129open (1,2) ; 130sub open { print $_[0] + $_[1], "\n" } 131EXPECT 1323 133######## 134 135# override a built-in function, call with () after definition 136use subs qw( open ) ; 137sub open { print $_[0] + $_[1], "\n" } 138open (1,2) ; 139EXPECT 1403 141######## 142 143--FILE-- abc 144Fred 1,2 ; 1451; 146--FILE-- 147use subs qw( Fred ) ; 148require "./abc" ; 149sub Fred { print $_[0] + $_[1], "\n" } 150EXPECT 1513 152######## 153 154# check that it isn't affected by block scope 155{ 156 use subs qw( Fred ) ; 157} 158Fred 1, 2; 159sub Fred { print $_[0] + $_[1], "\n" } 160EXPECT 1613 162