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