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