Re: [PATCH] resurrect t/op/magic.t on VMS
[p5sagit/p5-mst-13.2.git] / t / op / magic.t
1 #!./perl
2
3 BEGIN {
4     $| = 1;
5     chdir 't' if -d 't';
6     @INC = '../lib';
7     $SIG{__WARN__} = sub { die "Dying on warning: ", @_ };
8 }
9
10 use warnings;
11
12 my $test = 1;
13 sub ok {
14     my($ok, $info, $todo) = @_;
15
16     # You have to do it this way or VMS will get confused.
17     printf "%s $test%s\n", $ok ? "ok" : "not ok",
18                            $todo ? " # TODO $todo" : '';
19
20     unless( $ok ) {
21         printf "# Failed test at line %d\n", (caller)[2];
22         print  "# $info" if defined $info;
23     }
24
25     $test++;
26     return $ok;
27 }
28
29 sub skip {
30     my($reason) = @_;
31
32     printf "ok $test # skipped%s\n", defined $reason ? ": $reason" : '';
33
34     $test++;
35     return 1;
36 }
37
38 print "1..43\n";
39
40 $Is_MSWin32 = $^O eq 'MSWin32';
41 $Is_NetWare = $^O eq 'NetWare';
42 $Is_VMS     = $^O eq 'VMS';
43 $Is_Dos     = $^O eq 'dos';
44 $Is_os2     = $^O eq 'os2';
45 $Is_Cygwin  = $^O eq 'cygwin';
46 $Is_MacOS   = $^O eq 'MacOS';
47 $Is_MPE     = $^O eq 'mpeix';           
48
49 $PERL = ($Is_NetWare            ? 'perl'   :
50          ($Is_MacOS || $Is_VMS) ? $^X      :
51          $Is_MSWin32            ? '.\perl' :
52          './perl');
53
54 eval '$ENV{"FOO"} = "hi there";';       # check that ENV is inited inside eval
55 # cmd.exe will echo 'variable=value' but 4nt will echo just the value
56 # -- Nikola Knezevic
57 if ($Is_MSWin32)  { ok `set FOO` =~ /^(FOO=)?hi there$/; }
58 elsif ($Is_MacOS) { ok "1 # skipped", 1; }
59 elsif ($Is_VMS)   { ok `write sys\$output f\$trnlnm("FOO")` eq "hi there\n"; }
60 else              { ok `echo \$FOO` eq "hi there\n"; }
61
62 unlink 'ajslkdfpqjsjfk';
63 $! = 0;
64 open(FOO,'ajslkdfpqjsjfk');
65 ok $!, $!;
66 close FOO; # just mention it, squelch used-only-once
67
68 if ($Is_MSWin32 || $Is_NetWare || $Is_Dos || $Is_MPE || $Is_MacOS) {
69     skip('SIGINT not safe on this platform') for 1..2;
70 }
71 else {
72   # the next tests are done in a subprocess because sh spits out a
73   # newline onto stderr when a child process kills itself with SIGINT.
74   # We use a pipe rather than system() because the VMS command buffer 
75   # would overflow with a command that long.
76
77     open( CMDPIPE, "| $PERL");
78
79     print CMDPIPE <<'END';
80
81     $| = 1;             # command buffering
82
83     $SIG{"INT"} = "ok3";     kill "INT",$$; sleep 1;
84     $SIG{"INT"} = "IGNORE";  kill "INT",$$; sleep 1; print "ok 4\n";
85     $SIG{"INT"} = "DEFAULT"; kill "INT",$$; sleep 1; print "not ok 4\n";
86
87     sub ok3 {
88         if (($x = pop(@_)) eq "INT") {
89             print "ok 3\n";
90         }
91         else {
92             print "not ok 3 ($x @_)\n";
93         }
94     }
95
96 END
97
98     close CMDPIPE;
99
100     $test += 2;
101 }
102
103 # can we slice ENV?
104 @val1 = @ENV{keys(%ENV)};
105 @val2 = values(%ENV);
106 ok join(':',@val1) eq join(':',@val2);
107 ok @val1 > 1;
108
109 # regex vars
110 'foobarbaz' =~ /b(a)r/;
111 ok $` eq 'foo', $`;
112 ok $& eq 'bar', $&;
113 ok $' eq 'baz', $';
114 ok $+ eq 'a', $+;
115
116 # $"
117 @a = qw(foo bar baz);
118 ok "@a" eq "foo bar baz", "@a";
119 {
120     local $" = ',';
121     ok "@a" eq "foo,bar,baz", "@a";
122 }
123
124 # $;
125 %h = ();
126 $h{'foo', 'bar'} = 1;
127 ok((keys %h)[0] eq "foo\034bar", (keys %h)[0]);
128 {
129     local $; = 'x';
130     %h = ();
131     $h{'foo', 'bar'} = 1;
132     ok((keys %h)[0] eq 'fooxbar', (keys %h)[0]);
133 }
134
135 # $?, $@, $$
136 system qq[$PERL -e "exit(0)"];
137 ok $? == 0, $?;
138 system qq[$PERL -e "exit(1)"];
139 ok $? != 0, $?;
140
141 eval { die "foo\n" };
142 ok $@ eq "foo\n", $@;
143
144 ok $$ > 0, $$;
145
146 # $^X and $0
147 {
148     if ($^O eq 'qnx') {
149         chomp($wd = `/usr/bin/fullpath -t`);
150     }
151     elsif($Is_Cygwin) {
152        # Cygwin turns the symlink into the real file
153        chomp($wd = `pwd`);
154        $wd =~ s#/t$##;
155     }
156     elsif($Is_os2) {
157        $wd = Cwd::sys_cwd();
158     }
159     elsif($Is_MacOS) {
160        $wd = ':';
161     }
162     else {
163         $wd = '.';
164     }
165     my $perl = ($Is_MacOS || $Is_VMS) ? $^X : "$wd/perl";
166     my $headmaybe = '';
167     my $tailmaybe = '';
168     $script = "$wd/show-shebang";
169     if ($Is_MSWin32) {
170         chomp($wd = `cd`);
171         $wd =~ s|\\|/|g;
172         $perl = "$wd/perl.exe";
173         $script = "$wd/show-shebang.bat";
174         $headmaybe = <<EOH ;
175 \@rem ='
176 \@echo off
177 $perl -x \%0
178 goto endofperl
179 \@rem ';
180 EOH
181         $tailmaybe = <<EOT ;
182
183 __END__
184 :endofperl
185 EOT
186     }
187     elsif ($Is_os2) {
188       $script = "./show-shebang";
189     }
190     elsif ($Is_MacOS) {
191       $script = ":show-shebang";
192     }
193     elsif ($Is_VMS) {
194       $script = "[]show-shebang";
195     }
196     if ($^O eq 'os390' or $^O eq 'posix-bc' or $^O eq 'vmesa') {  # no shebang
197         $headmaybe = <<EOH ;
198     eval 'exec ./perl -S \$0 \${1+"\$\@"}'
199         if 0;
200 EOH
201     }
202     $s1 = "\$^X is $perl, \$0 is $script\n";
203     ok open(SCRIPT, ">$script"), $!;
204     ok print(SCRIPT $headmaybe . <<EOB . <<'EOF' . $tailmaybe), $!;
205 #!$wd/perl
206 EOB
207 print "\$^X is $^X, \$0 is $0\n";
208 EOF
209     ok close(SCRIPT), $!;
210     ok chmod(0755, $script), $!;
211     $_ = ($Is_MacOS || $Is_VMS) ? `$perl $script` : `$script`;
212     s/\.exe//i if $Is_Dos or $Is_Cygwin or $Is_os2;
213     s{\bminiperl\b}{perl}; # so that test doesn't fail with miniperl
214     s{is perl}{is $perl}; # for systems where $^X is only a basename
215     s{\\}{/}g;
216     ok((($Is_MSWin32 || $Is_os2) ? uc($_) eq uc($s1) : $_ eq $s1), " :$_:!=:$s1:");
217     $_ = `$perl $script`;
218     s/\.exe//i if $Is_Dos or $Is_os2;
219     s{\\}{/}g;
220     ok((($Is_MSWin32 || $Is_os2) ? uc($_) eq uc($s1) : $_ eq $s1), " :$_:!=:$s1: after `$perl $script`");
221     ok unlink($script), $!;
222 }
223
224 # $], $^O, $^T
225 ok $] >= 5.00319, $];
226 ok $^O;
227 ok $^T > 850000000, $^T;
228
229 if ($Is_VMS || $Is_Dos || $Is_MacOS) {
230     skip("%ENV manipulations fail or aren't safe on $^O") for 1..2;
231 }
232 else {
233         $PATH = $ENV{PATH};
234         $PDL = $ENV{PERL_DESTRUCT_LEVEL} || 0;
235         $ENV{foo} = "bar";
236         %ENV = ();
237         $ENV{PATH} = $PATH;
238         $ENV{PERL_DESTRUCT_LEVEL} = $PDL || 0;
239         ok ($Is_MSWin32 ? (`set foo 2>NUL` eq "")
240                                 : (`echo \$foo` eq "\n") );
241
242         $ENV{__NoNeSuCh} = "foo";
243         $0 = "bar";
244 # cmd.exe will echo 'variable=value' but 4nt will echo just the value
245 # -- Nikola Knezevic
246         ok ($Is_MSWin32 ? (`set __NoNeSuCh` =~ /^(__NoNeSuCh=)?foo$/)
247                             : (`echo \$__NoNeSuCh` eq "foo\n") );
248 }
249
250 if ($Is_VMS) {
251     ok(1,0,"'\$!=undef' does throw a warning");
252 }
253 else {
254     local $SIG{'__WARN__'} = sub { print "# @_\nnot " };
255     $! = undef;
256     ok 1;
257 }
258
259 # test case-insignificance of %ENV (these tests must be enabled only
260 # when perl is compiled with -DENV_IS_CASELESS)
261 if ($Is_MSWin32 || $Is_NetWare) {
262     %ENV = ();
263     $ENV{'Foo'} = 'bar';
264     $ENV{'fOo'} = 'baz';
265     ok (scalar(keys(%ENV)) == 1);
266     ok exists($ENV{'FOo'});
267     ok (delete($ENV{'foO'}) eq 'baz');
268     ok (scalar(keys(%ENV)) == 0);
269 }
270 else {
271     skip('no caseless %ENV support') for 1..4;
272 }
273
274 # Make sure Errno hasn't been prematurely autoloaded
275
276 ok !defined %Errno::;
277
278 # Test auto-loading of Errno when %! is used
279
280 ok scalar eval q{
281    my $errs = %!;
282    defined %Errno::;
283 }, $@;
284
285
286 # Make sure that Errno loading doesn't clobber $!
287
288 undef %Errno::;
289 delete $INC{"Errno.pm"};
290
291 open(FOO, "nonesuch"); # Generate ENOENT
292 my %errs = %{"!"}; # Cause Errno.pm to be loaded at run-time
293 ok ${"!"}{ENOENT};
294
295 ok $^S == 0;
296 eval { ok $^S == 1 };
297 ok $^S == 0;
298
299 ok ${^TAINT} == 0;
300 eval { ${^TAINT} = 1 };
301 ok ${^TAINT} == 0;