This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
mark two magic.t tests as TODO
[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';
0409250f 7 $ENV{PATH} = '/bin' if ${^TAINT};
774d564b 8 $SIG{__WARN__} = sub { die "Dying on warning: ", @_ };
b2978f4e 9 require './test.pl';
90ce63d5 10}
8d063cd8 11
9f1b1f2d 12use warnings;
04fee9b5 13use Config;
9f1b1f2d 14
fd69380d 15plan (tests => 81);
0bee926d 16
43651d81
NC
17$Is_MSWin32 = $^O eq 'MSWin32';
18$Is_NetWare = $^O eq 'NetWare';
19$Is_VMS = $^O eq 'VMS';
20$Is_Dos = $^O eq 'dos';
21$Is_os2 = $^O eq 'os2';
22$Is_Cygwin = $^O eq 'cygwin';
43651d81
NC
23$Is_MPE = $^O eq 'mpeix';
24$Is_miniperl = $ENV{PERL_CORE_MINITEST};
dbc1d986 25$Is_BeOS = $^O eq 'beos';
be708cc0 26
c8d62b71
RGS
27$PERL = $ENV{PERL}
28 || ($Is_NetWare ? 'perl' :
7b903762 29 $Is_VMS ? $^X :
c8d62b71
RGS
30 $Is_MSWin32 ? '.\perl' :
31 './perl');
68dc0745 32
8df0e0ed
JM
33END {
34 # On VMS, environment variable changes are peristent after perl exits
35 delete $ENV{'FOO'} if $Is_VMS;
36}
37
39e571d4 38eval '$ENV{"FOO"} = "hi there";'; # check that ENV is inited inside eval
26f6e342
NK
39# cmd.exe will echo 'variable=value' but 4nt will echo just the value
40# -- Nikola Knezevic
b2978f4e 41if ($Is_MSWin32) { like `set FOO`, qr/^(?:FOO=)?hi there$/; }
b2978f4e
NC
42elsif ($Is_VMS) { is `write sys\$output f\$trnlnm("FOO")`, "hi there\n"; }
43else { is `echo \$FOO`, "hi there\n"; }
8d063cd8 44
bf38876a 45unlink 'ajslkdfpqjsjfk';
8d063cd8 46$! = 0;
90ce63d5 47open(FOO,'ajslkdfpqjsjfk');
b2978f4e 48isnt($!, 0);
90ce63d5 49close FOO; # just mention it, squelch used-only-once
8d063cd8 50
b2978f4e
NC
51SKIP: {
52 skip('SIGINT not safe on this platform', 5)
7b903762 53 if $Is_MSWin32 || $Is_NetWare || $Is_Dos || $Is_MPE;
c363d00c
CB
54 # the next tests are done in a subprocess because sh spits out a
55 # newline onto stderr when a child process kills itself with SIGINT.
04fee9b5 56 # We use a pipe rather than system() because the VMS command buffer
c363d00c
CB
57 # would overflow with a command that long.
58
59 open( CMDPIPE, "| $PERL");
60
61 print CMDPIPE <<'END';
378cc40b 62
79072805 63 $| = 1; # command buffering
378cc40b 64
b715f106
MB
65 $SIG{"INT"} = "ok3"; kill "INT",$$; sleep 1;
66 $SIG{"INT"} = "IGNORE"; kill "INT",$$; sleep 1; print "ok 4\n";
0bee926d 67 $SIG{"INT"} = "DEFAULT"; kill "INT",$$; sleep 1; print "not ok 4\n";
79072805
LW
68
69 sub ok3 {
70 if (($x = pop(@_)) eq "INT") {
71 print "ok 3\n";
72 }
73 else {
652ed9f8 74 print "not ok 3 ($x @_)\n";
79072805
LW
75 }
76 }
77
78END
c363d00c
CB
79
80 close CMDPIPE;
81
2d4fcd5e
AJ
82 open( CMDPIPE, "| $PERL");
83 print CMDPIPE <<'END';
84
85 { package X;
86 sub DESTROY {
87 kill "INT",$$;
88 }
89 }
90 sub x {
91 my $x=bless [], 'X';
92 return sub { $x };
93 }
94 $| = 1; # command buffering
95 $SIG{"INT"} = "ok5";
96 {
97 local $SIG{"INT"}=x();
98 print ""; # Needed to expose failure in 5.8.0 (why?)
99 }
100 sleep 1;
101 delete $SIG{"INT"};
102 kill "INT",$$; sleep 1;
103 sub ok5 {
104 print "ok 5\n";
105 }
106END
107 close CMDPIPE;
bb4e15c8 108 $? >>= 8 if $^O eq 'VMS'; # POSIX status hiding in 2nd byte
639cf43b
IZ
109 my $todo = ($^O eq 'os2' ? ' # TODO: EMX v0.9d_fix4 bug: wrong nibble? ' : '');
110 print $? & 0xFF ? "ok 6$todo\n" : "not ok 6$todo\n";
2d4fcd5e 111
6e592b3a
BM
112 open(CMDPIPE, "| $PERL");
113 print CMDPIPE <<'END';
114
115 sub PVBM () { 'foo' }
116 index 'foo', PVBM;
117 my $pvbm = PVBM;
118
119 sub foo { exit 0 }
120
121 $SIG{"INT"} = $pvbm;
122 kill "INT", $$; sleep 1;
123END
124 close CMDPIPE;
125 $? >>= 8 if $^O eq 'VMS';
126 print $? ? "not ok 7\n" : "ok 7\n";
127
b2978f4e 128 curr_test(curr_test() + 5);
68dc0745 129}
a687059c 130
68dc0745
PP
131# can we slice ENV?
132@val1 = @ENV{keys(%ENV)};
a687059c 133@val2 = values(%ENV);
b2978f4e
NC
134is join(':',@val1), join(':',@val2);
135cmp_ok @val1, '>', 1;
90ce63d5
RS
136
137# regex vars
138'foobarbaz' =~ /b(a)r/;
b2978f4e
NC
139is $`, 'foo';
140is $&, 'bar';
141is $', 'baz';
142is $+, 'a';
90ce63d5
RS
143
144# $"
145@a = qw(foo bar baz);
b2978f4e 146is "@a", "foo bar baz";
90ce63d5
RS
147{
148 local $" = ',';
b2978f4e 149 is "@a", "foo,bar,baz";
90ce63d5 150}
a687059c 151
90ce63d5
RS
152# $;
153%h = ();
154$h{'foo', 'bar'} = 1;
b2978f4e 155is((keys %h)[0], "foo\034bar");
90ce63d5
RS
156{
157 local $; = 'x';
158 %h = ();
159 $h{'foo', 'bar'} = 1;
b2978f4e 160 is((keys %h)[0], 'fooxbar');
90ce63d5 161}
ed6116ce 162
90ce63d5 163# $?, $@, $$
7b903762
RGS
164system qq[$PERL "-I../lib" -e "use vmsish qw(hushed); exit(0)"];
165is $?, 0;
166system qq[$PERL "-I../lib" -e "use vmsish qw(hushed); exit(1)"];
167isnt $?, 0;
90ce63d5
RS
168
169eval { die "foo\n" };
b2978f4e 170is $@, "foo\n";
90ce63d5 171
b2978f4e 172cmp_ok($$, '>', 0);
306196c3 173eval { $$++ };
b2978f4e 174like ($@, qr/^Modification of a read-only value attempted/);
90ce63d5
RS
175
176# $^X and $0
ed37317b 177{
3e3baf6d 178 if ($^O eq 'qnx') {
7fbf1995 179 chomp($wd = `/usr/bin/fullpath -t`);
68dc0745 180 }
04fee9b5 181 elsif($Is_Cygwin || $Config{'d_procselfexe'}) {
1cab015a
FE
182 # Cygwin turns the symlink into the real file
183 chomp($wd = `pwd`);
184 $wd =~ s#/t$##;
0409250f 185 $wd =~ /(.*)/; $wd = $1; # untaint
6178c52a
JH
186 if ($Is_Cygwin) {
187 $wd = Cygwin::win_to_posix_path(Cygwin::posix_to_win_path($wd, 1));
188 }
1cab015a 189 }
ed344e4f
IZ
190 elsif($Is_os2) {
191 $wd = Cwd::sys_cwd();
192 }
68dc0745
PP
193 else {
194 $wd = '.';
195 }
7b903762 196 my $perl = $Is_VMS ? $^X : "$wd/perl";
ed37317b 197 my $headmaybe = '';
6178c52a 198 my $middlemaybe = '';
ed37317b 199 my $tailmaybe = '';
68dc0745 200 $script = "$wd/show-shebang";
ed37317b
TB
201 if ($Is_MSWin32) {
202 chomp($wd = `cd`);
8ac9c18d
GS
203 $wd =~ s|\\|/|g;
204 $perl = "$wd/perl.exe";
205 $script = "$wd/show-shebang.bat";
ed37317b
TB
206 $headmaybe = <<EOH ;
207\@rem ='
208\@echo off
209$perl -x \%0
210goto endofperl
211\@rem ';
212EOH
213 $tailmaybe = <<EOT ;
214
215__END__
216:endofperl
217EOT
218 }
ed344e4f
IZ
219 elsif ($Is_os2) {
220 $script = "./show-shebang";
221 }
c363d00c
CB
222 elsif ($Is_VMS) {
223 $script = "[]show-shebang";
be708cc0 224 }
6178c52a
JH
225 elsif ($Is_Cygwin) {
226 $middlemaybe = <<'EOX'
227$^X = Cygwin::win_to_posix_path(Cygwin::posix_to_win_path($^X, 1));
228$0 = Cygwin::win_to_posix_path(Cygwin::posix_to_win_path($0, 1));
229EOX
230 }
a1a0e61e 231 if ($^O eq 'os390' or $^O eq 'posix-bc' or $^O eq 'vmesa') { # no shebang
9d116dd7
JH
232 $headmaybe = <<EOH ;
233 eval 'exec ./perl -S \$0 \${1+"\$\@"}'
234 if 0;
235EOH
236 }
2eecd615 237 $s1 = "\$^X is $perl, \$0 is $script\n";
0409250f 238 ok open(SCRIPT, ">$script") or diag "Can't write to $script: $!";
b2978f4e 239 ok print(SCRIPT $headmaybe . <<EOB . $middlemaybe . <<'EOF' . $tailmaybe) or diag $!;
774d564b
PP
240#!$wd/perl
241EOB
90ce63d5
RS
242print "\$^X is $^X, \$0 is $0\n";
243EOF
b2978f4e
NC
244 ok close(SCRIPT) or diag $!;
245 ok chmod(0755, $script) or diag $!;
7b903762 246 $_ = $Is_VMS ? `$perl $script` : `$script`;
ed344e4f 247 s/\.exe//i if $Is_Dos or $Is_Cygwin or $Is_os2;
dbc1d986 248 s{./$script}{$script} if $Is_BeOS; # revert BeOS execvp() side-effect
68dc0745 249 s{\bminiperl\b}{perl}; # so that test doesn't fail with miniperl
ed37317b 250 s{is perl}{is $perl}; # for systems where $^X is only a basename
a6c40364 251 s{\\}{/}g;
b2978f4e
NC
252 if ($Is_MSWin32 || $Is_os2) {
253 is uc $_, uc $s1;
254 } else {
255 is $_, $s1;
256 }
ed37317b 257 $_ = `$perl $script`;
4bbb7126 258 s/\.exe//i if $Is_Dos or $Is_os2 or $Is_Cygwin;
dbc1d986 259 s{./$perl}{$perl} if $Is_BeOS; # revert BeOS execvp() side-effect
a6c40364 260 s{\\}{/}g;
b2978f4e
NC
261 if ($Is_MSWin32 || $Is_os2) {
262 is uc $_, uc $s1;
263 } else {
264 is $_, $s1;
265 }
266 ok unlink($script) or diag $!;
68dc0745 267}
ed6116ce 268
90ce63d5 269# $], $^O, $^T
b2978f4e 270cmp_ok $], '>=', 5.00319;
0bee926d 271ok $^O;
b2978f4e 272cmp_ok $^T, '>', 850000000;
66b1d557 273
881ddac4
SH
274# Test change 25062 is working
275my $orig_osname = $^O;
276{
277local $^I = '.bak';
b2978f4e 278is $^O, $orig_osname, 'Assigning $^I does not clobber $^O';
881ddac4
SH
279}
280$^O = $orig_osname;
281
b2978f4e
NC
282SKIP: {
283 skip("%ENV manipulations fail or aren't safe on $^O", 4)
7b903762 284 if $Is_VMS || $Is_Dos;
b2978f4e
NC
285
286 SKIP: {
287 skip("clearing \%ENV is not safe when running under valgrind")
288 if $ENV{PERL_VALGRIND};
289
da51b73c
MHM
290 $PATH = $ENV{PATH};
291 $PDL = $ENV{PERL_DESTRUCT_LEVEL} || 0;
292 $ENV{foo} = "bar";
293 %ENV = ();
294 $ENV{PATH} = $PATH;
295 $ENV{PERL_DESTRUCT_LEVEL} = $PDL || 0;
b2978f4e
NC
296 if ($Is_MSWin32) {
297 is `set foo 2>NUL`, "";
298 } else {
299 is `echo \$foo`, "\n";
300 }
da51b73c 301 }
3e3baf6d 302
ec00bdd8 303 $ENV{__NoNeSuCh} = "foo";
3e3baf6d 304 $0 = "bar";
26f6e342
NK
305# cmd.exe will echo 'variable=value' but 4nt will echo just the value
306# -- Nikola Knezevic
b2978f4e
NC
307 if ($Is_MSWin32) {
308 like `set __NoNeSuCh`, qr/^(?:__NoNeSuCh=)?foo$/;
309 } else {
310 is `echo \$__NoNeSuCh`, "foo\n";
311 }
312 SKIP: {
313 skip("\$0 check only on Linux and FreeBSD", 2)
314 unless $^O =~ /^(linux|freebsd)$/
315 && open CMDLINE, "/proc/$$/cmdline";
316
09fdc078
SR
317 chomp(my $line = scalar <CMDLINE>);
318 my $me = (split /\0/, $line)[0];
b2978f4e 319 is $me, $0, 'altering $0 is effective (testing with /proc/)';
09fdc078 320 close CMDLINE;
ecce83c2
JH
321 # perlbug #22811
322 my $mydollarzero = sub {
323 my($arg) = shift;
324 $0 = $arg if defined $arg;
fbd3c14b
SR
325 # In FreeBSD the ps -o command= will cause
326 # an empty header line, grab only the last line.
327 my $ps = (`ps -o command= -p $$`)[-1];
ecce83c2
JH
328 return if $?;
329 chomp $ps;
330 printf "# 0[%s]ps[%s]\n", $0, $ps;
331 $ps;
332 };
333 my $ps = $mydollarzero->("x");
e26ae24d 334 ok(!$ps # we allow that something goes wrong with the ps command
80bca1b4
JH
335 # In Linux 2.4 we would get an exact match ($ps eq 'x') but
336 # in Linux 2.2 there seems to be something funny going on:
337 # it seems as if the original length of the argv[] would
338 # be stored in the proc struct and then used by ps(1),
339 # no matter what characters we use to pad the argv[].
340 # (And if we use \0:s, they are shown as spaces.) Sigh.
341 || $ps =~ /^x\s*$/
6a4647a3
JH
342 # FreeBSD cannot get rid of both the leading "perl :"
343 # and the trailing " (perl)": some FreeBSD versions
344 # can get rid of the first one.
d2e0b13f 345 || ($^O eq 'freebsd' && $ps =~ m/^(?:perl: )?x(?: \(perl\))?$/),
e26ae24d 346 'altering $0 is effective (testing with `ps`)');
09fdc078 347 }
66b1d557 348}
3e3baf6d 349
c7213721 350{
a45269de
MS
351 my $ok = 1;
352 my $warn = '';
ae8ade65 353 local $SIG{'__WARN__'} = sub { $ok = 0; $warn = join '', @_; $warn =~ s/\n$//; };
78987ded 354 $! = undef;
b2978f4e
NC
355 local $TODO = $Is_VMS ? "'\$!=undef' does throw a warning" : '';
356 ok($ok, $warn);
78987ded
HS
357}
358
902173a3
GS
359# test case-insignificance of %ENV (these tests must be enabled only
360# when perl is compiled with -DENV_IS_CASELESS)
b2978f4e
NC
361SKIP: {
362 skip('no caseless %ENV support', 4) unless $Is_MSWin32 || $Is_NetWare;
363
902173a3
GS
364 %ENV = ();
365 $ENV{'Foo'} = 'bar';
366 $ENV{'fOo'} = 'baz';
b2978f4e
NC
367 is scalar(keys(%ENV)), 1;
368 ok exists $ENV{'FOo'};
369 is delete $ENV{'foO'}, 'baz';
370 is scalar(keys(%ENV)), 0;
902173a3 371}
d2c93421 372
b2978f4e
NC
373SKIP: {
374 skip ("miniperl can't rely on loading %Errno", 2) if $Is_miniperl;
126c71c8
YST
375 no warnings 'void';
376
d2c93421
RH
377# Make sure Errno hasn't been prematurely autoloaded
378
b79f7545 379 ok !keys %Errno::;
d2c93421
RH
380
381# Test auto-loading of Errno when %! is used
382
126c71c8
YST
383 ok scalar eval q{
384 %!;
902fde96 385 scalar %Errno::;
126c71c8
YST
386 }, $@;
387}
d2c93421 388
b2978f4e
NC
389SKIP: {
390 skip ("miniperl can't rely on loading %Errno") if $Is_miniperl;
43651d81 391 # Make sure that Errno loading doesn't clobber $!
d2c93421 392
43651d81
NC
393 undef %Errno::;
394 delete $INC{"Errno.pm"};
d2c93421 395
43651d81
NC
396 open(FOO, "nonesuch"); # Generate ENOENT
397 my %errs = %{"!"}; # Cause Errno.pm to be loaded at run-time
398 ok ${"!"}{ENOENT};
399}
a4268c0a 400
b2978f4e
NC
401is $^S, 0;
402eval { is $^S,1 };
b0e6f864 403eval " BEGIN { ok ! defined \$^S } ";
b2978f4e 404is $^S, 0;
7c36658b 405
0409250f
RGS
406my $taint = ${^TAINT};
407is ${^TAINT}, $taint;
7c36658b 408eval { ${^TAINT} = 1 };
0409250f 409is ${^TAINT}, $taint;
9aa702ec
MJD
410
411# 5.6.1 had a bug: @+ and @- were not properly interpolated
412# into double-quoted strings
413# 20020414 mjd-perl-patch+@plover.com
b64ebf53 414"I like pie" =~ /(I) (like) (pie)/;
b2978f4e
NC
415is "@-", "0 0 2 7";
416is "@+", "10 1 6 10";
9aa702ec 417
f28098ff
RGS
418# Tests for the magic get of $\
419{
420 my $ok = 0;
421 # [perl #19330]
422 {
423 local $\ = undef;
424 $\++; $\++;
425 $ok = $\ eq 2;
426 }
427 ok $ok;
428 $ok = 0;
429 {
430 local $\ = "a\0b";
431 $ok = "a$\b" eq "aa\0bb";
432 }
433 ok $ok;
434}
547d1dd8
HS
435
436# Test for bug [perl #27839]
437{
d1515be4 438 local $TODO = "fixing a casting issue revealed broken behaviour in this test";
547d1dd8
HS
439 my $x;
440 sub f {
441 "abc" =~ /(.)./;
442 $x = "@+";
443 return @+;
444 };
445 my @y = f();
b2978f4e 446 is $x, "@y", "return a magic array ($x) vs (@y)";
fd69380d
DM
447
448 sub f2 {
449 "abc" =~ /(?<foo>.)./;
450 my @h = %+;
451 $x = "@h";
452 return %+;
453 };
454 @y = f();
455 is $x, "@y", "return a magic hash ($x) vs (@y)";
547d1dd8 456}
4c9140ed
RGS
457
458# Test for bug [perl #36434]
b2978f4e
NC
459# Can not do this test on VMS, EPOC, and SYMBIAN according to comments
460# in mg.c/Perl_magic_clear_all_env()
461SKIP: {
462 skip('Can\'t make assignment to \%ENV on this system', 3) if $Is_VMS;
463
4c9140ed
RGS
464 local @ISA;
465 local %ENV;
e1a479c5
BB
466 # This used to be __PACKAGE__, but that causes recursive
467 # inheritance, which is detected earlier now and broke
468 # this test
469 eval { push @ISA, __FILE__ };
b2978f4e 470 is $@, '', 'Push a constant on a magic array';
4c9140ed
RGS
471 $@ and print "# $@";
472 eval { %ENV = (PATH => __PACKAGE__) };
b2978f4e 473 is $@, '', 'Assign a constant to a magic hash';
d8084ca5
DM
474 $@ and print "# $@";
475 eval { my %h = qw(A B); %ENV = (PATH => (keys %h)[0]) };
b2978f4e 476 is $@, '', 'Assign a shared key to a magic hash';
4c9140ed
RGS
477 $@ and print "# $@";
478}
179c85a2
NC
479
480# Tests for Perl_magic_clearsig
481foreach my $sig (qw(__WARN__ INT)) {
482 $SIG{$sig} = lc $sig;
483 is $SIG{$sig}, 'main::' . lc $sig, "Can assign to $sig";
484 is delete $SIG{$sig}, 'main::' . lc $sig, "Can delete from $sig";
485 is $SIG{$sig}, undef, "$sig is now gone";
486 is delete $SIG{$sig}, undef, "$sig remains gone";
487}
488
489# And now one which doesn't exist;
490{
491 no warnings 'signal';
492 $SIG{HUNGRY} = 'mmm, pie';
493}
494is $SIG{HUNGRY}, 'mmm, pie', 'Can assign to HUNGRY';
495is delete $SIG{HUNGRY}, 'mmm, pie', 'Can delete from HUNGRY';
496is $SIG{HUNGRY}, undef, "HUNGRY is now gone";
497is delete $SIG{HUNGRY}, undef, "HUNGRY remains gone";
498
499# Test deleting signals that we never set
c8be058c 500foreach my $sig (qw(__DIE__ _BOGUS_HOOK KILL THIRSTY)) {
179c85a2
NC
501 is $SIG{$sig}, undef, "$sig is not present";
502 is delete $SIG{$sig}, undef, "delete of $sig returns undef";
503}
be1cf43c
NC
504
505{
506 $! = 9999;
507 is int $!, 9999, q{[perl #72850] Core dump in bleadperl from perl -e '$! = 9999; $a = $!;'};
508
509}