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