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