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