This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
5.9.1 suidperl
[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';
774d564b 7 $SIG{__WARN__} = sub { die "Dying on warning: ", @_ };
90ce63d5 8}
8d063cd8 9
9f1b1f2d 10use warnings;
04fee9b5 11use Config;
9f1b1f2d 12
0bee926d 13my $test = 1;
90ce63d5 14sub ok {
0bee926d
MS
15 my($ok, $info, $todo) = @_;
16
17 # You have to do it this way or VMS will get confused.
18 printf "%s $test%s\n", $ok ? "ok" : "not ok",
19 $todo ? " # TODO $todo" : '';
20
21 unless( $ok ) {
22 printf "# Failed test at line %d\n", (caller)[2];
09fdc078 23 print "# $info\n" if defined $info;
90ce63d5 24 }
0bee926d
MS
25
26 $test++;
27 return $ok;
28}
29
30sub skip {
31 my($reason) = @_;
32
33 printf "ok $test # skipped%s\n", defined $reason ? ": $reason" : '';
34
35 $test++;
36 return 1;
90ce63d5
RS
37}
38
ecce83c2 39print "1..53\n";
0bee926d 40
43651d81
NC
41$Is_MSWin32 = $^O eq 'MSWin32';
42$Is_NetWare = $^O eq 'NetWare';
43$Is_VMS = $^O eq 'VMS';
44$Is_Dos = $^O eq 'dos';
45$Is_os2 = $^O eq 'os2';
46$Is_Cygwin = $^O eq 'cygwin';
47$Is_MacOS = $^O eq 'MacOS';
48$Is_MPE = $^O eq 'mpeix';
49$Is_miniperl = $ENV{PERL_CORE_MINITEST};
be708cc0 50
c363d00c
CB
51$PERL = ($Is_NetWare ? 'perl' :
52 ($Is_MacOS || $Is_VMS) ? $^X :
53 $Is_MSWin32 ? '.\perl' :
be708cc0 54 './perl');
68dc0745 55
39e571d4 56eval '$ENV{"FOO"} = "hi there";'; # check that ENV is inited inside eval
26f6e342
NK
57# cmd.exe will echo 'variable=value' but 4nt will echo just the value
58# -- Nikola Knezevic
8efe09f7 59if ($Is_MSWin32) { ok `set FOO` =~ /^(?:FOO=)?hi there$/; }
be708cc0 60elsif ($Is_MacOS) { ok "1 # skipped", 1; }
c363d00c 61elsif ($Is_VMS) { ok `write sys\$output f\$trnlnm("FOO")` eq "hi there\n"; }
be708cc0 62else { ok `echo \$FOO` eq "hi there\n"; }
8d063cd8 63
bf38876a 64unlink 'ajslkdfpqjsjfk';
8d063cd8 65$! = 0;
90ce63d5 66open(FOO,'ajslkdfpqjsjfk');
0bee926d 67ok $!, $!;
90ce63d5 68close FOO; # just mention it, squelch used-only-once
8d063cd8 69
be708cc0 70if ($Is_MSWin32 || $Is_NetWare || $Is_Dos || $Is_MPE || $Is_MacOS) {
2d4fcd5e 71 skip('SIGINT not safe on this platform') for 1..4;
68dc0745
PP
72}
73else {
c363d00c
CB
74 # the next tests are done in a subprocess because sh spits out a
75 # newline onto stderr when a child process kills itself with SIGINT.
04fee9b5 76 # We use a pipe rather than system() because the VMS command buffer
c363d00c
CB
77 # would overflow with a command that long.
78
79 open( CMDPIPE, "| $PERL");
80
81 print CMDPIPE <<'END';
378cc40b 82
79072805 83 $| = 1; # command buffering
378cc40b 84
b715f106
MB
85 $SIG{"INT"} = "ok3"; kill "INT",$$; sleep 1;
86 $SIG{"INT"} = "IGNORE"; kill "INT",$$; sleep 1; print "ok 4\n";
0bee926d 87 $SIG{"INT"} = "DEFAULT"; kill "INT",$$; sleep 1; print "not ok 4\n";
79072805
LW
88
89 sub ok3 {
90 if (($x = pop(@_)) eq "INT") {
91 print "ok 3\n";
92 }
93 else {
652ed9f8 94 print "not ok 3 ($x @_)\n";
79072805
LW
95 }
96 }
97
98END
c363d00c
CB
99
100 close CMDPIPE;
101
2d4fcd5e
AJ
102 open( CMDPIPE, "| $PERL");
103 print CMDPIPE <<'END';
104
105 { package X;
106 sub DESTROY {
107 kill "INT",$$;
108 }
109 }
110 sub x {
111 my $x=bless [], 'X';
112 return sub { $x };
113 }
114 $| = 1; # command buffering
115 $SIG{"INT"} = "ok5";
116 {
117 local $SIG{"INT"}=x();
118 print ""; # Needed to expose failure in 5.8.0 (why?)
119 }
120 sleep 1;
121 delete $SIG{"INT"};
122 kill "INT",$$; sleep 1;
123 sub ok5 {
124 print "ok 5\n";
125 }
126END
127 close CMDPIPE;
bb4e15c8 128 $? >>= 8 if $^O eq 'VMS'; # POSIX status hiding in 2nd byte
639cf43b
IZ
129 my $todo = ($^O eq 'os2' ? ' # TODO: EMX v0.9d_fix4 bug: wrong nibble? ' : '');
130 print $? & 0xFF ? "ok 6$todo\n" : "not ok 6$todo\n";
2d4fcd5e
AJ
131
132 $test += 4;
68dc0745 133}
a687059c 134
68dc0745
PP
135# can we slice ENV?
136@val1 = @ENV{keys(%ENV)};
a687059c 137@val2 = values(%ENV);
0bee926d
MS
138ok join(':',@val1) eq join(':',@val2);
139ok @val1 > 1;
90ce63d5
RS
140
141# regex vars
142'foobarbaz' =~ /b(a)r/;
0bee926d
MS
143ok $` eq 'foo', $`;
144ok $& eq 'bar', $&;
145ok $' eq 'baz', $';
146ok $+ eq 'a', $+;
90ce63d5
RS
147
148# $"
149@a = qw(foo bar baz);
0bee926d 150ok "@a" eq "foo bar baz", "@a";
90ce63d5
RS
151{
152 local $" = ',';
0bee926d 153 ok "@a" eq "foo,bar,baz", "@a";
90ce63d5 154}
a687059c 155
90ce63d5
RS
156# $;
157%h = ();
158$h{'foo', 'bar'} = 1;
0bee926d 159ok((keys %h)[0] eq "foo\034bar", (keys %h)[0]);
90ce63d5
RS
160{
161 local $; = 'x';
162 %h = ();
163 $h{'foo', 'bar'} = 1;
0bee926d 164 ok((keys %h)[0] eq 'fooxbar', (keys %h)[0]);
90ce63d5 165}
ed6116ce 166
90ce63d5 167# $?, $@, $$
dc459aad
JH
168if ($Is_MacOS) {
169 skip('$? + system are broken on MacPerl') for 1..2;
170}
171else {
172 system qq[$PERL "-I../lib" -e "use vmsish qw(hushed); exit(0)"];
173 ok $? == 0, $?;
174 system qq[$PERL "-I../lib" -e "use vmsish qw(hushed); exit(1)"];
175 ok $? != 0, $?;
176}
90ce63d5
RS
177
178eval { die "foo\n" };
0bee926d 179ok $@ eq "foo\n", $@;
90ce63d5 180
0bee926d 181ok $$ > 0, $$;
306196c3
MS
182eval { $$++ };
183ok $@ =~ /^Modification of a read-only value attempted/;
90ce63d5
RS
184
185# $^X and $0
ed37317b 186{
3e3baf6d 187 if ($^O eq 'qnx') {
7fbf1995 188 chomp($wd = `/usr/bin/fullpath -t`);
68dc0745 189 }
04fee9b5 190 elsif($Is_Cygwin || $Config{'d_procselfexe'}) {
1cab015a
FE
191 # Cygwin turns the symlink into the real file
192 chomp($wd = `pwd`);
193 $wd =~ s#/t$##;
194 }
ed344e4f
IZ
195 elsif($Is_os2) {
196 $wd = Cwd::sys_cwd();
197 }
be708cc0
JH
198 elsif($Is_MacOS) {
199 $wd = ':';
200 }
68dc0745
PP
201 else {
202 $wd = '.';
203 }
c363d00c 204 my $perl = ($Is_MacOS || $Is_VMS) ? $^X : "$wd/perl";
ed37317b
TB
205 my $headmaybe = '';
206 my $tailmaybe = '';
68dc0745 207 $script = "$wd/show-shebang";
ed37317b
TB
208 if ($Is_MSWin32) {
209 chomp($wd = `cd`);
8ac9c18d
GS
210 $wd =~ s|\\|/|g;
211 $perl = "$wd/perl.exe";
212 $script = "$wd/show-shebang.bat";
ed37317b
TB
213 $headmaybe = <<EOH ;
214\@rem ='
215\@echo off
216$perl -x \%0
217goto endofperl
218\@rem ';
219EOH
220 $tailmaybe = <<EOT ;
221
222__END__
223:endofperl
224EOT
225 }
ed344e4f
IZ
226 elsif ($Is_os2) {
227 $script = "./show-shebang";
228 }
be708cc0
JH
229 elsif ($Is_MacOS) {
230 $script = ":show-shebang";
231 }
c363d00c
CB
232 elsif ($Is_VMS) {
233 $script = "[]show-shebang";
be708cc0 234 }
a1a0e61e 235 if ($^O eq 'os390' or $^O eq 'posix-bc' or $^O eq 'vmesa') { # no shebang
9d116dd7
JH
236 $headmaybe = <<EOH ;
237 eval 'exec ./perl -S \$0 \${1+"\$\@"}'
238 if 0;
239EOH
240 }
2eecd615 241 $s1 = "\$^X is $perl, \$0 is $script\n";
0bee926d
MS
242 ok open(SCRIPT, ">$script"), $!;
243 ok print(SCRIPT $headmaybe . <<EOB . <<'EOF' . $tailmaybe), $!;
774d564b
PP
244#!$wd/perl
245EOB
90ce63d5
RS
246print "\$^X is $^X, \$0 is $0\n";
247EOF
0bee926d
MS
248 ok close(SCRIPT), $!;
249 ok chmod(0755, $script), $!;
c363d00c 250 $_ = ($Is_MacOS || $Is_VMS) ? `$perl $script` : `$script`;
ed344e4f 251 s/\.exe//i if $Is_Dos or $Is_Cygwin or $Is_os2;
68dc0745 252 s{\bminiperl\b}{perl}; # so that test doesn't fail with miniperl
ed37317b 253 s{is perl}{is $perl}; # for systems where $^X is only a basename
a6c40364 254 s{\\}{/}g;
0bee926d 255 ok((($Is_MSWin32 || $Is_os2) ? uc($_) eq uc($s1) : $_ eq $s1), " :$_:!=:$s1:");
ed37317b 256 $_ = `$perl $script`;
ed344e4f 257 s/\.exe//i if $Is_Dos or $Is_os2;
a6c40364 258 s{\\}{/}g;
0bee926d 259 ok((($Is_MSWin32 || $Is_os2) ? uc($_) eq uc($s1) : $_ eq $s1), " :$_:!=:$s1: after `$perl $script`");
22fc9b38 260 ok unlink($script), $!;
68dc0745 261}
ed6116ce 262
90ce63d5 263# $], $^O, $^T
0bee926d
MS
264ok $] >= 5.00319, $];
265ok $^O;
266ok $^T > 850000000, $^T;
66b1d557 267
be708cc0 268if ($Is_VMS || $Is_Dos || $Is_MacOS) {
44d95355 269 skip("%ENV manipulations fail or aren't safe on $^O") for 1..4;
66b1d557
HM
270}
271else {
da51b73c
MHM
272 if ($ENV{PERL_VALGRIND}) {
273 skip("clearing \%ENV is not safe when running under valgrind");
274 } else {
275 $PATH = $ENV{PATH};
276 $PDL = $ENV{PERL_DESTRUCT_LEVEL} || 0;
277 $ENV{foo} = "bar";
278 %ENV = ();
279 $ENV{PATH} = $PATH;
280 $ENV{PERL_DESTRUCT_LEVEL} = $PDL || 0;
281 ok ($Is_MSWin32 ? (`set foo 2>NUL` eq "")
282 : (`echo \$foo` eq "\n") );
283 }
3e3baf6d 284
ec00bdd8 285 $ENV{__NoNeSuCh} = "foo";
3e3baf6d 286 $0 = "bar";
26f6e342
NK
287# cmd.exe will echo 'variable=value' but 4nt will echo just the value
288# -- Nikola Knezevic
8efe09f7 289 ok ($Is_MSWin32 ? (`set __NoNeSuCh` =~ /^(?:__NoNeSuCh=)?foo$/)
ec00bdd8 290 : (`echo \$__NoNeSuCh` eq "foo\n") );
09fdc078
SR
291 if ($^O =~ /^(linux|freebsd)$/ &&
292 open CMDLINE, "/proc/$$/cmdline") {
293 chomp(my $line = scalar <CMDLINE>);
294 my $me = (split /\0/, $line)[0];
ecce83c2 295 ok($me eq $0, 'altering $0 is effective (testing with /proc/)');
09fdc078 296 close CMDLINE;
ecce83c2
JH
297 # perlbug #22811
298 my $mydollarzero = sub {
299 my($arg) = shift;
300 $0 = $arg if defined $arg;
fbd3c14b
SR
301 # In FreeBSD the ps -o command= will cause
302 # an empty header line, grab only the last line.
303 my $ps = (`ps -o command= -p $$`)[-1];
ecce83c2
JH
304 return if $?;
305 chomp $ps;
306 printf "# 0[%s]ps[%s]\n", $0, $ps;
307 $ps;
308 };
309 my $ps = $mydollarzero->("x");
e26ae24d 310 ok(!$ps # we allow that something goes wrong with the ps command
80bca1b4
JH
311 # In Linux 2.4 we would get an exact match ($ps eq 'x') but
312 # in Linux 2.2 there seems to be something funny going on:
313 # it seems as if the original length of the argv[] would
314 # be stored in the proc struct and then used by ps(1),
315 # no matter what characters we use to pad the argv[].
316 # (And if we use \0:s, they are shown as spaces.) Sigh.
317 || $ps =~ /^x\s*$/
6a4647a3
JH
318 # FreeBSD cannot get rid of both the leading "perl :"
319 # and the trailing " (perl)": some FreeBSD versions
320 # can get rid of the first one.
d2e0b13f 321 || ($^O eq 'freebsd' && $ps =~ m/^(?:perl: )?x(?: \(perl\))?$/),
e26ae24d 322 'altering $0 is effective (testing with `ps`)');
09fdc078 323 } else {
651aa52e 324 skip("\$0 check only on Linux and FreeBSD") for 0, 1;
09fdc078 325 }
66b1d557 326}
3e3baf6d 327
c7213721 328{
a45269de
MS
329 my $ok = 1;
330 my $warn = '';
331 local $SIG{'__WARN__'} = sub { $ok = 0; $warn = join '', @_; };
78987ded 332 $! = undef;
a45269de 333 ok($ok, $warn, $Is_VMS ? "'\$!=undef' does throw a warning" : '');
78987ded
HS
334}
335
902173a3
GS
336# test case-insignificance of %ENV (these tests must be enabled only
337# when perl is compiled with -DENV_IS_CASELESS)
2986a63f 338if ($Is_MSWin32 || $Is_NetWare) {
902173a3
GS
339 %ENV = ();
340 $ENV{'Foo'} = 'bar';
341 $ENV{'fOo'} = 'baz';
0bee926d
MS
342 ok (scalar(keys(%ENV)) == 1);
343 ok exists($ENV{'FOo'});
344 ok (delete($ENV{'foO'}) eq 'baz');
345 ok (scalar(keys(%ENV)) == 0);
902173a3
GS
346}
347else {
0bee926d 348 skip('no caseless %ENV support') for 1..4;
902173a3 349}
d2c93421 350
43651d81
NC
351if ($Is_miniperl) {
352 skip ("miniperl can't rely on loading %Errno");
353} else {
126c71c8
YST
354 no warnings 'void';
355
d2c93421
RH
356# Make sure Errno hasn't been prematurely autoloaded
357
126c71c8 358 ok !defined %Errno::;
d2c93421
RH
359
360# Test auto-loading of Errno when %! is used
361
126c71c8
YST
362 ok scalar eval q{
363 %!;
364 defined %Errno::;
365 }, $@;
366}
d2c93421 367
43651d81
NC
368if ($Is_miniperl) {
369 skip ("miniperl can't rely on loading %Errno");
370} else {
371 # Make sure that Errno loading doesn't clobber $!
d2c93421 372
43651d81
NC
373 undef %Errno::;
374 delete $INC{"Errno.pm"};
d2c93421 375
43651d81
NC
376 open(FOO, "nonesuch"); # Generate ENOENT
377 my %errs = %{"!"}; # Cause Errno.pm to be loaded at run-time
378 ok ${"!"}{ENOENT};
379}
a4268c0a 380
b0e6f864 381ok $^S == 0 && defined $^S;
0bee926d 382eval { ok $^S == 1 };
b0e6f864
RGS
383eval " BEGIN { ok ! defined \$^S } ";
384ok $^S == 0 && defined $^S;
7c36658b
MS
385
386ok ${^TAINT} == 0;
387eval { ${^TAINT} = 1 };
388ok ${^TAINT} == 0;
9aa702ec
MJD
389
390# 5.6.1 had a bug: @+ and @- were not properly interpolated
391# into double-quoted strings
392# 20020414 mjd-perl-patch+@plover.com
b64ebf53
JH
393"I like pie" =~ /(I) (like) (pie)/;
394ok "@-" eq "0 0 2 7";
395ok "@+" eq "10 1 6 10";
9aa702ec 396
f28098ff
RGS
397# Tests for the magic get of $\
398{
399 my $ok = 0;
400 # [perl #19330]
401 {
402 local $\ = undef;
403 $\++; $\++;
404 $ok = $\ eq 2;
405 }
406 ok $ok;
407 $ok = 0;
408 {
409 local $\ = "a\0b";
410 $ok = "a$\b" eq "aa\0bb";
411 }
412 ok $ok;
413}