This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Avoid uninit warning for qq|${\<<FOO}|
[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
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)
7b903762 109 if $Is_MSWin32 || $Is_NetWare || $Is_Dos || $Is_MPE;
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 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
EF
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 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 }
a1a0e61e 319 if ($^O eq 'os390' or $^O eq 'posix-bc' or $^O eq 'vmesa') { # 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
61ec922c
NC
607# ^^^^^^^^^ New tests go here ^^^^^^^^^
608
609SKIP: {
bf1be224 610 skip("%ENV manipulations fail or aren't safe on $^O", 19)
f8c6801b 611 if $Is_Dos;
61ec922c
NC
612
613 SKIP: {
f8c6801b
CB
614 skip("clearing \%ENV is not safe when running under valgrind or on VMS")
615 if $ENV{PERL_VALGRIND} || $Is_VMS;
61ec922c
NC
616
617 $PATH = $ENV{PATH};
618 $PDL = $ENV{PERL_DESTRUCT_LEVEL} || 0;
619 $ENV{foo} = "bar";
620 %ENV = ();
621 $ENV{PATH} = $PATH;
622 $ENV{PERL_DESTRUCT_LEVEL} = $PDL || 0;
623 if ($Is_MSWin32) {
624 is `set foo 2>NUL`, "";
625 } else {
626 is `echo \$foo`, "\n";
627 }
628 }
629
613c63b4
CS
630 $ENV{__NoNeSuCh} = 'foo';
631 $0 = 'bar';
632 env_is(__NoNeSuCh => 'foo', 'setting $0 does not break %ENV');
633
634 # stringify a glob
635 $ENV{foo} = *TODO;
636 env_is(foo => '*main::TODO', 'ENV store of stringified glob');
637
638 # stringify a ref
639 my $ref = [];
640 $ENV{foo} = $ref;
641 env_is(foo => "$ref", 'ENV store of stringified ref');
642
643 # downgrade utf8 when possible
644 $bytes = "eh zero \x{A0}";
645 utf8::upgrade($chars = $bytes);
646 $forced = $ENV{foo} = $chars;
647 ok(!utf8::is_utf8($forced) && $forced eq $bytes, 'ENV store downgrades utf8 in SV');
648 env_is(foo => $bytes, 'ENV store downgrades utf8 in setenv');
649
650 # warn when downgrading utf8 is not possible
651 $chars = "X-Day \x{1998}";
652 utf8::encode($bytes = $chars);
653 {
654 my $warned = 0;
655 local $SIG{__WARN__} = sub { ++$warned if $_[0] =~ /^Wide character in setenv/; print "# @_" };
656 $forced = $ENV{foo} = $chars;
657 ok($warned == 1, 'ENV store warns about wide characters');
61ec922c 658 }
613c63b4
CS
659 ok(!utf8::is_utf8($forced) && $forced eq $bytes, 'ENV store encodes high utf8 in SV');
660 env_is(foo => $bytes, 'ENV store encodes high utf8 in SV');
661
662 # test local $ENV{foo} on existing foo
663 {
664 local $ENV{__NoNeSuCh};
665 { local $TODO = 'exists on %ENV should reflect real env';
666 ok(!exists $ENV{__NoNeSuCh}, 'not exists $ENV{existing} during local $ENV{existing}'); }
667 env_is(__NoNeLoCaL => '');
668 }
669 ok(exists $ENV{__NoNeSuCh}, 'exists $ENV{existing} after local $ENV{existing}');
670 env_is(__NoNeSuCh => 'foo');
671
672 # test local $ENV{foo} on new foo
673 {
674 local $ENV{__NoNeLoCaL} = 'foo';
675 ok(exists $ENV{__NoNeLoCaL}, 'exists $ENV{new} during local $ENV{new}');
676 env_is(__NoNeLoCaL => 'foo');
677 }
678 ok(!exists $ENV{__NoNeLoCaL}, 'not exists $ENV{new} after local $ENV{new}');
679 env_is(__NoNeLoCaL => '');
680
61ec922c
NC
681 SKIP: {
682 skip("\$0 check only on Linux and FreeBSD", 2)
683 unless $^O =~ /^(linux|freebsd)$/
684 && open CMDLINE, "/proc/$$/cmdline";
685
686 chomp(my $line = scalar <CMDLINE>);
687 my $me = (split /\0/, $line)[0];
688 is $me, $0, 'altering $0 is effective (testing with /proc/)';
689 close CMDLINE;
690 # perlbug #22811
691 my $mydollarzero = sub {
692 my($arg) = shift;
693 $0 = $arg if defined $arg;
694 # In FreeBSD the ps -o command= will cause
695 # an empty header line, grab only the last line.
696 my $ps = (`ps -o command= -p $$`)[-1];
697 return if $?;
698 chomp $ps;
699 printf "# 0[%s]ps[%s]\n", $0, $ps;
700 $ps;
701 };
702 my $ps = $mydollarzero->("x");
703 ok(!$ps # we allow that something goes wrong with the ps command
704 # In Linux 2.4 we would get an exact match ($ps eq 'x') but
705 # in Linux 2.2 there seems to be something funny going on:
706 # it seems as if the original length of the argv[] would
707 # be stored in the proc struct and then used by ps(1),
708 # no matter what characters we use to pad the argv[].
709 # (And if we use \0:s, they are shown as spaces.) Sigh.
710 || $ps =~ /^x\s*$/
711 # FreeBSD cannot get rid of both the leading "perl :"
712 # and the trailing " (perl)": some FreeBSD versions
713 # can get rid of the first one.
714 || ($^O eq 'freebsd' && $ps =~ m/^(?:perl: )?x(?: \(perl\))?$/),
715 'altering $0 is effective (testing with `ps`)');
716 }
717}
718
719# test case-insignificance of %ENV (these tests must be enabled only
720# when perl is compiled with -DENV_IS_CASELESS)
721SKIP: {
722 skip('no caseless %ENV support', 4) unless $Is_MSWin32 || $Is_NetWare;
723
724 %ENV = ();
725 $ENV{'Foo'} = 'bar';
726 $ENV{'fOo'} = 'baz';
727 is scalar(keys(%ENV)), 1;
728 ok exists $ENV{'FOo'};
729 is delete $ENV{'foO'}, 'baz';
730 is scalar(keys(%ENV)), 0;
731}
732
733__END__
734
735# Put new tests before the various ENV tests, as they blow %ENV away.