This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
move the fresh_perl() tests above the environment tests
[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
4c0f30d6 15plan (tests => 85);
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
NC
23$Is_MPE = $^O eq 'mpeix';
24$Is_miniperl = $ENV{PERL_CORE_MINITEST};
dbc1d986 25$Is_BeOS = $^O eq 'beos';
be708cc0 26
c8d62b71
RGS
27$PERL = $ENV{PERL}
28 || ($Is_NetWare ? 'perl' :
7b903762 29 $Is_VMS ? $^X :
c8d62b71
RGS
30 $Is_MSWin32 ? '.\perl' :
31 './perl');
68dc0745 32
8df0e0ed
JM
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
NK
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
NC
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
NC
51SKIP: {
52 skip('SIGINT not safe on this platform', 5)
7b903762 53 if $Is_MSWin32 || $Is_NetWare || $Is_Dos || $Is_MPE;
c363d00c
CB
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
CB
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
MB
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
LW
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
LW
75 }
76 }
77
78END
c363d00c
CB
79
80 close CMDPIPE;
81
2d4fcd5e
AJ
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
IZ
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
BM
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
PP
131# can we slice ENV?
132@val1 = @ENV{keys(%ENV)};
a687059c 133@val2 = values(%ENV);
b2978f4e
NC
134is join(':',@val1), join(':',@val2);
135cmp_ok @val1, '>', 1;
90ce63d5
RS
136
137# regex vars
138'foobarbaz' =~ /b(a)r/;
b2978f4e
NC
139is $`, 'foo';
140is $&, 'bar';
141is $', 'baz';
142is $+, 'a';
90ce63d5
RS
143
144# $"
145@a = qw(foo bar baz);
b2978f4e 146is "@a", "foo bar baz";
90ce63d5
RS
147{
148 local $" = ',';
b2978f4e 149 is "@a", "foo,bar,baz";
90ce63d5 150}
a687059c 151
90ce63d5
RS
152# $;
153%h = ();
154$h{'foo', 'bar'} = 1;
b2978f4e 155is((keys %h)[0], "foo\034bar");
90ce63d5
RS
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
RGS
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
RS
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
RS
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
FE
182 # Cygwin turns the symlink into the real file
183 chomp($wd = `pwd`);
184 $wd =~ s#/t$##;
0409250f 185 $wd =~ /(.*)/; $wd = $1; # untaint
6178c52a
JH
186 if ($Is_Cygwin) {
187 $wd = Cygwin::win_to_posix_path(Cygwin::posix_to_win_path($wd, 1));
188 }
1cab015a 189 }
ed344e4f
IZ
190 elsif($Is_os2) {
191 $wd = Cwd::sys_cwd();
192 }
68dc0745
PP
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
TB
201 if ($Is_MSWin32) {
202 chomp($wd = `cd`);
8ac9c18d
GS
203 $wd =~ s|\\|/|g;
204 $perl = "$wd/perl.exe";
205 $script = "$wd/show-shebang.bat";
ed37317b
TB
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
IZ
219 elsif ($Is_os2) {
220 $script = "./show-shebang";
221 }
c363d00c
CB
222 elsif ($Is_VMS) {
223 $script = "[]show-shebang";
be708cc0 224 }
6178c52a
JH
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
JH
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
PP
240#!$wd/perl
241EOB
90ce63d5
RS
242print "\$^X is $^X, \$0 is $0\n";
243EOF
b2978f4e
NC
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
NC
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
NC
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
SH
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
SH
279}
280$^O = $orig_osname;
281
065144c6
TC
282{
283 #RT #72422
284 foreach my $p (0, 1) {
285 fresh_perl_is(<<"EOP", '2 4 8', undef, "test \$^P = $p");
286\$DB::single = 2;
287\$DB::trace = 4;
288\$DB::signal = 8;
289\$^P = $p;
290print "\$DB::single \$DB::trace \$DB::signal";
291EOP
292 }
293}
294
b2978f4e
NC
295SKIP: {
296 skip("%ENV manipulations fail or aren't safe on $^O", 4)
7b903762 297 if $Is_VMS || $Is_Dos;
b2978f4e
NC
298
299 SKIP: {
300 skip("clearing \%ENV is not safe when running under valgrind")
301 if $ENV{PERL_VALGRIND};
302
da51b73c
MHM
303 $PATH = $ENV{PATH};
304 $PDL = $ENV{PERL_DESTRUCT_LEVEL} || 0;
305 $ENV{foo} = "bar";
306 %ENV = ();
307 $ENV{PATH} = $PATH;
308 $ENV{PERL_DESTRUCT_LEVEL} = $PDL || 0;
b2978f4e
NC
309 if ($Is_MSWin32) {
310 is `set foo 2>NUL`, "";
311 } else {
312 is `echo \$foo`, "\n";
313 }
da51b73c 314 }
3e3baf6d 315
ec00bdd8 316 $ENV{__NoNeSuCh} = "foo";
3e3baf6d 317 $0 = "bar";
26f6e342
NK
318# cmd.exe will echo 'variable=value' but 4nt will echo just the value
319# -- Nikola Knezevic
b2978f4e
NC
320 if ($Is_MSWin32) {
321 like `set __NoNeSuCh`, qr/^(?:__NoNeSuCh=)?foo$/;
322 } else {
323 is `echo \$__NoNeSuCh`, "foo\n";
324 }
325 SKIP: {
326 skip("\$0 check only on Linux and FreeBSD", 2)
327 unless $^O =~ /^(linux|freebsd)$/
328 && open CMDLINE, "/proc/$$/cmdline";
329
09fdc078
SR
330 chomp(my $line = scalar <CMDLINE>);
331 my $me = (split /\0/, $line)[0];
b2978f4e 332 is $me, $0, 'altering $0 is effective (testing with /proc/)';
09fdc078 333 close CMDLINE;
ecce83c2
JH
334 # perlbug #22811
335 my $mydollarzero = sub {
336 my($arg) = shift;
337 $0 = $arg if defined $arg;
fbd3c14b
SR
338 # In FreeBSD the ps -o command= will cause
339 # an empty header line, grab only the last line.
340 my $ps = (`ps -o command= -p $$`)[-1];
ecce83c2
JH
341 return if $?;
342 chomp $ps;
343 printf "# 0[%s]ps[%s]\n", $0, $ps;
344 $ps;
345 };
346 my $ps = $mydollarzero->("x");
e26ae24d 347 ok(!$ps # we allow that something goes wrong with the ps command
80bca1b4
JH
348 # In Linux 2.4 we would get an exact match ($ps eq 'x') but
349 # in Linux 2.2 there seems to be something funny going on:
350 # it seems as if the original length of the argv[] would
351 # be stored in the proc struct and then used by ps(1),
352 # no matter what characters we use to pad the argv[].
353 # (And if we use \0:s, they are shown as spaces.) Sigh.
354 || $ps =~ /^x\s*$/
6a4647a3
JH
355 # FreeBSD cannot get rid of both the leading "perl :"
356 # and the trailing " (perl)": some FreeBSD versions
357 # can get rid of the first one.
d2e0b13f 358 || ($^O eq 'freebsd' && $ps =~ m/^(?:perl: )?x(?: \(perl\))?$/),
e26ae24d 359 'altering $0 is effective (testing with `ps`)');
09fdc078 360 }
66b1d557 361}
3e3baf6d 362
7636ea95
AB
363# Check that assigning to $0 on Linux sets the process name with both
364# argv[0] assignment and by calling prctl()
365{
366 SKIP: {
367 skip "We don't have prctl() here", 2 unless $Config{d_prctl_set_name};
368
369 # We don't really need these tests. prctl() is tested in the
370 # Kernel, but test it anyway for our sanity. If something doesn't
371 # work (like if the system doesn't have a ps(1) for whatever
372 # reason) just bail out gracefully.
373 my $maybe_ps = sub {
374 my ($cmd) = @_;
375 local ($?, $!);
376
377 no warnings;
378 my $res = `$cmd`;
379 skip "Couldn't shell out to `$cmd', returned code $?", 2 if $?;
380 return $res;
381 };
382
383 my $name = "Good Morning, Dave";
384 $0 = $name;
385
386 chomp(my $argv0 = $maybe_ps->("ps h $$"));
387 chomp(my $prctl = $maybe_ps->("ps hc $$"));
388
389 like($argv0, $name, "Set process name through argv[0] ($argv0)");
390 like($prctl, substr($name, 0, 15), "Set process name through prctl() ($prctl)");
391 }
392}
393
c7213721 394{
a45269de
MS
395 my $ok = 1;
396 my $warn = '';
ae8ade65 397 local $SIG{'__WARN__'} = sub { $ok = 0; $warn = join '', @_; $warn =~ s/\n$//; };
78987ded 398 $! = undef;
b2978f4e
NC
399 local $TODO = $Is_VMS ? "'\$!=undef' does throw a warning" : '';
400 ok($ok, $warn);
78987ded
HS
401}
402
902173a3
GS
403# test case-insignificance of %ENV (these tests must be enabled only
404# when perl is compiled with -DENV_IS_CASELESS)
b2978f4e
NC
405SKIP: {
406 skip('no caseless %ENV support', 4) unless $Is_MSWin32 || $Is_NetWare;
407
902173a3
GS
408 %ENV = ();
409 $ENV{'Foo'} = 'bar';
410 $ENV{'fOo'} = 'baz';
b2978f4e
NC
411 is scalar(keys(%ENV)), 1;
412 ok exists $ENV{'FOo'};
413 is delete $ENV{'foO'}, 'baz';
414 is scalar(keys(%ENV)), 0;
902173a3 415}
d2c93421 416
b2978f4e
NC
417SKIP: {
418 skip ("miniperl can't rely on loading %Errno", 2) if $Is_miniperl;
126c71c8
YST
419 no warnings 'void';
420
d2c93421
RH
421# Make sure Errno hasn't been prematurely autoloaded
422
b79f7545 423 ok !keys %Errno::;
d2c93421
RH
424
425# Test auto-loading of Errno when %! is used
426
126c71c8
YST
427 ok scalar eval q{
428 %!;
902fde96 429 scalar %Errno::;
126c71c8
YST
430 }, $@;
431}
d2c93421 432
b2978f4e
NC
433SKIP: {
434 skip ("miniperl can't rely on loading %Errno") if $Is_miniperl;
43651d81 435 # Make sure that Errno loading doesn't clobber $!
d2c93421 436
43651d81
NC
437 undef %Errno::;
438 delete $INC{"Errno.pm"};
d2c93421 439
43651d81
NC
440 open(FOO, "nonesuch"); # Generate ENOENT
441 my %errs = %{"!"}; # Cause Errno.pm to be loaded at run-time
442 ok ${"!"}{ENOENT};
443}
a4268c0a 444
b2978f4e
NC
445is $^S, 0;
446eval { is $^S,1 };
b0e6f864 447eval " BEGIN { ok ! defined \$^S } ";
b2978f4e 448is $^S, 0;
7c36658b 449
0409250f
RGS
450my $taint = ${^TAINT};
451is ${^TAINT}, $taint;
7c36658b 452eval { ${^TAINT} = 1 };
0409250f 453is ${^TAINT}, $taint;
9aa702ec
MJD
454
455# 5.6.1 had a bug: @+ and @- were not properly interpolated
456# into double-quoted strings
457# 20020414 mjd-perl-patch+@plover.com
b64ebf53 458"I like pie" =~ /(I) (like) (pie)/;
b2978f4e
NC
459is "@-", "0 0 2 7";
460is "@+", "10 1 6 10";
9aa702ec 461
f28098ff
RGS
462# Tests for the magic get of $\
463{
464 my $ok = 0;
465 # [perl #19330]
466 {
467 local $\ = undef;
468 $\++; $\++;
469 $ok = $\ eq 2;
470 }
471 ok $ok;
472 $ok = 0;
473 {
474 local $\ = "a\0b";
475 $ok = "a$\b" eq "aa\0bb";
476 }
477 ok $ok;
478}
547d1dd8
HS
479
480# Test for bug [perl #27839]
481{
482 my $x;
483 sub f {
484 "abc" =~ /(.)./;
485 $x = "@+";
486 return @+;
487 };
2d961f6d 488 "pqrstuvwxyz" =~ /..(....)../; # prime @+ etc in this scope
547d1dd8 489 my @y = f();
b2978f4e 490 is $x, "@y", "return a magic array ($x) vs (@y)";
fd69380d
DM
491
492 sub f2 {
493 "abc" =~ /(?<foo>.)./;
494 my @h = %+;
495 $x = "@h";
496 return %+;
497 };
498 @y = f();
499 is $x, "@y", "return a magic hash ($x) vs (@y)";
547d1dd8 500}
4c9140ed
RGS
501
502# Test for bug [perl #36434]
b2978f4e
NC
503# Can not do this test on VMS, EPOC, and SYMBIAN according to comments
504# in mg.c/Perl_magic_clear_all_env()
505SKIP: {
506 skip('Can\'t make assignment to \%ENV on this system', 3) if $Is_VMS;
507
4c9140ed
RGS
508 local @ISA;
509 local %ENV;
e1a479c5
BB
510 # This used to be __PACKAGE__, but that causes recursive
511 # inheritance, which is detected earlier now and broke
512 # this test
513 eval { push @ISA, __FILE__ };
b2978f4e 514 is $@, '', 'Push a constant on a magic array';
4c9140ed
RGS
515 $@ and print "# $@";
516 eval { %ENV = (PATH => __PACKAGE__) };
b2978f4e 517 is $@, '', 'Assign a constant to a magic hash';
d8084ca5
DM
518 $@ and print "# $@";
519 eval { my %h = qw(A B); %ENV = (PATH => (keys %h)[0]) };
b2978f4e 520 is $@, '', 'Assign a shared key to a magic hash';
4c9140ed
RGS
521 $@ and print "# $@";
522}
179c85a2
NC
523
524# Tests for Perl_magic_clearsig
525foreach my $sig (qw(__WARN__ INT)) {
526 $SIG{$sig} = lc $sig;
527 is $SIG{$sig}, 'main::' . lc $sig, "Can assign to $sig";
528 is delete $SIG{$sig}, 'main::' . lc $sig, "Can delete from $sig";
529 is $SIG{$sig}, undef, "$sig is now gone";
530 is delete $SIG{$sig}, undef, "$sig remains gone";
531}
532
533# And now one which doesn't exist;
534{
535 no warnings 'signal';
536 $SIG{HUNGRY} = 'mmm, pie';
537}
538is $SIG{HUNGRY}, 'mmm, pie', 'Can assign to HUNGRY';
539is delete $SIG{HUNGRY}, 'mmm, pie', 'Can delete from HUNGRY';
540is $SIG{HUNGRY}, undef, "HUNGRY is now gone";
541is delete $SIG{HUNGRY}, undef, "HUNGRY remains gone";
542
543# Test deleting signals that we never set
c8be058c 544foreach my $sig (qw(__DIE__ _BOGUS_HOOK KILL THIRSTY)) {
179c85a2
NC
545 is $SIG{$sig}, undef, "$sig is not present";
546 is delete $SIG{$sig}, undef, "delete of $sig returns undef";
547}
be1cf43c
NC
548
549{
550 $! = 9999;
551 is int $!, 9999, q{[perl #72850] Core dump in bleadperl from perl -e '$! = 9999; $a = $!;'};
552
553}
4c0f30d6 554