Ignore backup files in strict.t and warning.t
[p5sagit/p5-mst-13.2.git] / t / pragma / subs.t
CommitLineData
8ebc5c01 1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
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';
8ebc5c01 15my $tmpfile = "tmp0000";
16my $i = 0 ;
171 while -f ++$tmpfile;
44a8e56a 18END { if ($tmpfile) { 1 while unlink $tmpfile} }
8ebc5c01 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 }
44a8e56a 44 open TEST, ">$tmpfile";
45 print TEST $prog,"\n";
8ebc5c01 46 close TEST;
44a8e56a 47 my $results = $Is_VMS ?
48 `MCR $^X $switch $tmpfile` :
49 `sh -c './perl $switch $tmpfile' 2>&1`;
8ebc5c01 50 my $status = $?;
8ebc5c01 51 $results =~ s/\n+$//;
44a8e56a 52 # allow expected output to be written as if $prog is on STDIN
53 $results =~ s/tmp\d+/-/g;
54 $results =~ s/\n%[A-Z]+-[SIWEF]-.*$// if $Is_VMS; # clip off DCL status msg
8ebc5c01 55 $expected =~ s/\n+$//;
56 my $prefix = ($results =~ s/^PREFIX\n//) ;
57 if ( $results =~ s/^SKIPPED\n//) {
58 print "$results\n" ;
59 }
60 elsif (($prefix and $results !~ /^\Q$expected/) or
61 (!$prefix and $results ne $expected)){
62 print STDERR "PROG: $switch\n$prog\n";
63 print STDERR "EXPECTED:\n$expected\n";
64 print STDERR "GOT:\n$results\n";
65 print "not ";
66 }
67 print "ok ", ++$i, "\n";
68 foreach (@temps)
69 { unlink $_ if $_ }
70}
71
72__END__
73
74# Error - not predeclaring a sub
75Fred 1,2 ;
76sub Fred {}
77EXPECT
78Number found where operator expected at - line 3, near "Fred 1"
79 (Do you need to predeclare Fred?)
80syntax error at - line 3, near "Fred 1"
81Execution of - aborted due to compilation errors.
82########
83
84# Error - not predeclaring a sub in time
85Fred 1,2 ;
86use subs qw( Fred ) ;
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# AOK
96use subs qw( Fred) ;
97Fred 1,2 ;
98sub Fred { print $_[0] + $_[1], "\n" }
99EXPECT
1003
101########
102
103# override a built-in function
104use subs qw( open ) ;
105open 1,2 ;
106sub open { print $_[0] + $_[1], "\n" }
107EXPECT
1083
109########
110
111--FILE-- abc
112Fred 1,2 ;
1131;
114--FILE--
115use subs qw( Fred ) ;
116require "./abc" ;
117sub Fred { print $_[0] + $_[1], "\n" }
118EXPECT
1193
120########
121
122# check that it isn't affected by block scope
123{
124 use subs qw( Fred ) ;
125}
126Fred 1, 2;
127sub Fred { print $_[0] + $_[1], "\n" }
128EXPECT
1293