This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
skip failing leak test under -Dmad
[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';
613c63b4 8 plan (tests => 171);
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
5e9f035f
FC
23 )) {
24 my $v = $_;
25 # avoid using any global vars here:
26 if ($v =~ s/^\^(?=.)//) {
27 for(substr $v, 0, 1) {
28 $_ = chr ord() - 64;
29 }
30 }
811a7562
TC
31 SKIP:
32 {
33 skip_if_miniperl("the module for *$_ may not be available in "
34 . "miniperl", 1) if $non_mini{$_};
35 ok defined *$v, "*$_ appears to be defined at the outset";
36 }
5e9f035f
FC
37 }
38}
39
40# This must be in a separate BEGIN block, as the mere mention of ${^TAINT}
41# will invalidate the test for it.
42BEGIN {
0409250f 43 $ENV{PATH} = '/bin' if ${^TAINT};
774d564b 44 $SIG{__WARN__} = sub { die "Dying on warning: ", @_ };
90ce63d5 45}
8d063cd8 46
9f1b1f2d 47use warnings;
04fee9b5 48use Config;
9f1b1f2d 49
0bee926d 50
43651d81
NC
51$Is_MSWin32 = $^O eq 'MSWin32';
52$Is_NetWare = $^O eq 'NetWare';
53$Is_VMS = $^O eq 'VMS';
54$Is_Dos = $^O eq 'dos';
55$Is_os2 = $^O eq 'os2';
56$Is_Cygwin = $^O eq 'cygwin';
43651d81 57$Is_MPE = $^O eq 'mpeix';
dbc1d986 58$Is_BeOS = $^O eq 'beos';
be708cc0 59
c8d62b71
RGS
60$PERL = $ENV{PERL}
61 || ($Is_NetWare ? 'perl' :
7b903762 62 $Is_VMS ? $^X :
c8d62b71
RGS
63 $Is_MSWin32 ? '.\perl' :
64 './perl');
68dc0745 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
5ceaabe8
SH
73 (my $set = `set $key`) =~ s/\r\n$/\n/;
74 like $set, qr/^(?:\Q$key\E=)?\Q$val\E$/, $desc;
f8c6801b
CB
75 } elsif ($Is_VMS) {
76 is `write sys\$output f\$trnlnm("\Q$key\E")`, "$val\n", $desc;
613c63b4
CS
77 } else {
78 is `echo \$\Q$key\E`, "$val\n", $desc;
79 }
80}
81
8df0e0ed
JM
82END {
83 # On VMS, environment variable changes are peristent after perl exits
f8c6801b
CB
84 if ($Is_VMS) {
85 delete $ENV{'FOO'};
86 delete $ENV{'__NoNeSuCh'};
87 }
8df0e0ed
JM
88}
89
39e571d4 90eval '$ENV{"FOO"} = "hi there";'; # check that ENV is inited inside eval
26f6e342
NK
91# cmd.exe will echo 'variable=value' but 4nt will echo just the value
92# -- Nikola Knezevic
b2978f4e 93if ($Is_MSWin32) { like `set FOO`, qr/^(?:FOO=)?hi there$/; }
b2978f4e
NC
94elsif ($Is_VMS) { is `write sys\$output f\$trnlnm("FOO")`, "hi there\n"; }
95else { is `echo \$FOO`, "hi there\n"; }
8d063cd8 96
ba2406eb 97unlink_all 'ajslkdfpqjsjfk';
8d063cd8 98$! = 0;
90ce63d5 99open(FOO,'ajslkdfpqjsjfk');
b2978f4e 100isnt($!, 0);
90ce63d5 101close FOO; # just mention it, squelch used-only-once
8d063cd8 102
b2978f4e
NC
103SKIP: {
104 skip('SIGINT not safe on this platform', 5)
7b903762 105 if $Is_MSWin32 || $Is_NetWare || $Is_Dos || $Is_MPE;
c363d00c
CB
106 # the next tests are done in a subprocess because sh spits out a
107 # newline onto stderr when a child process kills itself with SIGINT.
04fee9b5 108 # We use a pipe rather than system() because the VMS command buffer
c363d00c
CB
109 # would overflow with a command that long.
110
5e9f035f
FC
111 # For easy interpolation of test numbers:
112 $next_test = curr_test() - 1;
113 sub TIEARRAY {bless[]}
114 sub FETCH { $next_test + pop }
115 tie my @tn, __PACKAGE__;
116
c363d00c
CB
117 open( CMDPIPE, "| $PERL");
118
5e9f035f 119 print CMDPIPE "\$t1 = $tn[1]; \$t2 = $tn[2];\n", <<'END';
378cc40b 120
79072805 121 $| = 1; # command buffering
378cc40b 122
5e9f035f
FC
123 $SIG{"INT"} = "ok1"; kill "INT",$$; sleep 1;
124 $SIG{"INT"} = "IGNORE"; kill "INT",$$; sleep 1; print "ok $t2\n";
125 $SIG{"INT"} = "DEFAULT"; kill "INT",$$; sleep 1; print" not ok $t2\n";
79072805 126
5e9f035f 127 sub ok1 {
79072805 128 if (($x = pop(@_)) eq "INT") {
5e9f035f 129 print "ok $t1\n";
79072805
LW
130 }
131 else {
5e9f035f 132 print "not ok $t1 ($x @_)\n";
79072805
LW
133 }
134 }
135
136END
c363d00c
CB
137
138 close CMDPIPE;
139
2d4fcd5e 140 open( CMDPIPE, "| $PERL");
5e9f035f 141 print CMDPIPE "\$t3 = $tn[3];\n", <<'END';
2d4fcd5e
AJ
142
143 { package X;
144 sub DESTROY {
145 kill "INT",$$;
146 }
147 }
148 sub x {
149 my $x=bless [], 'X';
150 return sub { $x };
151 }
152 $| = 1; # command buffering
5e9f035f 153 $SIG{"INT"} = "ok3";
2d4fcd5e
AJ
154 {
155 local $SIG{"INT"}=x();
156 print ""; # Needed to expose failure in 5.8.0 (why?)
157 }
158 sleep 1;
159 delete $SIG{"INT"};
160 kill "INT",$$; sleep 1;
5e9f035f
FC
161 sub ok3 {
162 print "ok $t3\n";
2d4fcd5e
AJ
163 }
164END
165 close CMDPIPE;
bb4e15c8 166 $? >>= 8 if $^O eq 'VMS'; # POSIX status hiding in 2nd byte
639cf43b 167 my $todo = ($^O eq 'os2' ? ' # TODO: EMX v0.9d_fix4 bug: wrong nibble? ' : '');
5e9f035f 168 print $? & 0xFF ? "ok $tn[4]$todo\n" : "not ok $tn[4]$todo\n";
2d4fcd5e 169
6e592b3a
BM
170 open(CMDPIPE, "| $PERL");
171 print CMDPIPE <<'END';
172
173 sub PVBM () { 'foo' }
174 index 'foo', PVBM;
175 my $pvbm = PVBM;
176
177 sub foo { exit 0 }
178
179 $SIG{"INT"} = $pvbm;
180 kill "INT", $$; sleep 1;
181END
182 close CMDPIPE;
183 $? >>= 8 if $^O eq 'VMS';
5e9f035f 184 print $? ? "not ok $tn[5]\n" : "ok $tn[5]\n";
6e592b3a 185
b2978f4e 186 curr_test(curr_test() + 5);
68dc0745 187}
a687059c 188
68dc0745 189# can we slice ENV?
190@val1 = @ENV{keys(%ENV)};
a687059c 191@val2 = values(%ENV);
b2978f4e
NC
192is join(':',@val1), join(':',@val2);
193cmp_ok @val1, '>', 1;
90ce63d5
RS
194
195# regex vars
196'foobarbaz' =~ /b(a)r/;
b2978f4e
NC
197is $`, 'foo';
198is $&, 'bar';
199is $', 'baz';
200is $+, 'a';
90ce63d5 201
a289ef89
FC
202# [perl #24237]
203for (qw < ` & ' >) {
204 fresh_perl_is
205 qq < \@$_; q "fff" =~ /(?!^)./; print "[\$$_]\\n" >,
206 "[f]\n", {},
207 "referencing \@$_ before \$$_ etc. still saws off ampersands";
208}
209
90ce63d5
RS
210# $"
211@a = qw(foo bar baz);
b2978f4e 212is "@a", "foo bar baz";
90ce63d5
RS
213{
214 local $" = ',';
b2978f4e 215 is "@a", "foo,bar,baz";
90ce63d5 216}
a687059c 217
90ce63d5
RS
218# $;
219%h = ();
220$h{'foo', 'bar'} = 1;
b2978f4e 221is((keys %h)[0], "foo\034bar");
90ce63d5
RS
222{
223 local $; = 'x';
224 %h = ();
225 $h{'foo', 'bar'} = 1;
b2978f4e 226 is((keys %h)[0], 'fooxbar');
90ce63d5 227}
ed6116ce 228
90ce63d5 229# $?, $@, $$
7b903762
RGS
230system qq[$PERL "-I../lib" -e "use vmsish qw(hushed); exit(0)"];
231is $?, 0;
232system qq[$PERL "-I../lib" -e "use vmsish qw(hushed); exit(1)"];
233isnt $?, 0;
90ce63d5
RS
234
235eval { die "foo\n" };
b2978f4e 236is $@, "foo\n";
90ce63d5 237
b2978f4e 238cmp_ok($$, '>', 0);
41e07bbc 239my $pid = $$;
9cdac2a2
FC
240eval { $$ = 42 };
241is $$, 42, '$$ can be modified';
242SKIP: {
243 skip "no fork", 1 unless $Config{d_fork};
244 (my $kidpid = open my $fh, "-|") // skip "cannot fork: $!", 1;
245 if($kidpid) { # parent
246 my $kiddollars = <$fh>;
247 close $fh or die "cannot close pipe from kid proc: $!";
248 is $kiddollars, $kidpid, '$$ is reset on fork';
249 }
250 else { # child
251 print $$;
252 $::NO_ENDING = 1; # silence "Looks like you only ran..."
253 exit;
254 }
255}
41e07bbc 256$$ = $pid; # Tests below use $$
90ce63d5
RS
257
258# $^X and $0
ed37317b 259{
ae60cb46
NC
260 my $is_abs = $Config{d_procselfexe} || $Config{usekernprocpathname}
261 || $Config{usensgetexecutablepath};
3e3baf6d 262 if ($^O eq 'qnx') {
7fbf1995 263 chomp($wd = `/usr/bin/fullpath -t`);
68dc0745 264 }
2982a345 265 elsif($Is_Cygwin || $is_abs) {
1cab015a
EF
266 # Cygwin turns the symlink into the real file
267 chomp($wd = `pwd`);
268 $wd =~ s#/t$##;
0409250f 269 $wd =~ /(.*)/; $wd = $1; # untaint
6178c52a
JH
270 if ($Is_Cygwin) {
271 $wd = Cygwin::win_to_posix_path(Cygwin::posix_to_win_path($wd, 1));
272 }
1cab015a 273 }
ed344e4f
IZ
274 elsif($Is_os2) {
275 $wd = Cwd::sys_cwd();
276 }
68dc0745 277 else {
278 $wd = '.';
279 }
2982a345 280 my $perl = $Is_VMS || $is_abs ? $^X : "$wd/perl";
ed37317b 281 my $headmaybe = '';
6178c52a 282 my $middlemaybe = '';
ed37317b 283 my $tailmaybe = '';
68dc0745 284 $script = "$wd/show-shebang";
ed37317b
TB
285 if ($Is_MSWin32) {
286 chomp($wd = `cd`);
8ac9c18d
GS
287 $wd =~ s|\\|/|g;
288 $perl = "$wd/perl.exe";
289 $script = "$wd/show-shebang.bat";
ed37317b
TB
290 $headmaybe = <<EOH ;
291\@rem ='
292\@echo off
293$perl -x \%0
294goto endofperl
295\@rem ';
296EOH
297 $tailmaybe = <<EOT ;
298
299__END__
300:endofperl
301EOT
302 }
ed344e4f
IZ
303 elsif ($Is_os2) {
304 $script = "./show-shebang";
305 }
c363d00c
CB
306 elsif ($Is_VMS) {
307 $script = "[]show-shebang";
be708cc0 308 }
6178c52a
JH
309 elsif ($Is_Cygwin) {
310 $middlemaybe = <<'EOX'
311$^X = Cygwin::win_to_posix_path(Cygwin::posix_to_win_path($^X, 1));
312$0 = Cygwin::win_to_posix_path(Cygwin::posix_to_win_path($0, 1));
313EOX
314 }
a1a0e61e 315 if ($^O eq 'os390' or $^O eq 'posix-bc' or $^O eq 'vmesa') { # no shebang
9d116dd7
JH
316 $headmaybe = <<EOH ;
317 eval 'exec ./perl -S \$0 \${1+"\$\@"}'
318 if 0;
319EOH
320 }
2eecd615 321 $s1 = "\$^X is $perl, \$0 is $script\n";
0409250f 322 ok open(SCRIPT, ">$script") or diag "Can't write to $script: $!";
b2978f4e 323 ok print(SCRIPT $headmaybe . <<EOB . $middlemaybe . <<'EOF' . $tailmaybe) or diag $!;
0f77baf6 324#!$perl
774d564b 325EOB
90ce63d5
RS
326print "\$^X is $^X, \$0 is $0\n";
327EOF
b2978f4e
NC
328 ok close(SCRIPT) or diag $!;
329 ok chmod(0755, $script) or diag $!;
7b903762 330 $_ = $Is_VMS ? `$perl $script` : `$script`;
ed344e4f 331 s/\.exe//i if $Is_Dos or $Is_Cygwin or $Is_os2;
dbc1d986 332 s{./$script}{$script} if $Is_BeOS; # revert BeOS execvp() side-effect
ed37317b 333 s{is perl}{is $perl}; # for systems where $^X is only a basename
a6c40364 334 s{\\}{/}g;
b2978f4e
NC
335 if ($Is_MSWin32 || $Is_os2) {
336 is uc $_, uc $s1;
337 } else {
338 is $_, $s1;
339 }
ed37317b 340 $_ = `$perl $script`;
4bbb7126 341 s/\.exe//i if $Is_Dos or $Is_os2 or $Is_Cygwin;
dbc1d986 342 s{./$perl}{$perl} if $Is_BeOS; # revert BeOS execvp() side-effect
a6c40364 343 s{\\}{/}g;
b2978f4e
NC
344 if ($Is_MSWin32 || $Is_os2) {
345 is uc $_, uc $s1;
346 } else {
347 is $_, $s1;
348 }
349 ok unlink($script) or diag $!;
ba2406eb
BG
350 # CHECK
351 # Could this be replaced with:
352 # unlink_all($script);
68dc0745 353}
ed6116ce 354
90ce63d5 355# $], $^O, $^T
b2978f4e 356cmp_ok $], '>=', 5.00319;
0bee926d 357ok $^O;
b2978f4e 358cmp_ok $^T, '>', 850000000;
66b1d557 359
881ddac4
SH
360# Test change 25062 is working
361my $orig_osname = $^O;
362{
363local $^I = '.bak';
b2978f4e 364is $^O, $orig_osname, 'Assigning $^I does not clobber $^O';
881ddac4
SH
365}
366$^O = $orig_osname;
367
065144c6
TC
368{
369 #RT #72422
370 foreach my $p (0, 1) {
371 fresh_perl_is(<<"EOP", '2 4 8', undef, "test \$^P = $p");
372\$DB::single = 2;
373\$DB::trace = 4;
374\$DB::signal = 8;
375\$^P = $p;
376print "\$DB::single \$DB::trace \$DB::signal";
377EOP
378 }
379}
380
7636ea95
AB
381# Check that assigning to $0 on Linux sets the process name with both
382# argv[0] assignment and by calling prctl()
383{
384 SKIP: {
385 skip "We don't have prctl() here", 2 unless $Config{d_prctl_set_name};
386
387 # We don't really need these tests. prctl() is tested in the
388 # Kernel, but test it anyway for our sanity. If something doesn't
389 # work (like if the system doesn't have a ps(1) for whatever
390 # reason) just bail out gracefully.
391 my $maybe_ps = sub {
392 my ($cmd) = @_;
393 local ($?, $!);
394
395 no warnings;
396 my $res = `$cmd`;
3a46d15c 397 skip "Couldn't shell out to '$cmd', returned code $?", 2 if $?;
7636ea95
AB
398 return $res;
399 };
400
401 my $name = "Good Morning, Dave";
402 $0 = $name;
403
404 chomp(my $argv0 = $maybe_ps->("ps h $$"));
405 chomp(my $prctl = $maybe_ps->("ps hc $$"));
406
407 like($argv0, $name, "Set process name through argv[0] ($argv0)");
408 like($prctl, substr($name, 0, 15), "Set process name through prctl() ($prctl)");
409 }
410}
411
c7213721 412{
a45269de
MS
413 my $ok = 1;
414 my $warn = '';
ae8ade65 415 local $SIG{'__WARN__'} = sub { $ok = 0; $warn = join '', @_; $warn =~ s/\n$//; };
78987ded 416 $! = undef;
b2978f4e
NC
417 local $TODO = $Is_VMS ? "'\$!=undef' does throw a warning" : '';
418 ok($ok, $warn);
78987ded
HS
419}
420
b2978f4e 421SKIP: {
d3d1232e 422 skip_if_miniperl("miniperl can't rely on loading %Errno", 2);
126c71c8
YST
423 no warnings 'void';
424
d2c93421
RH
425# Make sure Errno hasn't been prematurely autoloaded
426
b79f7545 427 ok !keys %Errno::;
d2c93421
RH
428
429# Test auto-loading of Errno when %! is used
430
126c71c8
YST
431 ok scalar eval q{
432 %!;
902fde96 433 scalar %Errno::;
126c71c8
YST
434 }, $@;
435}
d2c93421 436
b2978f4e 437SKIP: {
ffdb8bcd 438 skip_if_miniperl("miniperl can't rely on loading %Errno", 2);
43651d81 439 # Make sure that Errno loading doesn't clobber $!
d2c93421 440
43651d81
NC
441 undef %Errno::;
442 delete $INC{"Errno.pm"};
d2c93421 443
43651d81
NC
444 open(FOO, "nonesuch"); # Generate ENOENT
445 my %errs = %{"!"}; # Cause Errno.pm to be loaded at run-time
446 ok ${"!"}{ENOENT};
ffdb8bcd
FC
447
448 # Make sure defined(*{"!"}) before %! does not stop %! from working
449 is
450 runperl(
451 prog => 'BEGIN { defined *{q-!-} } print qq-ok\n- if tied %!',
452 ),
453 "ok\n",
454 'defined *{"!"} does not stop %! from working';
43651d81 455}
a4268c0a 456
ccdda9cb 457# Check that we don't auto-load packages
976bd1ee
DM
458SKIP: {
459 skip "staticly linked; may be preloaded", 4 unless $Config{usedl};
460 foreach (['powie::!', 'Errno'],
461 ['powie::+', 'Tie::Hash::NamedCapture']) {
462 my ($symbol, $package) = @$_;
463 foreach my $scalar_first ('', '$$symbol;') {
464 my $desc = qq{Referencing %{"$symbol"}};
465 $desc .= qq{ after mentioning \${"$symbol"}} if $scalar_first;
466 $desc .= " doesn't load $package";
467
468 fresh_perl_is(<<"EOP", 0, {}, $desc);
ccdda9cb
NC
469use strict qw(vars subs);
470my \$symbol = '$symbol';
471$scalar_first;
4721 if %{\$symbol};
473print scalar %${package}::;
474EOP
976bd1ee 475 }
ccdda9cb
NC
476 }
477}
478
b2978f4e
NC
479is $^S, 0;
480eval { is $^S,1 };
b0e6f864 481eval " BEGIN { ok ! defined \$^S } ";
b2978f4e 482is $^S, 0;
7c36658b 483
0409250f
RGS
484my $taint = ${^TAINT};
485is ${^TAINT}, $taint;
7c36658b 486eval { ${^TAINT} = 1 };
0409250f 487is ${^TAINT}, $taint;
9aa702ec
MJD
488
489# 5.6.1 had a bug: @+ and @- were not properly interpolated
490# into double-quoted strings
491# 20020414 mjd-perl-patch+@plover.com
b64ebf53 492"I like pie" =~ /(I) (like) (pie)/;
b2978f4e
NC
493is "@-", "0 0 2 7";
494is "@+", "10 1 6 10";
9aa702ec 495
f28098ff
RGS
496# Tests for the magic get of $\
497{
498 my $ok = 0;
499 # [perl #19330]
500 {
501 local $\ = undef;
502 $\++; $\++;
503 $ok = $\ eq 2;
504 }
505 ok $ok;
506 $ok = 0;
507 {
508 local $\ = "a\0b";
509 $ok = "a$\b" eq "aa\0bb";
510 }
511 ok $ok;
512}
547d1dd8 513
4c9140ed 514# Test for bug [perl #36434]
b2978f4e
NC
515# Can not do this test on VMS, EPOC, and SYMBIAN according to comments
516# in mg.c/Perl_magic_clear_all_env()
517SKIP: {
518 skip('Can\'t make assignment to \%ENV on this system', 3) if $Is_VMS;
519
4c9140ed
RGS
520 local @ISA;
521 local %ENV;
e1a479c5
BB
522 # This used to be __PACKAGE__, but that causes recursive
523 # inheritance, which is detected earlier now and broke
524 # this test
525 eval { push @ISA, __FILE__ };
b2978f4e 526 is $@, '', 'Push a constant on a magic array';
4c9140ed
RGS
527 $@ and print "# $@";
528 eval { %ENV = (PATH => __PACKAGE__) };
b2978f4e 529 is $@, '', 'Assign a constant to a magic hash';
d8084ca5
DM
530 $@ and print "# $@";
531 eval { my %h = qw(A B); %ENV = (PATH => (keys %h)[0]) };
b2978f4e 532 is $@, '', 'Assign a shared key to a magic hash';
4c9140ed
RGS
533 $@ and print "# $@";
534}
179c85a2
NC
535
536# Tests for Perl_magic_clearsig
537foreach my $sig (qw(__WARN__ INT)) {
538 $SIG{$sig} = lc $sig;
539 is $SIG{$sig}, 'main::' . lc $sig, "Can assign to $sig";
540 is delete $SIG{$sig}, 'main::' . lc $sig, "Can delete from $sig";
541 is $SIG{$sig}, undef, "$sig is now gone";
542 is delete $SIG{$sig}, undef, "$sig remains gone";
543}
544
545# And now one which doesn't exist;
546{
547 no warnings 'signal';
548 $SIG{HUNGRY} = 'mmm, pie';
549}
550is $SIG{HUNGRY}, 'mmm, pie', 'Can assign to HUNGRY';
551is delete $SIG{HUNGRY}, 'mmm, pie', 'Can delete from HUNGRY';
552is $SIG{HUNGRY}, undef, "HUNGRY is now gone";
553is delete $SIG{HUNGRY}, undef, "HUNGRY remains gone";
554
555# Test deleting signals that we never set
c8be058c 556foreach my $sig (qw(__DIE__ _BOGUS_HOOK KILL THIRSTY)) {
179c85a2
NC
557 is $SIG{$sig}, undef, "$sig is not present";
558 is delete $SIG{$sig}, undef, "delete of $sig returns undef";
559}
be1cf43c
NC
560
561{
562 $! = 9999;
563 is int $!, 9999, q{[perl #72850] Core dump in bleadperl from perl -e '$! = 9999; $a = $!;'};
564
565}
4c0f30d6 566
213084e4
FC
567# %+ %-
568SKIP: {
afb8fe69 569 skip_if_miniperl("No XS in miniperl", 2);
213084e4
FC
570 # Make sure defined(*{"+"}) before %+ does not stop %+ from working
571 is
572 runperl(
573 prog => 'BEGIN { defined *{q-+-} } print qq-ok\n- if tied %+',
574 ),
575 "ok\n",
576 'defined *{"+"} does not stop %+ from working';
577 is
578 runperl(
579 prog => 'BEGIN { defined *{q=-=} } print qq-ok\n- if tied %-',
580 ),
581 "ok\n",
582 'defined *{"-"} does not stop %- from working';
583}
584
0ea03996
FC
585SKIP: {
586 skip_if_miniperl("No XS in miniperl", 3);
587
588 for ( [qw( %- Tie::Hash::NamedCapture )], [qw( $[ arybase )],
589 [qw( %! Errno )] ) {
590 my ($var, $mod) = @$_;
591 my $modfile = $mod =~ s|::|/|gr . ".pm";
592 fresh_perl_is
593 qq 'sub UNIVERSAL::AUTOLOAD{}
594 $mod\::foo() if 0;
595 $var;
596 print "ok\\n" if \$INC{"$modfile"}',
597 "ok\n",
598 { switches => [ '-X' ] },
599 "$var still loads $mod when stash and UNIVERSAL::AUTOLOAD exist";
600 }
601}
602
61ec922c
NC
603# ^^^^^^^^^ New tests go here ^^^^^^^^^
604
605SKIP: {
bf1be224 606 skip("%ENV manipulations fail or aren't safe on $^O", 19)
f8c6801b 607 if $Is_Dos;
61ec922c
NC
608
609 SKIP: {
f8c6801b
CB
610 skip("clearing \%ENV is not safe when running under valgrind or on VMS")
611 if $ENV{PERL_VALGRIND} || $Is_VMS;
61ec922c
NC
612
613 $PATH = $ENV{PATH};
614 $PDL = $ENV{PERL_DESTRUCT_LEVEL} || 0;
615 $ENV{foo} = "bar";
616 %ENV = ();
617 $ENV{PATH} = $PATH;
618 $ENV{PERL_DESTRUCT_LEVEL} = $PDL || 0;
619 if ($Is_MSWin32) {
620 is `set foo 2>NUL`, "";
621 } else {
622 is `echo \$foo`, "\n";
623 }
624 }
625
613c63b4
CS
626 $ENV{__NoNeSuCh} = 'foo';
627 $0 = 'bar';
628 env_is(__NoNeSuCh => 'foo', 'setting $0 does not break %ENV');
629
630 # stringify a glob
631 $ENV{foo} = *TODO;
632 env_is(foo => '*main::TODO', 'ENV store of stringified glob');
633
634 # stringify a ref
635 my $ref = [];
636 $ENV{foo} = $ref;
637 env_is(foo => "$ref", 'ENV store of stringified ref');
638
639 # downgrade utf8 when possible
640 $bytes = "eh zero \x{A0}";
641 utf8::upgrade($chars = $bytes);
642 $forced = $ENV{foo} = $chars;
643 ok(!utf8::is_utf8($forced) && $forced eq $bytes, 'ENV store downgrades utf8 in SV');
644 env_is(foo => $bytes, 'ENV store downgrades utf8 in setenv');
645
646 # warn when downgrading utf8 is not possible
647 $chars = "X-Day \x{1998}";
648 utf8::encode($bytes = $chars);
649 {
650 my $warned = 0;
651 local $SIG{__WARN__} = sub { ++$warned if $_[0] =~ /^Wide character in setenv/; print "# @_" };
652 $forced = $ENV{foo} = $chars;
653 ok($warned == 1, 'ENV store warns about wide characters');
61ec922c 654 }
613c63b4
CS
655 ok(!utf8::is_utf8($forced) && $forced eq $bytes, 'ENV store encodes high utf8 in SV');
656 env_is(foo => $bytes, 'ENV store encodes high utf8 in SV');
657
658 # test local $ENV{foo} on existing foo
659 {
660 local $ENV{__NoNeSuCh};
661 { local $TODO = 'exists on %ENV should reflect real env';
662 ok(!exists $ENV{__NoNeSuCh}, 'not exists $ENV{existing} during local $ENV{existing}'); }
663 env_is(__NoNeLoCaL => '');
664 }
665 ok(exists $ENV{__NoNeSuCh}, 'exists $ENV{existing} after local $ENV{existing}');
666 env_is(__NoNeSuCh => 'foo');
667
668 # test local $ENV{foo} on new foo
669 {
670 local $ENV{__NoNeLoCaL} = 'foo';
671 ok(exists $ENV{__NoNeLoCaL}, 'exists $ENV{new} during local $ENV{new}');
672 env_is(__NoNeLoCaL => 'foo');
673 }
674 ok(!exists $ENV{__NoNeLoCaL}, 'not exists $ENV{new} after local $ENV{new}');
675 env_is(__NoNeLoCaL => '');
676
61ec922c
NC
677 SKIP: {
678 skip("\$0 check only on Linux and FreeBSD", 2)
679 unless $^O =~ /^(linux|freebsd)$/
680 && open CMDLINE, "/proc/$$/cmdline";
681
682 chomp(my $line = scalar <CMDLINE>);
683 my $me = (split /\0/, $line)[0];
684 is $me, $0, 'altering $0 is effective (testing with /proc/)';
685 close CMDLINE;
686 # perlbug #22811
687 my $mydollarzero = sub {
688 my($arg) = shift;
689 $0 = $arg if defined $arg;
690 # In FreeBSD the ps -o command= will cause
691 # an empty header line, grab only the last line.
692 my $ps = (`ps -o command= -p $$`)[-1];
693 return if $?;
694 chomp $ps;
695 printf "# 0[%s]ps[%s]\n", $0, $ps;
696 $ps;
697 };
698 my $ps = $mydollarzero->("x");
699 ok(!$ps # we allow that something goes wrong with the ps command
700 # In Linux 2.4 we would get an exact match ($ps eq 'x') but
701 # in Linux 2.2 there seems to be something funny going on:
702 # it seems as if the original length of the argv[] would
703 # be stored in the proc struct and then used by ps(1),
704 # no matter what characters we use to pad the argv[].
705 # (And if we use \0:s, they are shown as spaces.) Sigh.
706 || $ps =~ /^x\s*$/
707 # FreeBSD cannot get rid of both the leading "perl :"
708 # and the trailing " (perl)": some FreeBSD versions
709 # can get rid of the first one.
710 || ($^O eq 'freebsd' && $ps =~ m/^(?:perl: )?x(?: \(perl\))?$/),
711 'altering $0 is effective (testing with `ps`)');
712 }
713}
714
715# test case-insignificance of %ENV (these tests must be enabled only
716# when perl is compiled with -DENV_IS_CASELESS)
717SKIP: {
718 skip('no caseless %ENV support', 4) unless $Is_MSWin32 || $Is_NetWare;
719
720 %ENV = ();
721 $ENV{'Foo'} = 'bar';
722 $ENV{'fOo'} = 'baz';
723 is scalar(keys(%ENV)), 1;
724 ok exists $ENV{'FOo'};
725 is delete $ENV{'foO'}, 'baz';
726 is scalar(keys(%ENV)), 0;
727}
728
729__END__
730
731# Put new tests before the various ENV tests, as they blow %ENV away.