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.
17 # not available in miniperl
18 my %non_mini = map { $_ => 1 } qw(+ - [);
20 SIG ^OPEN ^TAINT ^UNICODE ^UTF8LOCALE ^WARNING_BITS 1 2 3 4 5 6 7 8
21 9 42 & ` ' : ? ! _ - [ ^ ~ = % . ( ) < > \ / $ | + ; ] ^A ^C ^D
22 ^E ^F ^H ^I ^L ^N ^O ^P ^S ^T ^V ^W ^UTF8CACHE ::12345 main::98732
26 # avoid using any global vars here:
27 if ($v =~ s/^\^(?=.)//) {
28 for(substr $v, 0, 1) {
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";
41 # This must be in a separate BEGIN block, as the mere mention of ${^TAINT}
42 # will invalidate the test for it.
44 $ENV{PATH} = '/bin' if ${^TAINT};
45 $SIG{__WARN__} = sub { die "Dying on warning: ", @_ };
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';
58 $Is_BeOS = $^O eq 'beos';
61 || ($Is_NetWare ? 'perl' :
63 $Is_MSWin32 ? '.\perl' :
67 my ($key, $val, $desc) = @_;
69 use open IN => ":raw";
71 # cmd.exe will echo 'variable=value' but 4nt will echo just the value
74 my $cp = Win32::GetConsoleOutputCP();
75 Win32::SetConsoleOutputCP(Win32::GetACP());
76 (my $set = `set $key`) =~ s/\r\n$/\n/;
77 Win32::SetConsoleOutputCP($cp);
78 like $set, qr/^(?:\Q$key\E=)?\Q$val\E$/, $desc;
80 is `write sys\$output f\$trnlnm("\Q$key\E")`, "$val\n", $desc;
82 is `echo \$\Q$key\E`, "$val\n", $desc;
87 # On VMS, environment variable changes are peristent after perl exits
90 delete $ENV{'__NoNeSuCh'};
94 eval '$ENV{"FOO"} = "hi there";'; # check that ENV is inited inside eval
95 # cmd.exe will echo 'variable=value' but 4nt will echo just the value
97 if ($Is_MSWin32) { like `set FOO`, qr/^(?:FOO=)?hi there$/; }
98 elsif ($Is_VMS) { is `write sys\$output f\$trnlnm("FOO")`, "hi there\n"; }
99 else { is `echo \$FOO`, "hi there\n"; }
101 unlink_all 'ajslkdfpqjsjfk';
103 open(FOO,'ajslkdfpqjsjfk');
105 close FOO; # just mention it, squelch used-only-once
108 skip('SIGINT not safe on this platform', 5)
109 if $Is_MSWin32 || $Is_NetWare || $Is_Dos;
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.
112 # We use a pipe rather than system() because the VMS command buffer
113 # would overflow with a command that long.
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__;
121 open( CMDPIPE, "| $PERL");
123 print CMDPIPE "\$t1 = $tn[1]; \$t2 = $tn[2];\n", <<'END';
125 $| = 1; # command buffering
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";
132 if (($x = pop(@_)) eq "INT") {
136 print "not ok $t1 ($x @_)\n";
144 open( CMDPIPE, "| $PERL");
145 print CMDPIPE "\$t3 = $tn[3];\n", <<'END';
156 $| = 1; # command buffering
159 local $SIG{"INT"}=x();
160 print ""; # Needed to expose failure in 5.8.0 (why?)
164 kill "INT",$$; sleep 1;
170 $? >>= 8 if $^O eq 'VMS'; # POSIX status hiding in 2nd byte
171 my $todo = ($^O eq 'os2' ? ' # TODO: EMX v0.9d_fix4 bug: wrong nibble? ' : '');
172 print $? & 0xFF ? "ok $tn[4]$todo\n" : "not ok $tn[4]$todo\n";
174 open(CMDPIPE, "| $PERL");
175 print CMDPIPE <<'END';
177 sub PVBM () { 'foo' }
184 kill "INT", $$; sleep 1;
187 $? >>= 8 if $^O eq 'VMS';
188 print $? ? "not ok $tn[5]\n" : "ok $tn[5]\n";
190 curr_test(curr_test() + 5);
194 @val1 = @ENV{keys(%ENV)};
195 @val2 = values(%ENV);
196 is join(':',@val1), join(':',@val2);
197 cmp_ok @val1, '>', 1;
200 'foobarbaz' =~ /b(a)r/;
209 qq < \@$_; q "fff" =~ /(?!^)./; print "[\$$_]\\n" >,
211 "referencing \@$_ before \$$_ etc. still saws off ampersands";
215 @a = qw(foo bar baz);
216 is "@a", "foo bar baz";
219 is "@a", "foo,bar,baz";
224 $h{'foo', 'bar'} = 1;
225 is((keys %h)[0], "foo\034bar");
229 $h{'foo', 'bar'} = 1;
230 is((keys %h)[0], 'fooxbar');
234 system qq[$PERL "-I../lib" -e "use vmsish qw(hushed); exit(0)"];
236 system qq[$PERL "-I../lib" -e "use vmsish qw(hushed); exit(1)"];
239 eval { die "foo\n" };
245 is $$, 42, '$$ can be modified';
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';
256 $::NO_ENDING = 1; # silence "Looks like you only ran..."
260 $$ = $pid; # Tests below use $$
264 my $is_abs = $Config{d_procselfexe} || $Config{usekernprocpathname}
265 || $Config{usensgetexecutablepath};
267 chomp($wd = `/usr/bin/fullpath -t`);
269 elsif($Is_Cygwin || $is_abs) {
270 # Cygwin turns the symlink into the real file
273 $wd =~ /(.*)/; $wd = $1; # untaint
275 $wd = Cygwin::win_to_posix_path(Cygwin::posix_to_win_path($wd, 1));
279 $wd = Cwd::sys_cwd();
284 my $perl = $Is_VMS || $is_abs ? $^X : "$wd/perl";
286 my $middlemaybe = '';
288 $script = "$wd/show-shebang";
292 $perl = "$wd/perl.exe";
293 $script = "$wd/show-shebang.bat";
308 $script = "./show-shebang";
311 $script = "[]show-shebang";
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));
319 if ($^O eq 'os390' or $^O eq 'posix-bc') { # no shebang
321 eval 'exec ./perl -S \$0 \${1+"\$\@"}'
325 $s1 = "\$^X is $perl, \$0 is $script\n";
326 ok open(SCRIPT, ">$script") or diag "Can't write to $script: $!";
327 ok print(SCRIPT $headmaybe . <<EOB . $middlemaybe . <<'EOF' . $tailmaybe) or diag $!;
330 print "\$^X is $^X, \$0 is $0\n";
332 ok close(SCRIPT) or diag $!;
333 ok chmod(0755, $script) or diag $!;
334 $_ = $Is_VMS ? `$perl $script` : `$script`;
335 s/\.exe//i if $Is_Dos or $Is_Cygwin or $Is_os2;
336 s{./$script}{$script} if $Is_BeOS; # revert BeOS execvp() side-effect
337 s{is perl}{is $perl}; # for systems where $^X is only a basename
339 if ($Is_MSWin32 || $Is_os2) {
344 $_ = `$perl $script`;
345 s/\.exe//i if $Is_Dos or $Is_os2 or $Is_Cygwin;
346 s{./$perl}{$perl} if $Is_BeOS; # revert BeOS execvp() side-effect
348 if ($Is_MSWin32 || $Is_os2) {
353 ok unlink($script) or diag $!;
355 # Could this be replaced with:
356 # unlink_all($script);
360 cmp_ok $], '>=', 5.00319;
362 cmp_ok $^T, '>', 850000000;
364 # Test change 25062 is working
365 my $orig_osname = $^O;
368 is $^O, $orig_osname, 'Assigning $^I does not clobber $^O';
374 foreach my $p (0, 1) {
375 fresh_perl_is(<<"EOP", '2 4 8', undef, "test \$^P = $p");
380 print "\$DB::single \$DB::trace \$DB::signal";
385 # Check that assigning to $0 on Linux sets the process name with both
386 # argv[0] assignment and by calling prctl()
389 skip "We don't have prctl() here", 2 unless $Config{d_prctl_set_name};
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.
401 skip "Couldn't shell out to '$cmd', returned code $?", 2 if $?;
405 my $name = "Good Morning, Dave";
408 chomp(my $argv0 = $maybe_ps->("ps h $$"));
409 chomp(my $prctl = $maybe_ps->("ps hc $$"));
411 like($argv0, $name, "Set process name through argv[0] ($argv0)");
412 like($prctl, substr($name, 0, 15), "Set process name through prctl() ($prctl)");
419 local $SIG{'__WARN__'} = sub { $ok = 0; $warn = join '', @_; $warn =~ s/\n$//; };
421 local $TODO = $Is_VMS ? "'\$!=undef' does throw a warning" : '';
426 skip_if_miniperl("miniperl can't rely on loading %Errno", 2);
429 # Make sure Errno hasn't been prematurely autoloaded
433 # Test auto-loading of Errno when %! is used
442 skip_if_miniperl("miniperl can't rely on loading %Errno", 2);
443 # Make sure that Errno loading doesn't clobber $!
446 delete $INC{"Errno.pm"};
448 open(FOO, "nonesuch"); # Generate ENOENT
449 my %errs = %{"!"}; # Cause Errno.pm to be loaded at run-time
452 # Make sure defined(*{"!"}) before %! does not stop %! from working
455 prog => 'BEGIN { defined *{q-!-} } print qq-ok\n- if tied %!',
458 'defined *{"!"} does not stop %! from working';
461 # Check that we don't auto-load packages
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";
472 fresh_perl_is(<<"EOP", 0, {}, $desc);
473 use strict qw(vars subs);
474 my \$symbol = '$symbol';
477 print scalar %${package}::;
485 eval " BEGIN { ok ! defined \$^S } ";
488 my $taint = ${^TAINT};
489 is ${^TAINT}, $taint;
490 eval { ${^TAINT} = 1 };
491 is ${^TAINT}, $taint;
493 # 5.6.1 had a bug: @+ and @- were not properly interpolated
494 # into double-quoted strings
495 # 20020414 mjd-perl-patch+@plover.com
496 "I like pie" =~ /(I) (like) (pie)/;
498 is "@+", "10 1 6 10";
500 # Tests for the magic get of $\
513 $ok = "a$\b" eq "aa\0bb";
518 # Test for bug [perl #36434]
519 # Can not do this test on VMS, EPOC, and SYMBIAN according to comments
520 # in mg.c/Perl_magic_clear_all_env()
522 skip('Can\'t make assignment to \%ENV on this system', 3) if $Is_VMS;
526 # This used to be __PACKAGE__, but that causes recursive
527 # inheritance, which is detected earlier now and broke
529 eval { push @ISA, __FILE__ };
530 is $@, '', 'Push a constant on a magic array';
532 eval { %ENV = (PATH => __PACKAGE__) };
533 is $@, '', 'Assign a constant to a magic hash';
535 eval { my %h = qw(A B); %ENV = (PATH => (keys %h)[0]) };
536 is $@, '', 'Assign a shared key to a magic hash';
540 # Tests for Perl_magic_clearsig
541 foreach 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";
549 # And now one which doesn't exist;
551 no warnings 'signal';
552 $SIG{HUNGRY} = 'mmm, pie';
554 is $SIG{HUNGRY}, 'mmm, pie', 'Can assign to HUNGRY';
555 is delete $SIG{HUNGRY}, 'mmm, pie', 'Can delete from HUNGRY';
556 is $SIG{HUNGRY}, undef, "HUNGRY is now gone";
557 is delete $SIG{HUNGRY}, undef, "HUNGRY remains gone";
559 # Test deleting signals that we never set
560 foreach my $sig (qw(__DIE__ _BOGUS_HOOK KILL THIRSTY)) {
561 is $SIG{$sig}, undef, "$sig is not present";
562 is delete $SIG{$sig}, undef, "delete of $sig returns undef";
567 is int $!, 9999, q{[perl #72850] Core dump in bleadperl from perl -e '$! = 9999; $a = $!;'};
573 skip_if_miniperl("No XS in miniperl", 2);
574 # Make sure defined(*{"+"}) before %+ does not stop %+ from working
577 prog => 'BEGIN { defined *{q-+-} } print qq-ok\n- if tied %+',
580 'defined *{"+"} does not stop %+ from working';
583 prog => 'BEGIN { defined *{q=-=} } print qq-ok\n- if tied %-',
586 'defined *{"-"} does not stop %- from working';
590 skip_if_miniperl("No XS in miniperl", 3);
592 for ( [qw( %- Tie::Hash::NamedCapture )], [qw( $[ arybase )],
594 my ($var, $mod) = @$_;
595 my $modfile = $mod =~ s|::|/|gr . ".pm";
597 qq 'sub UNIVERSAL::AUTOLOAD{}
600 print "ok\\n" if \$INC{"$modfile"}',
602 { switches => [ '-X' ] },
603 "$var still loads $mod when stash and UNIVERSAL::AUTOLOAD exist";
609 is ${^LAST_FH}, \*STDOUT, '${^LAST_FH} after tell';
611 is ${^LAST_FH}, \*STDIN, '${^LAST_FH} after another tell';
615 is ${^LAST_FH}, \$fh, '${^LAST_FH} referencing lexical coercible glob';
617 # This also tests that ${^LAST_FH} is a weak reference:
618 is ${^LAST_FH}, undef, '${^LAST_FH} is undef when PL_last_in_gv is NULL';
621 # ^^^^^^^^^ New tests go here ^^^^^^^^^
624 skip("%ENV manipulations fail or aren't safe on $^O", 19)
628 skip("clearing \%ENV is not safe when running under valgrind or on VMS")
629 if $ENV{PERL_VALGRIND} || $Is_VMS;
632 $PDL = $ENV{PERL_DESTRUCT_LEVEL} || 0;
636 $ENV{PERL_DESTRUCT_LEVEL} = $PDL || 0;
638 is `set foo 2>NUL`, "";
640 is `echo \$foo`, "\n";
644 $ENV{__NoNeSuCh} = 'foo';
646 env_is(__NoNeSuCh => 'foo', 'setting $0 does not break %ENV');
650 env_is(foo => '*main::TODO', 'ENV store of stringified glob');
655 env_is(foo => "$ref", 'ENV store of stringified ref');
657 # downgrade utf8 when possible
658 $bytes = "eh zero \x{A0}";
659 utf8::upgrade($chars = $bytes);
660 $forced = $ENV{foo} = $chars;
661 ok(!utf8::is_utf8($forced) && $forced eq $bytes, 'ENV store downgrades utf8 in SV');
662 env_is(foo => $bytes, 'ENV store downgrades utf8 in setenv');
664 # warn when downgrading utf8 is not possible
665 $chars = "X-Day \x{1998}";
666 utf8::encode($bytes = $chars);
669 local $SIG{__WARN__} = sub { ++$warned if $_[0] =~ /^Wide character in setenv/; print "# @_" };
670 $forced = $ENV{foo} = $chars;
671 ok($warned == 1, 'ENV store warns about wide characters');
673 ok(!utf8::is_utf8($forced) && $forced eq $bytes, 'ENV store encodes high utf8 in SV');
674 env_is(foo => $bytes, 'ENV store encodes high utf8 in SV');
676 # test local $ENV{foo} on existing foo
678 local $ENV{__NoNeSuCh};
679 { local $TODO = 'exists on %ENV should reflect real env';
680 ok(!exists $ENV{__NoNeSuCh}, 'not exists $ENV{existing} during local $ENV{existing}'); }
681 env_is(__NoNeLoCaL => '');
683 ok(exists $ENV{__NoNeSuCh}, 'exists $ENV{existing} after local $ENV{existing}');
684 env_is(__NoNeSuCh => 'foo');
686 # test local $ENV{foo} on new foo
688 local $ENV{__NoNeLoCaL} = 'foo';
689 ok(exists $ENV{__NoNeLoCaL}, 'exists $ENV{new} during local $ENV{new}');
690 env_is(__NoNeLoCaL => 'foo');
692 ok(!exists $ENV{__NoNeLoCaL}, 'not exists $ENV{new} after local $ENV{new}');
693 env_is(__NoNeLoCaL => '');
696 skip("\$0 check only on Linux and FreeBSD", 2)
697 unless $^O =~ /^(linux|freebsd)$/
698 && open CMDLINE, "/proc/$$/cmdline";
700 chomp(my $line = scalar <CMDLINE>);
701 my $me = (split /\0/, $line)[0];
702 is $me, $0, 'altering $0 is effective (testing with /proc/)';
705 my $mydollarzero = sub {
707 $0 = $arg if defined $arg;
708 # In FreeBSD the ps -o command= will cause
709 # an empty header line, grab only the last line.
710 my $ps = (`ps -o command= -p $$`)[-1];
713 printf "# 0[%s]ps[%s]\n", $0, $ps;
716 my $ps = $mydollarzero->("x");
717 ok(!$ps # we allow that something goes wrong with the ps command
718 # In Linux 2.4 we would get an exact match ($ps eq 'x') but
719 # in Linux 2.2 there seems to be something funny going on:
720 # it seems as if the original length of the argv[] would
721 # be stored in the proc struct and then used by ps(1),
722 # no matter what characters we use to pad the argv[].
723 # (And if we use \0:s, they are shown as spaces.) Sigh.
725 # FreeBSD cannot get rid of both the leading "perl :"
726 # and the trailing " (perl)": some FreeBSD versions
727 # can get rid of the first one.
728 || ($^O eq 'freebsd' && $ps =~ m/^(?:perl: )?x(?: \(perl\))?$/),
729 'altering $0 is effective (testing with `ps`)');
733 # test case-insignificance of %ENV (these tests must be enabled only
734 # when perl is compiled with -DENV_IS_CASELESS)
736 skip('no caseless %ENV support', 4) unless $Is_MSWin32 || $Is_NetWare;
741 is scalar(keys(%ENV)), 1;
742 ok exists $ENV{'FOo'};
743 is delete $ENV{'foO'}, 'baz';
744 is scalar(keys(%ENV)), 0;
749 # Put new tests before the various ENV tests, as they blow %ENV away.