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