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