[perl #68108] : also fix if/else constant folding
[p5sagit/p5-mst-13.2.git] / t / op / magic.t
CommitLineData
8d063cd8 1#!./perl
2
90ce63d5 3BEGIN {
90ce63d5 4 $| = 1;
5 chdir 't' if -d 't';
20822f61 6 @INC = '../lib';
0409250f 7 $ENV{PATH} = '/bin' if ${^TAINT};
774d564b 8 $SIG{__WARN__} = sub { die "Dying on warning: ", @_ };
b2978f4e 9 require './test.pl';
90ce63d5 10}
8d063cd8 11
9f1b1f2d 12use warnings;
04fee9b5 13use Config;
9f1b1f2d 14
179c85a2 15plan (tests => 79);
0bee926d 16
43651d81 17$Is_MSWin32 = $^O eq 'MSWin32';
18$Is_NetWare = $^O eq 'NetWare';
19$Is_VMS = $^O eq 'VMS';
20$Is_Dos = $^O eq 'dos';
21$Is_os2 = $^O eq 'os2';
22$Is_Cygwin = $^O eq 'cygwin';
23$Is_MacOS = $^O eq 'MacOS';
24$Is_MPE = $^O eq 'mpeix';
25$Is_miniperl = $ENV{PERL_CORE_MINITEST};
dbc1d986 26$Is_BeOS = $^O eq 'beos';
be708cc0 27
c8d62b71 28$PERL = $ENV{PERL}
29 || ($Is_NetWare ? 'perl' :
30 ($Is_MacOS || $Is_VMS) ? $^X :
31 $Is_MSWin32 ? '.\perl' :
32 './perl');
68dc0745 33
8df0e0ed 34END {
35 # On VMS, environment variable changes are peristent after perl exits
36 delete $ENV{'FOO'} if $Is_VMS;
37}
38
39e571d4 39eval '$ENV{"FOO"} = "hi there";'; # check that ENV is inited inside eval
26f6e342 40# cmd.exe will echo 'variable=value' but 4nt will echo just the value
41# -- Nikola Knezevic
b2978f4e 42if ($Is_MSWin32) { like `set FOO`, qr/^(?:FOO=)?hi there$/; }
be708cc0 43elsif ($Is_MacOS) { ok "1 # skipped", 1; }
b2978f4e 44elsif ($Is_VMS) { is `write sys\$output f\$trnlnm("FOO")`, "hi there\n"; }
45else { is `echo \$FOO`, "hi there\n"; }
8d063cd8 46
bf38876a 47unlink 'ajslkdfpqjsjfk';
8d063cd8 48$! = 0;
90ce63d5 49open(FOO,'ajslkdfpqjsjfk');
b2978f4e 50isnt($!, 0);
90ce63d5 51close FOO; # just mention it, squelch used-only-once
8d063cd8 52
b2978f4e 53SKIP: {
54 skip('SIGINT not safe on this platform', 5)
55 if $Is_MSWin32 || $Is_NetWare || $Is_Dos || $Is_MPE || $Is_MacOS;
c363d00c 56 # the next tests are done in a subprocess because sh spits out a
57 # newline onto stderr when a child process kills itself with SIGINT.
04fee9b5 58 # We use a pipe rather than system() because the VMS command buffer
c363d00c 59 # would overflow with a command that long.
60
61 open( CMDPIPE, "| $PERL");
62
63 print CMDPIPE <<'END';
378cc40b 64
79072805 65 $| = 1; # command buffering
378cc40b 66
b715f106 67 $SIG{"INT"} = "ok3"; kill "INT",$$; sleep 1;
68 $SIG{"INT"} = "IGNORE"; kill "INT",$$; sleep 1; print "ok 4\n";
0bee926d 69 $SIG{"INT"} = "DEFAULT"; kill "INT",$$; sleep 1; print "not ok 4\n";
79072805 70
71 sub ok3 {
72 if (($x = pop(@_)) eq "INT") {
73 print "ok 3\n";
74 }
75 else {
652ed9f8 76 print "not ok 3 ($x @_)\n";
79072805 77 }
78 }
79
80END
c363d00c 81
82 close CMDPIPE;
83
2d4fcd5e 84 open( CMDPIPE, "| $PERL");
85 print CMDPIPE <<'END';
86
87 { package X;
88 sub DESTROY {
89 kill "INT",$$;
90 }
91 }
92 sub x {
93 my $x=bless [], 'X';
94 return sub { $x };
95 }
96 $| = 1; # command buffering
97 $SIG{"INT"} = "ok5";
98 {
99 local $SIG{"INT"}=x();
100 print ""; # Needed to expose failure in 5.8.0 (why?)
101 }
102 sleep 1;
103 delete $SIG{"INT"};
104 kill "INT",$$; sleep 1;
105 sub ok5 {
106 print "ok 5\n";
107 }
108END
109 close CMDPIPE;
bb4e15c8 110 $? >>= 8 if $^O eq 'VMS'; # POSIX status hiding in 2nd byte
639cf43b 111 my $todo = ($^O eq 'os2' ? ' # TODO: EMX v0.9d_fix4 bug: wrong nibble? ' : '');
112 print $? & 0xFF ? "ok 6$todo\n" : "not ok 6$todo\n";
2d4fcd5e 113
6e592b3a 114 open(CMDPIPE, "| $PERL");
115 print CMDPIPE <<'END';
116
117 sub PVBM () { 'foo' }
118 index 'foo', PVBM;
119 my $pvbm = PVBM;
120
121 sub foo { exit 0 }
122
123 $SIG{"INT"} = $pvbm;
124 kill "INT", $$; sleep 1;
125END
126 close CMDPIPE;
127 $? >>= 8 if $^O eq 'VMS';
128 print $? ? "not ok 7\n" : "ok 7\n";
129
b2978f4e 130 curr_test(curr_test() + 5);
68dc0745 131}
a687059c 132
68dc0745 133# can we slice ENV?
134@val1 = @ENV{keys(%ENV)};
a687059c 135@val2 = values(%ENV);
b2978f4e 136is join(':',@val1), join(':',@val2);
137cmp_ok @val1, '>', 1;
90ce63d5 138
139# regex vars
140'foobarbaz' =~ /b(a)r/;
b2978f4e 141is $`, 'foo';
142is $&, 'bar';
143is $', 'baz';
144is $+, 'a';
90ce63d5 145
146# $"
147@a = qw(foo bar baz);
b2978f4e 148is "@a", "foo bar baz";
90ce63d5 149{
150 local $" = ',';
b2978f4e 151 is "@a", "foo,bar,baz";
90ce63d5 152}
a687059c 153
90ce63d5 154# $;
155%h = ();
156$h{'foo', 'bar'} = 1;
b2978f4e 157is((keys %h)[0], "foo\034bar");
90ce63d5 158{
159 local $; = 'x';
160 %h = ();
161 $h{'foo', 'bar'} = 1;
b2978f4e 162 is((keys %h)[0], 'fooxbar');
90ce63d5 163}
ed6116ce 164
90ce63d5 165# $?, $@, $$
b2978f4e 166SKIP: {
167 skip('$? + system are broken on MacPerl', 2) if $Is_MacOS;
dc459aad 168 system qq[$PERL "-I../lib" -e "use vmsish qw(hushed); exit(0)"];
b2978f4e 169 is $?, 0;
dc459aad 170 system qq[$PERL "-I../lib" -e "use vmsish qw(hushed); exit(1)"];
b2978f4e 171 isnt $?, 0;
dc459aad 172}
90ce63d5 173
174eval { die "foo\n" };
b2978f4e 175is $@, "foo\n";
90ce63d5 176
b2978f4e 177cmp_ok($$, '>', 0);
306196c3 178eval { $$++ };
b2978f4e 179like ($@, qr/^Modification of a read-only value attempted/);
90ce63d5 180
181# $^X and $0
ed37317b 182{
3e3baf6d 183 if ($^O eq 'qnx') {
7fbf1995 184 chomp($wd = `/usr/bin/fullpath -t`);
68dc0745 185 }
04fee9b5 186 elsif($Is_Cygwin || $Config{'d_procselfexe'}) {
1cab015a 187 # Cygwin turns the symlink into the real file
188 chomp($wd = `pwd`);
189 $wd =~ s#/t$##;
0409250f 190 $wd =~ /(.*)/; $wd = $1; # untaint
6178c52a 191 if ($Is_Cygwin) {
192 $wd = Cygwin::win_to_posix_path(Cygwin::posix_to_win_path($wd, 1));
193 }
1cab015a 194 }
ed344e4f 195 elsif($Is_os2) {
196 $wd = Cwd::sys_cwd();
197 }
be708cc0 198 elsif($Is_MacOS) {
199 $wd = ':';
200 }
68dc0745 201 else {
202 $wd = '.';
203 }
c363d00c 204 my $perl = ($Is_MacOS || $Is_VMS) ? $^X : "$wd/perl";
ed37317b 205 my $headmaybe = '';
6178c52a 206 my $middlemaybe = '';
ed37317b 207 my $tailmaybe = '';
68dc0745 208 $script = "$wd/show-shebang";
ed37317b 209 if ($Is_MSWin32) {
210 chomp($wd = `cd`);
8ac9c18d 211 $wd =~ s|\\|/|g;
212 $perl = "$wd/perl.exe";
213 $script = "$wd/show-shebang.bat";
ed37317b 214 $headmaybe = <<EOH ;
215\@rem ='
216\@echo off
217$perl -x \%0
218goto endofperl
219\@rem ';
220EOH
221 $tailmaybe = <<EOT ;
222
223__END__
224:endofperl
225EOT
226 }
ed344e4f 227 elsif ($Is_os2) {
228 $script = "./show-shebang";
229 }
be708cc0 230 elsif ($Is_MacOS) {
231 $script = ":show-shebang";
232 }
c363d00c 233 elsif ($Is_VMS) {
234 $script = "[]show-shebang";
be708cc0 235 }
6178c52a 236 elsif ($Is_Cygwin) {
237 $middlemaybe = <<'EOX'
238$^X = Cygwin::win_to_posix_path(Cygwin::posix_to_win_path($^X, 1));
239$0 = Cygwin::win_to_posix_path(Cygwin::posix_to_win_path($0, 1));
240EOX
241 }
a1a0e61e 242 if ($^O eq 'os390' or $^O eq 'posix-bc' or $^O eq 'vmesa') { # no shebang
9d116dd7 243 $headmaybe = <<EOH ;
244 eval 'exec ./perl -S \$0 \${1+"\$\@"}'
245 if 0;
246EOH
247 }
2eecd615 248 $s1 = "\$^X is $perl, \$0 is $script\n";
0409250f 249 ok open(SCRIPT, ">$script") or diag "Can't write to $script: $!";
b2978f4e 250 ok print(SCRIPT $headmaybe . <<EOB . $middlemaybe . <<'EOF' . $tailmaybe) or diag $!;
774d564b 251#!$wd/perl
252EOB
90ce63d5 253print "\$^X is $^X, \$0 is $0\n";
254EOF
b2978f4e 255 ok close(SCRIPT) or diag $!;
256 ok chmod(0755, $script) or diag $!;
c363d00c 257 $_ = ($Is_MacOS || $Is_VMS) ? `$perl $script` : `$script`;
ed344e4f 258 s/\.exe//i if $Is_Dos or $Is_Cygwin or $Is_os2;
dbc1d986 259 s{./$script}{$script} if $Is_BeOS; # revert BeOS execvp() side-effect
68dc0745 260 s{\bminiperl\b}{perl}; # so that test doesn't fail with miniperl
ed37317b 261 s{is perl}{is $perl}; # for systems where $^X is only a basename
a6c40364 262 s{\\}{/}g;
b2978f4e 263 if ($Is_MSWin32 || $Is_os2) {
264 is uc $_, uc $s1;
265 } else {
266 is $_, $s1;
267 }
ed37317b 268 $_ = `$perl $script`;
4bbb7126 269 s/\.exe//i if $Is_Dos or $Is_os2 or $Is_Cygwin;
dbc1d986 270 s{./$perl}{$perl} if $Is_BeOS; # revert BeOS execvp() side-effect
a6c40364 271 s{\\}{/}g;
b2978f4e 272 if ($Is_MSWin32 || $Is_os2) {
273 is uc $_, uc $s1;
274 } else {
275 is $_, $s1;
276 }
277 ok unlink($script) or diag $!;
68dc0745 278}
ed6116ce 279
90ce63d5 280# $], $^O, $^T
b2978f4e 281cmp_ok $], '>=', 5.00319;
0bee926d 282ok $^O;
b2978f4e 283cmp_ok $^T, '>', 850000000;
66b1d557 284
881ddac4 285# Test change 25062 is working
286my $orig_osname = $^O;
287{
288local $^I = '.bak';
b2978f4e 289is $^O, $orig_osname, 'Assigning $^I does not clobber $^O';
881ddac4 290}
291$^O = $orig_osname;
292
b2978f4e 293SKIP: {
294 skip("%ENV manipulations fail or aren't safe on $^O", 4)
295 if $Is_VMS || $Is_Dos || $Is_MacOS;
296
297 SKIP: {
298 skip("clearing \%ENV is not safe when running under valgrind")
299 if $ENV{PERL_VALGRIND};
300
da51b73c 301 $PATH = $ENV{PATH};
302 $PDL = $ENV{PERL_DESTRUCT_LEVEL} || 0;
303 $ENV{foo} = "bar";
304 %ENV = ();
305 $ENV{PATH} = $PATH;
306 $ENV{PERL_DESTRUCT_LEVEL} = $PDL || 0;
b2978f4e 307 if ($Is_MSWin32) {
308 is `set foo 2>NUL`, "";
309 } else {
310 is `echo \$foo`, "\n";
311 }
da51b73c 312 }
3e3baf6d 313
ec00bdd8 314 $ENV{__NoNeSuCh} = "foo";
3e3baf6d 315 $0 = "bar";
26f6e342 316# cmd.exe will echo 'variable=value' but 4nt will echo just the value
317# -- Nikola Knezevic
b2978f4e 318 if ($Is_MSWin32) {
319 like `set __NoNeSuCh`, qr/^(?:__NoNeSuCh=)?foo$/;
320 } else {
321 is `echo \$__NoNeSuCh`, "foo\n";
322 }
323 SKIP: {
324 skip("\$0 check only on Linux and FreeBSD", 2)
325 unless $^O =~ /^(linux|freebsd)$/
326 && open CMDLINE, "/proc/$$/cmdline";
327
09fdc078 328 chomp(my $line = scalar <CMDLINE>);
329 my $me = (split /\0/, $line)[0];
b2978f4e 330 is $me, $0, 'altering $0 is effective (testing with /proc/)';
09fdc078 331 close CMDLINE;
ecce83c2 332 # perlbug #22811
333 my $mydollarzero = sub {
334 my($arg) = shift;
335 $0 = $arg if defined $arg;
fbd3c14b 336 # In FreeBSD the ps -o command= will cause
337 # an empty header line, grab only the last line.
338 my $ps = (`ps -o command= -p $$`)[-1];
ecce83c2 339 return if $?;
340 chomp $ps;
341 printf "# 0[%s]ps[%s]\n", $0, $ps;
342 $ps;
343 };
344 my $ps = $mydollarzero->("x");
e26ae24d 345 ok(!$ps # we allow that something goes wrong with the ps command
80bca1b4 346 # In Linux 2.4 we would get an exact match ($ps eq 'x') but
347 # in Linux 2.2 there seems to be something funny going on:
348 # it seems as if the original length of the argv[] would
349 # be stored in the proc struct and then used by ps(1),
350 # no matter what characters we use to pad the argv[].
351 # (And if we use \0:s, they are shown as spaces.) Sigh.
352 || $ps =~ /^x\s*$/
6a4647a3 353 # FreeBSD cannot get rid of both the leading "perl :"
354 # and the trailing " (perl)": some FreeBSD versions
355 # can get rid of the first one.
d2e0b13f 356 || ($^O eq 'freebsd' && $ps =~ m/^(?:perl: )?x(?: \(perl\))?$/),
e26ae24d 357 'altering $0 is effective (testing with `ps`)');
09fdc078 358 }
66b1d557 359}
3e3baf6d 360
c7213721 361{
a45269de 362 my $ok = 1;
363 my $warn = '';
ae8ade65 364 local $SIG{'__WARN__'} = sub { $ok = 0; $warn = join '', @_; $warn =~ s/\n$//; };
78987ded 365 $! = undef;
b2978f4e 366 local $TODO = $Is_VMS ? "'\$!=undef' does throw a warning" : '';
367 ok($ok, $warn);
78987ded 368}
369
902173a3 370# test case-insignificance of %ENV (these tests must be enabled only
371# when perl is compiled with -DENV_IS_CASELESS)
b2978f4e 372SKIP: {
373 skip('no caseless %ENV support', 4) unless $Is_MSWin32 || $Is_NetWare;
374
902173a3 375 %ENV = ();
376 $ENV{'Foo'} = 'bar';
377 $ENV{'fOo'} = 'baz';
b2978f4e 378 is scalar(keys(%ENV)), 1;
379 ok exists $ENV{'FOo'};
380 is delete $ENV{'foO'}, 'baz';
381 is scalar(keys(%ENV)), 0;
902173a3 382}
d2c93421 383
b2978f4e 384SKIP: {
385 skip ("miniperl can't rely on loading %Errno", 2) if $Is_miniperl;
126c71c8 386 no warnings 'void';
387
d2c93421 388# Make sure Errno hasn't been prematurely autoloaded
389
b79f7545 390 ok !keys %Errno::;
d2c93421 391
392# Test auto-loading of Errno when %! is used
393
126c71c8 394 ok scalar eval q{
395 %!;
396 defined %Errno::;
397 }, $@;
398}
d2c93421 399
b2978f4e 400SKIP: {
401 skip ("miniperl can't rely on loading %Errno") if $Is_miniperl;
43651d81 402 # Make sure that Errno loading doesn't clobber $!
d2c93421 403
43651d81 404 undef %Errno::;
405 delete $INC{"Errno.pm"};
d2c93421 406
43651d81 407 open(FOO, "nonesuch"); # Generate ENOENT
408 my %errs = %{"!"}; # Cause Errno.pm to be loaded at run-time
409 ok ${"!"}{ENOENT};
410}
a4268c0a 411
b2978f4e 412is $^S, 0;
413eval { is $^S,1 };
b0e6f864 414eval " BEGIN { ok ! defined \$^S } ";
b2978f4e 415is $^S, 0;
7c36658b 416
0409250f 417my $taint = ${^TAINT};
418is ${^TAINT}, $taint;
7c36658b 419eval { ${^TAINT} = 1 };
0409250f 420is ${^TAINT}, $taint;
9aa702ec 421
422# 5.6.1 had a bug: @+ and @- were not properly interpolated
423# into double-quoted strings
424# 20020414 mjd-perl-patch+@plover.com
b64ebf53 425"I like pie" =~ /(I) (like) (pie)/;
b2978f4e 426is "@-", "0 0 2 7";
427is "@+", "10 1 6 10";
9aa702ec 428
f28098ff 429# Tests for the magic get of $\
430{
431 my $ok = 0;
432 # [perl #19330]
433 {
434 local $\ = undef;
435 $\++; $\++;
436 $ok = $\ eq 2;
437 }
438 ok $ok;
439 $ok = 0;
440 {
441 local $\ = "a\0b";
442 $ok = "a$\b" eq "aa\0bb";
443 }
444 ok $ok;
445}
547d1dd8 446
447# Test for bug [perl #27839]
448{
449 my $x;
450 sub f {
451 "abc" =~ /(.)./;
452 $x = "@+";
453 return @+;
454 };
455 my @y = f();
b2978f4e 456 is $x, "@y", "return a magic array ($x) vs (@y)";
547d1dd8 457}
4c9140ed 458
459# Test for bug [perl #36434]
b2978f4e 460# Can not do this test on VMS, EPOC, and SYMBIAN according to comments
461# in mg.c/Perl_magic_clear_all_env()
462SKIP: {
463 skip('Can\'t make assignment to \%ENV on this system', 3) if $Is_VMS;
464
4c9140ed 465 local @ISA;
466 local %ENV;
e1a479c5 467 # This used to be __PACKAGE__, but that causes recursive
468 # inheritance, which is detected earlier now and broke
469 # this test
470 eval { push @ISA, __FILE__ };
b2978f4e 471 is $@, '', 'Push a constant on a magic array';
4c9140ed 472 $@ and print "# $@";
473 eval { %ENV = (PATH => __PACKAGE__) };
b2978f4e 474 is $@, '', 'Assign a constant to a magic hash';
d8084ca5 475 $@ and print "# $@";
476 eval { my %h = qw(A B); %ENV = (PATH => (keys %h)[0]) };
b2978f4e 477 is $@, '', 'Assign a shared key to a magic hash';
4c9140ed 478 $@ and print "# $@";
479}
179c85a2 480
481# Tests for Perl_magic_clearsig
482foreach my $sig (qw(__WARN__ INT)) {
483 $SIG{$sig} = lc $sig;
484 is $SIG{$sig}, 'main::' . lc $sig, "Can assign to $sig";
485 is delete $SIG{$sig}, 'main::' . lc $sig, "Can delete from $sig";
486 is $SIG{$sig}, undef, "$sig is now gone";
487 is delete $SIG{$sig}, undef, "$sig remains gone";
488}
489
490# And now one which doesn't exist;
491{
492 no warnings 'signal';
493 $SIG{HUNGRY} = 'mmm, pie';
494}
495is $SIG{HUNGRY}, 'mmm, pie', 'Can assign to HUNGRY';
496is delete $SIG{HUNGRY}, 'mmm, pie', 'Can delete from HUNGRY';
497is $SIG{HUNGRY}, undef, "HUNGRY is now gone";
498is delete $SIG{HUNGRY}, undef, "HUNGRY remains gone";
499
500# Test deleting signals that we never set
c8be058c 501foreach my $sig (qw(__DIE__ _BOGUS_HOOK KILL THIRSTY)) {
179c85a2 502 is $SIG{$sig}, undef, "$sig is not present";
503 is delete $SIG{$sig}, undef, "delete of $sig returns undef";
504}