This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Merge branch 'smoke-me/jkeenan/gh-18040-parts-inc-mess' into blead
[perl5.git] / t / op / magic.t
CommitLineData
8d063cd8
LW
1#!./perl
2
90ce63d5 3BEGIN {
90ce63d5
RS
4 $| = 1;
5 chdir 't' if -d 't';
5e9f035f 6 require './test.pl';
624c42e2 7 set_up_inc( '../lib' );
d88d17cb 8 plan (tests => 192); # some tests are run in BEGIN block
5e9f035f
FC
9}
10
11# Test that defined() returns true for magic variables created on the fly,
12# even before they have been created.
13# This must come first, even before turning on warnings or setting up
14# $SIG{__WARN__}, to avoid invalidating the tests. warnings.pm currently
15# does not mention any special variables, but that could easily change.
16BEGIN {
811a7562 17 # not available in miniperl
b82b06b8 18 my %non_mini = map { $_ => 1 } qw(+ - [);
5e9f035f
FC
19 for (qw(
20 SIG ^OPEN ^TAINT ^UNICODE ^UTF8LOCALE ^WARNING_BITS 1 2 3 4 5 6 7 8
23496c6e 21 9 42 & ` ' : ? ! _ - [ ^ ~ = % . ( ) < > \ / $ | + ; ] ^A ^C ^D
b4155db2 22 ^E ^F ^H ^I ^L ^N ^O ^P ^S ^T ^V ^W ^UTF8CACHE ::12345 main::98732
8561ea1d 23 ^LAST_FH
5e9f035f
FC
24 )) {
25 my $v = $_;
26 # avoid using any global vars here:
27 if ($v =~ s/^\^(?=.)//) {
28 for(substr $v, 0, 1) {
73676ccb 29 $_ = chr(utf8::native_to_unicode(ord($_)) - 64);
5e9f035f
FC
30 }
31 }
811a7562
TC
32 SKIP:
33 {
34 skip_if_miniperl("the module for *$_ may not be available in "
35 . "miniperl", 1) if $non_mini{$_};
36 ok defined *$v, "*$_ appears to be defined at the outset";
37 }
5e9f035f
FC
38 }
39}
40
41# This must be in a separate BEGIN block, as the mere mention of ${^TAINT}
42# will invalidate the test for it.
43BEGIN {
0409250f 44 $ENV{PATH} = '/bin' if ${^TAINT};
774d564b 45 $SIG{__WARN__} = sub { die "Dying on warning: ", @_ };
90ce63d5 46}
8d063cd8 47
9f1b1f2d 48use warnings;
04fee9b5 49use Config;
9f1b1f2d 50
0bee926d 51
43651d81
NC
52$Is_MSWin32 = $^O eq 'MSWin32';
53$Is_NetWare = $^O eq 'NetWare';
54$Is_VMS = $^O eq 'VMS';
55$Is_Dos = $^O eq 'dos';
56$Is_os2 = $^O eq 'os2';
57$Is_Cygwin = $^O eq 'cygwin';
be708cc0 58
463c96de 59$PERL =
60acf818
MB
60 ($Is_NetWare ? 'perl' :
61 $Is_VMS ? $^X :
62 $Is_MSWin32 ? '.\perl' :
63 './perl');
68dc0745 64
624c42e2 65
613c63b4
CS
66sub env_is {
67 my ($key, $val, $desc) = @_;
e2e1d5ce
TC
68
69 use open IN => ":raw";
613c63b4
CS
70 if ($Is_MSWin32) {
71 # cmd.exe will echo 'variable=value' but 4nt will echo just the value
72 # -- Nikola Knezevic
1070c8d6
SH
73 require Win32;
74 my $cp = Win32::GetConsoleOutputCP();
75 Win32::SetConsoleOutputCP(Win32::GetACP());
615e2f77 76 (my $set = `set $key 2>nul`) =~ s/\r\n$/\n/;
1070c8d6 77 Win32::SetConsoleOutputCP($cp);
5ceaabe8 78 like $set, qr/^(?:\Q$key\E=)?\Q$val\E$/, $desc;
f8c6801b 79 } elsif ($Is_VMS) {
95725928
CB
80 my $eqv = `write sys\$output f\$trnlnm("\Q$key\E")`;
81 # A single null byte in the equivalence string means
82 # an undef value for Perl, so mimic that here.
83 $eqv = "\n" if length($eqv) == 2 and $eqv eq "\000\n";
84 is $eqv, "$val\n", $desc;
613c63b4 85 } else {
ef41c525
BF
86 my @env = `env`;
87 SKIP: {
88 skip("env doesn't work on this android", 1) if !@env && $^O =~ /android/;
89 chomp (my @env = grep { s/^$key=// } @env);
90 is "@env", $val, $desc;
91 }
613c63b4
CS
92 }
93}
94
8df0e0ed
JM
95END {
96 # On VMS, environment variable changes are peristent after perl exits
f8c6801b
CB
97 if ($Is_VMS) {
98 delete $ENV{'FOO'};
99 delete $ENV{'__NoNeSuCh'};
95725928 100 delete $ENV{'__NoNeSuCh2'};
f8c6801b 101 }
8df0e0ed
JM
102}
103
39e571d4 104eval '$ENV{"FOO"} = "hi there";'; # check that ENV is inited inside eval
26f6e342
NK
105# cmd.exe will echo 'variable=value' but 4nt will echo just the value
106# -- Nikola Knezevic
43e5ab2e 107if ($Is_MSWin32) { like `set FOO`, qr/^(?:FOO=)?hi there$/m; }
b2978f4e
NC
108elsif ($Is_VMS) { is `write sys\$output f\$trnlnm("FOO")`, "hi there\n"; }
109else { is `echo \$FOO`, "hi there\n"; }
8d063cd8 110
ba2406eb 111unlink_all 'ajslkdfpqjsjfk';
8d063cd8 112$! = 0;
90ce63d5 113open(FOO,'ajslkdfpqjsjfk');
18b6b0bf 114isnt($!, 0, "Unlinked file can't be opened");
90ce63d5 115close FOO; # just mention it, squelch used-only-once
8d063cd8 116
b2978f4e
NC
117SKIP: {
118 skip('SIGINT not safe on this platform', 5)
b5afd346 119 if $Is_MSWin32 || $Is_NetWare || $Is_Dos;
c363d00c
CB
120 # the next tests are done in a subprocess because sh spits out a
121 # newline onto stderr when a child process kills itself with SIGINT.
04fee9b5 122 # We use a pipe rather than system() because the VMS command buffer
c363d00c
CB
123 # would overflow with a command that long.
124
5e9f035f
FC
125 # For easy interpolation of test numbers:
126 $next_test = curr_test() - 1;
127 sub TIEARRAY {bless[]}
128 sub FETCH { $next_test + pop }
129 tie my @tn, __PACKAGE__;
130
0ef4ffa9 131 open( CMDPIPE, "|-", $PERL);
c363d00c 132
5e9f035f 133 print CMDPIPE "\$t1 = $tn[1]; \$t2 = $tn[2];\n", <<'END';
378cc40b 134
79072805 135 $| = 1; # command buffering
378cc40b 136
5e9f035f
FC
137 $SIG{"INT"} = "ok1"; kill "INT",$$; sleep 1;
138 $SIG{"INT"} = "IGNORE"; kill "INT",$$; sleep 1; print "ok $t2\n";
139 $SIG{"INT"} = "DEFAULT"; kill "INT",$$; sleep 1; print" not ok $t2\n";
79072805 140
5e9f035f 141 sub ok1 {
79072805 142 if (($x = pop(@_)) eq "INT") {
5e9f035f 143 print "ok $t1\n";
79072805
LW
144 }
145 else {
5e9f035f 146 print "not ok $t1 ($x @_)\n";
79072805
LW
147 }
148 }
149
150END
c363d00c
CB
151
152 close CMDPIPE;
153
0ef4ffa9 154 open( CMDPIPE, "|-", $PERL);
5e9f035f 155 print CMDPIPE "\$t3 = $tn[3];\n", <<'END';
2d4fcd5e
AJ
156
157 { package X;
158 sub DESTROY {
159 kill "INT",$$;
160 }
161 }
162 sub x {
163 my $x=bless [], 'X';
164 return sub { $x };
165 }
166 $| = 1; # command buffering
5e9f035f 167 $SIG{"INT"} = "ok3";
2d4fcd5e
AJ
168 {
169 local $SIG{"INT"}=x();
170 print ""; # Needed to expose failure in 5.8.0 (why?)
171 }
172 sleep 1;
173 delete $SIG{"INT"};
174 kill "INT",$$; sleep 1;
5e9f035f
FC
175 sub ok3 {
176 print "ok $t3\n";
2d4fcd5e
AJ
177 }
178END
179 close CMDPIPE;
bb4e15c8 180 $? >>= 8 if $^O eq 'VMS'; # POSIX status hiding in 2nd byte
639cf43b 181 my $todo = ($^O eq 'os2' ? ' # TODO: EMX v0.9d_fix4 bug: wrong nibble? ' : '');
b5b6faf3 182 $todo = ($Config{usecrosscompile} ? '# TODO: Not sure whats going on here when cross-compiling' : '');
5e9f035f 183 print $? & 0xFF ? "ok $tn[4]$todo\n" : "not ok $tn[4]$todo\n";
2d4fcd5e 184
0ef4ffa9 185 open(CMDPIPE, "|-", $PERL);
6e592b3a
BM
186 print CMDPIPE <<'END';
187
188 sub PVBM () { 'foo' }
189 index 'foo', PVBM;
190 my $pvbm = PVBM;
191
192 sub foo { exit 0 }
193
194 $SIG{"INT"} = $pvbm;
195 kill "INT", $$; sleep 1;
196END
197 close CMDPIPE;
198 $? >>= 8 if $^O eq 'VMS';
5e9f035f 199 print $? ? "not ok $tn[5]\n" : "ok $tn[5]\n";
6e592b3a 200
b2978f4e 201 curr_test(curr_test() + 5);
68dc0745 202}
a687059c 203
68dc0745 204# can we slice ENV?
205@val1 = @ENV{keys(%ENV)};
a687059c 206@val2 = values(%ENV);
b2978f4e
NC
207is join(':',@val1), join(':',@val2);
208cmp_ok @val1, '>', 1;
90ce63d5 209
cf93a474
FC
210# deleting $::{ENV}
211is runperl(prog => 'delete $::{ENV}; chdir; print qq-ok\n-'), "ok\n",
212 'deleting $::{ENV}';
213
90ce63d5
RS
214# regex vars
215'foobarbaz' =~ /b(a)r/;
b2978f4e
NC
216is $`, 'foo';
217is $&, 'bar';
218is $', 'baz';
219is $+, 'a';
90ce63d5 220
a289ef89
FC
221# [perl #24237]
222for (qw < ` & ' >) {
223 fresh_perl_is
224 qq < \@$_; q "fff" =~ /(?!^)./; print "[\$$_]\\n" >,
225 "[f]\n", {},
226 "referencing \@$_ before \$$_ etc. still saws off ampersands";
227}
228
90ce63d5
RS
229# $"
230@a = qw(foo bar baz);
b2978f4e 231is "@a", "foo bar baz";
90ce63d5
RS
232{
233 local $" = ',';
b2978f4e 234 is "@a", "foo,bar,baz";
90ce63d5 235}
a687059c 236
90ce63d5
RS
237# $;
238%h = ();
239$h{'foo', 'bar'} = 1;
b2978f4e 240is((keys %h)[0], "foo\034bar");
90ce63d5
RS
241{
242 local $; = 'x';
243 %h = ();
244 $h{'foo', 'bar'} = 1;
b2978f4e 245 is((keys %h)[0], 'fooxbar');
90ce63d5 246}
ed6116ce 247
90ce63d5 248# $?, $@, $$
7b903762
RGS
249system qq[$PERL "-I../lib" -e "use vmsish qw(hushed); exit(0)"];
250is $?, 0;
251system qq[$PERL "-I../lib" -e "use vmsish qw(hushed); exit(1)"];
252isnt $?, 0;
90ce63d5
RS
253
254eval { die "foo\n" };
b2978f4e 255is $@, "foo\n";
90ce63d5 256
d456e3f4
FC
257ok !*@{HASH}, 'no %@';
258
b2978f4e 259cmp_ok($$, '>', 0);
41e07bbc 260my $pid = $$;
9cdac2a2
FC
261eval { $$ = 42 };
262is $$, 42, '$$ can be modified';
263SKIP: {
264 skip "no fork", 1 unless $Config{d_fork};
265 (my $kidpid = open my $fh, "-|") // skip "cannot fork: $!", 1;
266 if($kidpid) { # parent
267 my $kiddollars = <$fh>;
268 close $fh or die "cannot close pipe from kid proc: $!";
269 is $kiddollars, $kidpid, '$$ is reset on fork';
270 }
271 else { # child
272 print $$;
273 $::NO_ENDING = 1; # silence "Looks like you only ran..."
274 exit;
275 }
276}
41e07bbc 277$$ = $pid; # Tests below use $$
90ce63d5
RS
278
279# $^X and $0
ed37317b 280{
ae60cb46
NC
281 my $is_abs = $Config{d_procselfexe} || $Config{usekernprocpathname}
282 || $Config{usensgetexecutablepath};
3e3baf6d 283 if ($^O eq 'qnx') {
7fbf1995 284 chomp($wd = `/usr/bin/fullpath -t`);
68dc0745 285 }
b58c24b0
BF
286 elsif($^O =~ /android/) {
287 chomp($wd = `sh -c 'pwd'`);
288 }
2982a345 289 elsif($Is_Cygwin || $is_abs) {
1cab015a
EF
290 # Cygwin turns the symlink into the real file
291 chomp($wd = `pwd`);
292 $wd =~ s#/t$##;
0409250f 293 $wd =~ /(.*)/; $wd = $1; # untaint
6178c52a
JH
294 if ($Is_Cygwin) {
295 $wd = Cygwin::win_to_posix_path(Cygwin::posix_to_win_path($wd, 1));
296 }
1cab015a 297 }
ed344e4f
IZ
298 elsif($Is_os2) {
299 $wd = Cwd::sys_cwd();
300 }
68dc0745 301 else {
302 $wd = '.';
303 }
2982a345 304 my $perl = $Is_VMS || $is_abs ? $^X : "$wd/perl";
ed37317b 305 my $headmaybe = '';
6178c52a 306 my $middlemaybe = '';
ed37317b 307 my $tailmaybe = '';
68dc0745 308 $script = "$wd/show-shebang";
ed37317b
TB
309 if ($Is_MSWin32) {
310 chomp($wd = `cd`);
8ac9c18d
GS
311 $wd =~ s|\\|/|g;
312 $perl = "$wd/perl.exe";
313 $script = "$wd/show-shebang.bat";
ed37317b
TB
314 $headmaybe = <<EOH ;
315\@rem ='
316\@echo off
317$perl -x \%0
318goto endofperl
319\@rem ';
320EOH
321 $tailmaybe = <<EOT ;
322
323__END__
324:endofperl
325EOT
326 }
ed344e4f
IZ
327 elsif ($Is_os2) {
328 $script = "./show-shebang";
329 }
c363d00c
CB
330 elsif ($Is_VMS) {
331 $script = "[]show-shebang";
be708cc0 332 }
6178c52a
JH
333 elsif ($Is_Cygwin) {
334 $middlemaybe = <<'EOX'
335$^X = Cygwin::win_to_posix_path(Cygwin::posix_to_win_path($^X, 1));
336$0 = Cygwin::win_to_posix_path(Cygwin::posix_to_win_path($0, 1));
337EOX
338 }
043fec90 339 if ($^O eq 'os390' or $^O eq 'posix-bc') { # no shebang
9d116dd7
JH
340 $headmaybe = <<EOH ;
341 eval 'exec ./perl -S \$0 \${1+"\$\@"}'
342 if 0;
343EOH
344 }
2eecd615 345 $s1 = "\$^X is $perl, \$0 is $script\n";
0409250f 346 ok open(SCRIPT, ">$script") or diag "Can't write to $script: $!";
b2978f4e 347 ok print(SCRIPT $headmaybe . <<EOB . $middlemaybe . <<'EOF' . $tailmaybe) or diag $!;
0f77baf6 348#!$perl
774d564b 349EOB
90ce63d5
RS
350print "\$^X is $^X, \$0 is $0\n";
351EOF
b2978f4e
NC
352 ok close(SCRIPT) or diag $!;
353 ok chmod(0755, $script) or diag $!;
7b903762 354 $_ = $Is_VMS ? `$perl $script` : `$script`;
ed344e4f 355 s/\.exe//i if $Is_Dos or $Is_Cygwin or $Is_os2;
ed37317b 356 s{is perl}{is $perl}; # for systems where $^X is only a basename
a6c40364 357 s{\\}{/}g;
b2978f4e
NC
358 if ($Is_MSWin32 || $Is_os2) {
359 is uc $_, uc $s1;
360 } else {
d18d7217
PG
361 SKIP:
362 {
363 skip "# TODO: Hit bug posix-2058; exec does not setup argv[0] correctly." if ($^O eq "vos");
364 is $_, $s1;
365 }
b2978f4e 366 }
ed37317b 367 $_ = `$perl $script`;
4bbb7126 368 s/\.exe//i if $Is_Dos or $Is_os2 or $Is_Cygwin;
a6c40364 369 s{\\}{/}g;
b2978f4e
NC
370 if ($Is_MSWin32 || $Is_os2) {
371 is uc $_, uc $s1;
372 } else {
373 is $_, $s1;
374 }
375 ok unlink($script) or diag $!;
ba2406eb
BG
376 # CHECK
377 # Could this be replaced with:
378 # unlink_all($script);
68dc0745 379}
ed6116ce 380
90ce63d5 381# $], $^O, $^T
b2978f4e 382cmp_ok $], '>=', 5.00319;
0bee926d 383ok $^O;
b2978f4e 384cmp_ok $^T, '>', 850000000;
66b1d557 385
881ddac4
SH
386# Test change 25062 is working
387my $orig_osname = $^O;
388{
389local $^I = '.bak';
b2978f4e 390is $^O, $orig_osname, 'Assigning $^I does not clobber $^O';
881ddac4
SH
391}
392$^O = $orig_osname;
393
065144c6
TC
394{
395 #RT #72422
396 foreach my $p (0, 1) {
397 fresh_perl_is(<<"EOP", '2 4 8', undef, "test \$^P = $p");
398\$DB::single = 2;
399\$DB::trace = 4;
400\$DB::signal = 8;
401\$^P = $p;
402print "\$DB::single \$DB::trace \$DB::signal";
403EOP
404 }
405}
406
7636ea95
AB
407# Check that assigning to $0 on Linux sets the process name with both
408# argv[0] assignment and by calling prctl()
409{
410 SKIP: {
fca083c4 411 skip "We don't have prctl() here, or we're on Android", 2 unless $Config{d_prctl_set_name} && $^O ne 'android';
7636ea95
AB
412
413 # We don't really need these tests. prctl() is tested in the
414 # Kernel, but test it anyway for our sanity. If something doesn't
415 # work (like if the system doesn't have a ps(1) for whatever
416 # reason) just bail out gracefully.
417 my $maybe_ps = sub {
418 my ($cmd) = @_;
419 local ($?, $!);
420
421 no warnings;
422 my $res = `$cmd`;
3a46d15c 423 skip "Couldn't shell out to '$cmd', returned code $?", 2 if $?;
7636ea95
AB
424 return $res;
425 };
426
427 my $name = "Good Morning, Dave";
428 $0 = $name;
429
430 chomp(my $argv0 = $maybe_ps->("ps h $$"));
431 chomp(my $prctl = $maybe_ps->("ps hc $$"));
432
aaa63dae
AB
433 like($argv0, qr/$name/, "Set process name through argv[0] ($argv0)");
434 my $name_substr = substr($name, 0, 15);
435 like($prctl, qr/$name_substr/, "Set process name through prctl() ($prctl)");
7636ea95
AB
436 }
437}
438
c7213721 439{
a45269de
MS
440 my $ok = 1;
441 my $warn = '';
ae8ade65 442 local $SIG{'__WARN__'} = sub { $ok = 0; $warn = join '', @_; $warn =~ s/\n$//; };
78987ded 443 $! = undef;
b2978f4e
NC
444 local $TODO = $Is_VMS ? "'\$!=undef' does throw a warning" : '';
445 ok($ok, $warn);
78987ded
HS
446}
447
b2978f4e 448SKIP: {
d3d1232e 449 skip_if_miniperl("miniperl can't rely on loading %Errno", 2);
126c71c8
YST
450 no warnings 'void';
451
d2c93421
RH
452# Make sure Errno hasn't been prematurely autoloaded
453
b79f7545 454 ok !keys %Errno::;
d2c93421
RH
455
456# Test auto-loading of Errno when %! is used
457
126c71c8
YST
458 ok scalar eval q{
459 %!;
902fde96 460 scalar %Errno::;
126c71c8
YST
461 }, $@;
462}
d2c93421 463
b2978f4e 464SKIP: {
ffdb8bcd 465 skip_if_miniperl("miniperl can't rely on loading %Errno", 2);
43651d81 466 # Make sure that Errno loading doesn't clobber $!
d2c93421 467
43651d81
NC
468 undef %Errno::;
469 delete $INC{"Errno.pm"};
e94ea821 470 delete $::{"!"};
d2c93421 471
43651d81
NC
472 open(FOO, "nonesuch"); # Generate ENOENT
473 my %errs = %{"!"}; # Cause Errno.pm to be loaded at run-time
474 ok ${"!"}{ENOENT};
ffdb8bcd
FC
475
476 # Make sure defined(*{"!"}) before %! does not stop %! from working
477 is
478 runperl(
479 prog => 'BEGIN { defined *{q-!-} } print qq-ok\n- if tied %!',
480 ),
481 "ok\n",
482 'defined *{"!"} does not stop %! from working';
43651d81 483}
a4268c0a 484
ccdda9cb 485# Check that we don't auto-load packages
d88d17cb 486foreach (['powie::!', 'Errno']) {
88348cbd
SH
487 my ($symbol, $package) = @$_;
488 SKIP: {
489 (my $extension = $package) =~ s|::|/|g;
490 skip "$package is statically linked", 2
491 if $Config{static_ext} =~ m|\b\Q$extension\E\b|;
976bd1ee
DM
492 foreach my $scalar_first ('', '$$symbol;') {
493 my $desc = qq{Referencing %{"$symbol"}};
494 $desc .= qq{ after mentioning \${"$symbol"}} if $scalar_first;
495 $desc .= " doesn't load $package";
496
497 fresh_perl_is(<<"EOP", 0, {}, $desc);
ccdda9cb
NC
498use strict qw(vars subs);
499my \$symbol = '$symbol';
500$scalar_first;
5011 if %{\$symbol};
502print scalar %${package}::;
503EOP
976bd1ee 504 }
ccdda9cb
NC
505 }
506}
507
b2978f4e
NC
508is $^S, 0;
509eval { is $^S,1 };
b0e6f864 510eval " BEGIN { ok ! defined \$^S } ";
b2978f4e 511is $^S, 0;
7c36658b 512
0409250f
RGS
513my $taint = ${^TAINT};
514is ${^TAINT}, $taint;
7c36658b 515eval { ${^TAINT} = 1 };
0409250f 516is ${^TAINT}, $taint;
9aa702ec
MJD
517
518# 5.6.1 had a bug: @+ and @- were not properly interpolated
519# into double-quoted strings
520# 20020414 mjd-perl-patch+@plover.com
b64ebf53 521"I like pie" =~ /(I) (like) (pie)/;
b2978f4e
NC
522is "@-", "0 0 2 7";
523is "@+", "10 1 6 10";
9aa702ec 524
f28098ff
RGS
525# Tests for the magic get of $\
526{
527 my $ok = 0;
528 # [perl #19330]
529 {
530 local $\ = undef;
531 $\++; $\++;
532 $ok = $\ eq 2;
533 }
534 ok $ok;
535 $ok = 0;
536 {
537 local $\ = "a\0b";
538 $ok = "a$\b" eq "aa\0bb";
539 }
540 ok $ok;
541}
547d1dd8 542
4c9140ed 543# Test for bug [perl #36434]
822c8b4d 544# Can not do this test on VMS according to comments
b2978f4e
NC
545# in mg.c/Perl_magic_clear_all_env()
546SKIP: {
547 skip('Can\'t make assignment to \%ENV on this system', 3) if $Is_VMS;
548
4c9140ed
RGS
549 local @ISA;
550 local %ENV;
e1a479c5
BB
551 # This used to be __PACKAGE__, but that causes recursive
552 # inheritance, which is detected earlier now and broke
553 # this test
554 eval { push @ISA, __FILE__ };
b2978f4e 555 is $@, '', 'Push a constant on a magic array';
4c9140ed
RGS
556 $@ and print "# $@";
557 eval { %ENV = (PATH => __PACKAGE__) };
b2978f4e 558 is $@, '', 'Assign a constant to a magic hash';
d8084ca5
DM
559 $@ and print "# $@";
560 eval { my %h = qw(A B); %ENV = (PATH => (keys %h)[0]) };
b2978f4e 561 is $@, '', 'Assign a shared key to a magic hash';
4c9140ed
RGS
562 $@ and print "# $@";
563}
179c85a2
NC
564
565# Tests for Perl_magic_clearsig
566foreach my $sig (qw(__WARN__ INT)) {
567 $SIG{$sig} = lc $sig;
568 is $SIG{$sig}, 'main::' . lc $sig, "Can assign to $sig";
569 is delete $SIG{$sig}, 'main::' . lc $sig, "Can delete from $sig";
570 is $SIG{$sig}, undef, "$sig is now gone";
571 is delete $SIG{$sig}, undef, "$sig remains gone";
572}
573
574# And now one which doesn't exist;
575{
576 no warnings 'signal';
577 $SIG{HUNGRY} = 'mmm, pie';
578}
579is $SIG{HUNGRY}, 'mmm, pie', 'Can assign to HUNGRY';
580is delete $SIG{HUNGRY}, 'mmm, pie', 'Can delete from HUNGRY';
581is $SIG{HUNGRY}, undef, "HUNGRY is now gone";
582is delete $SIG{HUNGRY}, undef, "HUNGRY remains gone";
583
584# Test deleting signals that we never set
c8be058c 585foreach my $sig (qw(__DIE__ _BOGUS_HOOK KILL THIRSTY)) {
179c85a2
NC
586 is $SIG{$sig}, undef, "$sig is not present";
587 is delete $SIG{$sig}, undef, "delete of $sig returns undef";
588}
be1cf43c
NC
589
590{
591 $! = 9999;
592 is int $!, 9999, q{[perl #72850] Core dump in bleadperl from perl -e '$! = 9999; $a = $!;'};
593
594}
4c0f30d6 595
213084e4
FC
596# %+ %-
597SKIP: {
afb8fe69 598 skip_if_miniperl("No XS in miniperl", 2);
213084e4
FC
599 # Make sure defined(*{"+"}) before %+ does not stop %+ from working
600 is
601 runperl(
602 prog => 'BEGIN { defined *{q-+-} } print qq-ok\n- if tied %+',
603 ),
604 "ok\n",
605 'defined *{"+"} does not stop %+ from working';
606 is
607 runperl(
608 prog => 'BEGIN { defined *{q=-=} } print qq-ok\n- if tied %-',
609 ),
610 "ok\n",
611 'defined *{"-"} does not stop %- from working';
612}
613
0ea03996 614SKIP: {
d88d17cb 615 skip_if_miniperl("No XS in miniperl", 1);
0ea03996 616
d88d17cb 617 for ( [qw( %! Errno )] ) {
0ea03996
FC
618 my ($var, $mod) = @$_;
619 my $modfile = $mod =~ s|::|/|gr . ".pm";
620 fresh_perl_is
621 qq 'sub UNIVERSAL::AUTOLOAD{}
622 $mod\::foo() if 0;
623 $var;
624 print "ok\\n" if \$INC{"$modfile"}',
625 "ok\n",
626 { switches => [ '-X' ] },
627 "$var still loads $mod when stash and UNIVERSAL::AUTOLOAD exist";
628 }
629}
630
8561ea1d
FC
631# ${^LAST_FH}
632() = tell STDOUT;
633is ${^LAST_FH}, \*STDOUT, '${^LAST_FH} after tell';
634() = tell STDIN;
635is ${^LAST_FH}, \*STDIN, '${^LAST_FH} after another tell';
636{
637 my $fh = *STDOUT;
638 () = tell $fh;
639 is ${^LAST_FH}, \$fh, '${^LAST_FH} referencing lexical coercible glob';
640}
641# This also tests that ${^LAST_FH} is a weak reference:
642is ${^LAST_FH}, undef, '${^LAST_FH} is undef when PL_last_in_gv is NULL';
643
745e740c
TC
644# all of these would set PL_last_in_gv to a non-GV which would
645# assert when referenced by the magic for ${^LAST_FH}.
745e740c
TC
646# The approach to fixing this has changed (#128263), but it's still useful
647# to check each op.
648for my $code ('tell $0', 'sysseek $0, 0, 0', 'seek $0, 0, 0', 'eof $0') {
649 fresh_perl_is("$code; print defined \${^LAST_FH} ? qq(not ok\n) : qq(ok\n)", "ok\n",
650 undef, "check $code doesn't define \${^LAST_FH}");
651}
8561ea1d 652
8f25f6c0 653# $|
60acf818 654fresh_perl_is 'print $| = ~$|', "1\n", {switches => ['-l']},
8f25f6c0 655 '[perl #4760] print $| = ~$|';
4505a31f 656fresh_perl_is
60acf818 657 'select f; undef *f; ${q/|/}; print STDOUT qq|ok\n|', "ok\n", {},
4505a31f 658 '[perl #115206] no crash when vivifying $| while *{+select}{IO} is undef';
8f25f6c0 659
1c75beb8
FC
660# ${^OPEN} and $^H interaction
661# Setting ${^OPEN} causes $^H to change, but setting $^H would only some-
662# times make ${^OPEN} change, depending on whether it was in the same BEGIN
663# block. Don’t test actual values (subject to change); just test for
664# consistency.
665my @stuff;
666eval '
667 BEGIN { ${^OPEN} = "a\0b"; $^H = 0; push @stuff, ${^OPEN} }
668 BEGIN { ${^OPEN} = "a\0b"; $^H = 0 } BEGIN { push @stuff, ${^OPEN} }
6691' or die $@;
670is $stuff[0], $stuff[1], '$^H modifies ${^OPEN} consistently';
671
4639d557
FC
672# deleting $::{"\cH"}
673is runperl(prog => 'delete $::{qq-\cH-}; ${^OPEN}=foo; print qq-ok\n-'),
674 "ok\n",
675 'deleting $::{"\cH"}';
676
66230c86
NC
677# Tests for some non-magic names:
678is ${^MPE}, undef, '${^MPE} starts undefined';
679is ++${^MPE}, 1, '${^MPE} can be incremented';
680
53f775c2 681# This one used to behave as ${^MATCH} due to a missing break:
66230c86 682is ${^MPEN}, undef, '${^MPEN} starts undefined';
53f775c2 683# This one used to croak due to that missing break:
66230c86 684is ++${^MPEN}, 1, '${^MPEN} can be incremented';
8f25f6c0 685
a15a3d9b
FC
686{
687 no warnings 'deprecated';
688 eval { ${^E_NCODING} = 1 };
689 is $@, "", 'Setting ${^E_NCODING} does nothing';
690 $_ = ${^E_NCODING};
691 pass('can read ${^E_NCODING} without blowing up');
692 is $_, 1, '${^E_NCODING} is whatever it was set to';
693}
199670db 694
39539141
DIM
695{
696 my $warned = 0;
697 local $SIG{__WARN__} = sub { ++$warned if $_[0] =~ /Use of uninitialized value in unshift/; print "# @_"; };
698 unshift @RT12608::A::ISA, qw(RT12608::B RT12608::C);
699 is $warned, 0, '[perl #126082] unshifting onto @ISA doesn\'t trigger set magic for each item';
700}
701
702{
703 my $warned = 0;
704 local $SIG{__WARN__} = sub { ++$warned if $_[0] =~ /Use of uninitialized value in unshift/; print "# @_"; };
705
706 my $x; tie $x, 'RT12608::F';
707 unshift @RT12608::X::ISA, $x, "RT12608::Z";
708 is $warned, 0, '[perl #126082] PL_delaymagic correctly/saved restored when pushing/unshifting onto @ISA';
709
710 package RT12608::F;
711 use parent 'Tie::Scalar';
712 sub TIESCALAR { bless {}; }
713 sub FETCH { push @RT12608::G::ISA, "RT12608::H"; "RT12608::Y"; }
714}
715
624c42e2 716
61ec922c
NC
717# ^^^^^^^^^ New tests go here ^^^^^^^^^
718
719SKIP: {
1eea96ba 720 skip("%ENV manipulations fail or aren't safe on $^O", 20)
f8c6801b 721 if $Is_Dos;
1eea96ba
TC
722 skip "Win32 needs XS for env/shell tests", 20
723 if $Is_MSWin32 && is_miniperl;
61ec922c
NC
724
725 SKIP: {
f8c6801b
CB
726 skip("clearing \%ENV is not safe when running under valgrind or on VMS")
727 if $ENV{PERL_VALGRIND} || $Is_VMS;
61ec922c
NC
728
729 $PATH = $ENV{PATH};
15dd0034 730 $SYSTEMROOT = $ENV{SYSTEMROOT} if exists $ENV{SYSTEMROOT}; # win32
61ec922c
NC
731 $PDL = $ENV{PERL_DESTRUCT_LEVEL} || 0;
732 $ENV{foo} = "bar";
733 %ENV = ();
734 $ENV{PATH} = $PATH;
15dd0034 735 $ENV{SYSTEMROOT} = $SYSTEMROOT if defined $SYSTEMROOT;
61ec922c
NC
736 $ENV{PERL_DESTRUCT_LEVEL} = $PDL || 0;
737 if ($Is_MSWin32) {
738 is `set foo 2>NUL`, "";
739 } else {
740 is `echo \$foo`, "\n";
741 }
742 }
743
613c63b4
CS
744 $ENV{__NoNeSuCh} = 'foo';
745 $0 = 'bar';
746 env_is(__NoNeSuCh => 'foo', 'setting $0 does not break %ENV');
747
efeca31e 748 $ENV{__NoNeSuCh2} = 'foo';
888a67f6
KF
749 $ENV{__NoNeSuCh2} = undef;
750 env_is(__NoNeSuCh2 => '', 'setting a key as undef does not delete it');
751
613c63b4
CS
752 # stringify a glob
753 $ENV{foo} = *TODO;
754 env_is(foo => '*main::TODO', 'ENV store of stringified glob');
755
756 # stringify a ref
757 my $ref = [];
758 $ENV{foo} = $ref;
759 env_is(foo => "$ref", 'ENV store of stringified ref');
760
761 # downgrade utf8 when possible
762 $bytes = "eh zero \x{A0}";
763 utf8::upgrade($chars = $bytes);
764 $forced = $ENV{foo} = $chars;
765 ok(!utf8::is_utf8($forced) && $forced eq $bytes, 'ENV store downgrades utf8 in SV');
766 env_is(foo => $bytes, 'ENV store downgrades utf8 in setenv');
767
768 # warn when downgrading utf8 is not possible
769 $chars = "X-Day \x{1998}";
770 utf8::encode($bytes = $chars);
771 {
772 my $warned = 0;
773 local $SIG{__WARN__} = sub { ++$warned if $_[0] =~ /^Wide character in setenv/; print "# @_" };
774 $forced = $ENV{foo} = $chars;
775 ok($warned == 1, 'ENV store warns about wide characters');
61ec922c 776 }
613c63b4
CS
777 ok(!utf8::is_utf8($forced) && $forced eq $bytes, 'ENV store encodes high utf8 in SV');
778 env_is(foo => $bytes, 'ENV store encodes high utf8 in SV');
779
780 # test local $ENV{foo} on existing foo
781 {
782 local $ENV{__NoNeSuCh};
783 { local $TODO = 'exists on %ENV should reflect real env';
784 ok(!exists $ENV{__NoNeSuCh}, 'not exists $ENV{existing} during local $ENV{existing}'); }
785 env_is(__NoNeLoCaL => '');
786 }
787 ok(exists $ENV{__NoNeSuCh}, 'exists $ENV{existing} after local $ENV{existing}');
788 env_is(__NoNeSuCh => 'foo');
789
790 # test local $ENV{foo} on new foo
791 {
792 local $ENV{__NoNeLoCaL} = 'foo';
793 ok(exists $ENV{__NoNeLoCaL}, 'exists $ENV{new} during local $ENV{new}');
794 env_is(__NoNeLoCaL => 'foo');
795 }
796 ok(!exists $ENV{__NoNeLoCaL}, 'not exists $ENV{new} after local $ENV{new}');
797 env_is(__NoNeLoCaL => '');
798
61ec922c 799 SKIP: {
4b64ef59 800 skip("\$0 check only on Linux, Dragonfly BSD and FreeBSD", 2)
09d8c0d2 801 unless $^O =~ /^(linux|android|dragonfly|freebsd)$/;
61ec922c 802
09d8c0d2
TH
803 SKIP: {
804 skip("No procfs cmdline support", 1)
805 unless open CMDLINE, "/proc/$$/cmdline";
806
807 chomp(my $line = scalar <CMDLINE>);
808 my $me = (split /\0/, $line)[0];
809 is $me, $0, 'altering $0 is effective (testing with /proc/)';
810 close CMDLINE;
811 }
4b64ef59 812 skip("No \$0 check with 'ps' on Android", 1) if $^O eq 'android';
61ec922c
NC
813 # perlbug #22811
814 my $mydollarzero = sub {
815 my($arg) = shift;
816 $0 = $arg if defined $arg;
817 # In FreeBSD the ps -o command= will cause
818 # an empty header line, grab only the last line.
819 my $ps = (`ps -o command= -p $$`)[-1];
820 return if $?;
821 chomp $ps;
61ec922c
NC
822 $ps;
823 };
824 my $ps = $mydollarzero->("x");
0522a9e7
TH
825 # we allow that something goes wrong with the ps command
826 !$ps && skip("The ps command failed", 1);
827 my $ps_re = ( $^O =~ /^(dragonfly|freebsd)$/ )
828 # FreeBSD cannot get rid of both the leading "perl :"
829 # and the trailing " (perl)": some FreeBSD versions
830 # can get rid of the first one.
831 ? qr/^(?:perl: )?x(?: \(perl\))?$/
832 # In Linux 2.4 we would get an exact match ($ps eq 'x') but
833 # in Linux 2.2 there seems to be something funny going on:
834 # it seems as if the original length of the argv[] would
835 # be stored in the proc struct and then used by ps(1),
836 # no matter what characters we use to pad the argv[].
837 # (And if we use \0:s, they are shown as spaces.) Sigh.
838 : qr/^x\s*$/
839 ;
840 like($ps, $ps_re, 'altering $0 is effective (testing with `ps`)');
61ec922c
NC
841 }
842}
843
844# test case-insignificance of %ENV (these tests must be enabled only
845# when perl is compiled with -DENV_IS_CASELESS)
846SKIP: {
847 skip('no caseless %ENV support', 4) unless $Is_MSWin32 || $Is_NetWare;
848
849 %ENV = ();
850 $ENV{'Foo'} = 'bar';
851 $ENV{'fOo'} = 'baz';
852 is scalar(keys(%ENV)), 1;
853 ok exists $ENV{'FOo'};
854 is delete $ENV{'foO'}, 'baz';
855 is scalar(keys(%ENV)), 0;
856}
857
858__END__
859
860# Put new tests before the various ENV tests, as they blow %ENV away.