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