[inseparable changes from patch from perl5.003_20 to perl5.003_21]
[p5sagit/p5-mst-13.2.git] / t / pragma / subs.t
1 #!./perl 
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = '../lib';
6     $ENV{PERL5LIB} = '../lib';
7 }
8
9 $| = 1;
10 undef $/;
11 my @prgs = split "\n########\n", <DATA>;
12 print "1..", scalar @prgs, "\n";
13
14 my $Is_VMS = $^O eq 'VMS';
15 my $tmpfile = "tmp0000";
16 my $i = 0 ;
17 1 while -f ++$tmpfile;
18 END {  if ($tmpfile) { 1 while unlink $tmpfile} }
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     open TEST, ">$tmpfile";
45     print TEST $prog,"\n";
46     close TEST;
47     my $results = $Is_VMS ?
48                   `MCR $^X $switch $tmpfile` :
49                   `sh -c './perl $switch $tmpfile' 2>&1`;
50     my $status = $?;
51     $results =~ s/\n+$//;
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
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
75 Fred 1,2 ;
76 sub Fred {}
77 EXPECT
78 Number found where operator expected at - line 3, near "Fred 1"
79         (Do you need to predeclare Fred?)
80 syntax error at - line 3, near "Fred 1"
81 Execution of - aborted due to compilation errors.
82 ########
83
84 # Error - not predeclaring a sub in time
85 Fred 1,2 ;
86 use subs qw( Fred ) ;
87 sub Fred {}
88 EXPECT
89 Number found where operator expected at - line 3, near "Fred 1"
90         (Do you need to predeclare Fred?)
91 syntax error at - line 3, near "Fred 1"
92 Execution of - aborted due to compilation errors.
93 ########
94
95 # AOK
96 use subs qw( Fred) ;
97 Fred 1,2 ;
98 sub Fred { print $_[0] + $_[1], "\n" }
99 EXPECT
100 3
101 ########
102
103 # override a built-in function
104 use subs qw( open ) ;
105 open 1,2 ;
106 sub open { print $_[0] + $_[1], "\n" }
107 EXPECT
108 3
109 ########
110
111 --FILE-- abc
112 Fred 1,2 ;
113 1;
114 --FILE--
115 use subs qw( Fred ) ;
116 require "./abc" ;
117 sub Fred { print $_[0] + $_[1], "\n" }
118 EXPECT
119 3
120 ########
121
122 # check that it isn't affected by block scope
123 {
124     use subs qw( Fred ) ;
125 }
126 Fred 1, 2;
127 sub Fred { print $_[0] + $_[1], "\n" }
128 EXPECT
129 3