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