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