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