RE: [perl #20613] Perl_magic_setsig/clearsig problems (patch included)
[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
2d4fcd5e 39print "1..52\n";
0bee926d 40
3e3baf6d 41$Is_MSWin32 = $^O eq 'MSWin32';
2986a63f 42$Is_NetWare = $^O eq 'NetWare';
3e3baf6d 43$Is_VMS = $^O eq 'VMS';
be708cc0 44$Is_Dos = $^O eq 'dos';
45$Is_os2 = $^O eq 'os2';
46$Is_Cygwin = $^O eq 'cygwin';
47$Is_MacOS = $^O eq 'MacOS';
dc22e1c4 48$Is_MPE = $^O eq 'mpeix';
be708cc0 49
c363d00c 50$PERL = ($Is_NetWare ? 'perl' :
51 ($Is_MacOS || $Is_VMS) ? $^X :
52 $Is_MSWin32 ? '.\perl' :
be708cc0 53 './perl');
68dc0745 54
39e571d4 55eval '$ENV{"FOO"} = "hi there";'; # check that ENV is inited inside eval
26f6e342 56# cmd.exe will echo 'variable=value' but 4nt will echo just the value
57# -- Nikola Knezevic
8efe09f7 58if ($Is_MSWin32) { ok `set FOO` =~ /^(?:FOO=)?hi there$/; }
be708cc0 59elsif ($Is_MacOS) { ok "1 # skipped", 1; }
c363d00c 60elsif ($Is_VMS) { ok `write sys\$output f\$trnlnm("FOO")` eq "hi there\n"; }
be708cc0 61else { ok `echo \$FOO` eq "hi there\n"; }
8d063cd8 62
bf38876a 63unlink 'ajslkdfpqjsjfk';
8d063cd8 64$! = 0;
90ce63d5 65open(FOO,'ajslkdfpqjsjfk');
0bee926d 66ok $!, $!;
90ce63d5 67close FOO; # just mention it, squelch used-only-once
8d063cd8 68
be708cc0 69if ($Is_MSWin32 || $Is_NetWare || $Is_Dos || $Is_MPE || $Is_MacOS) {
2d4fcd5e 70 skip('SIGINT not safe on this platform') for 1..4;
68dc0745 71}
72else {
c363d00c 73 # the next tests are done in a subprocess because sh spits out a
74 # newline onto stderr when a child process kills itself with SIGINT.
04fee9b5 75 # We use a pipe rather than system() because the VMS command buffer
c363d00c 76 # would overflow with a command that long.
77
78 open( CMDPIPE, "| $PERL");
79
80 print CMDPIPE <<'END';
378cc40b 81
79072805 82 $| = 1; # command buffering
378cc40b 83
b715f106 84 $SIG{"INT"} = "ok3"; kill "INT",$$; sleep 1;
85 $SIG{"INT"} = "IGNORE"; kill "INT",$$; sleep 1; print "ok 4\n";
0bee926d 86 $SIG{"INT"} = "DEFAULT"; kill "INT",$$; sleep 1; print "not ok 4\n";
79072805 87
88 sub ok3 {
89 if (($x = pop(@_)) eq "INT") {
90 print "ok 3\n";
91 }
92 else {
652ed9f8 93 print "not ok 3 ($x @_)\n";
79072805 94 }
95 }
96
97END
c363d00c 98
99 close CMDPIPE;
100
2d4fcd5e 101 open( CMDPIPE, "| $PERL");
102 print CMDPIPE <<'END';
103
104 { package X;
105 sub DESTROY {
106 kill "INT",$$;
107 }
108 }
109 sub x {
110 my $x=bless [], 'X';
111 return sub { $x };
112 }
113 $| = 1; # command buffering
114 $SIG{"INT"} = "ok5";
115 {
116 local $SIG{"INT"}=x();
117 print ""; # Needed to expose failure in 5.8.0 (why?)
118 }
119 sleep 1;
120 delete $SIG{"INT"};
121 kill "INT",$$; sleep 1;
122 sub ok5 {
123 print "ok 5\n";
124 }
125END
126 close CMDPIPE;
127 print $? & 0xFF ? "ok 6\n" : "not ok 6\n";
128
129 $test += 4;
68dc0745 130}
a687059c 131
68dc0745 132# can we slice ENV?
133@val1 = @ENV{keys(%ENV)};
a687059c 134@val2 = values(%ENV);
0bee926d 135ok join(':',@val1) eq join(':',@val2);
136ok @val1 > 1;
90ce63d5 137
138# regex vars
139'foobarbaz' =~ /b(a)r/;
0bee926d 140ok $` eq 'foo', $`;
141ok $& eq 'bar', $&;
142ok $' eq 'baz', $';
143ok $+ eq 'a', $+;
90ce63d5 144
145# $"
146@a = qw(foo bar baz);
0bee926d 147ok "@a" eq "foo bar baz", "@a";
90ce63d5 148{
149 local $" = ',';
0bee926d 150 ok "@a" eq "foo,bar,baz", "@a";
90ce63d5 151}
a687059c 152
90ce63d5 153# $;
154%h = ();
155$h{'foo', 'bar'} = 1;
0bee926d 156ok((keys %h)[0] eq "foo\034bar", (keys %h)[0]);
90ce63d5 157{
158 local $; = 'x';
159 %h = ();
160 $h{'foo', 'bar'} = 1;
0bee926d 161 ok((keys %h)[0] eq 'fooxbar', (keys %h)[0]);
90ce63d5 162}
ed6116ce 163
90ce63d5 164# $?, $@, $$
dc459aad 165if ($Is_MacOS) {
166 skip('$? + system are broken on MacPerl') for 1..2;
167}
168else {
169 system qq[$PERL "-I../lib" -e "use vmsish qw(hushed); exit(0)"];
170 ok $? == 0, $?;
171 system qq[$PERL "-I../lib" -e "use vmsish qw(hushed); exit(1)"];
172 ok $? != 0, $?;
173}
90ce63d5 174
175eval { die "foo\n" };
0bee926d 176ok $@ eq "foo\n", $@;
90ce63d5 177
0bee926d 178ok $$ > 0, $$;
306196c3 179eval { $$++ };
180ok $@ =~ /^Modification of a read-only value attempted/;
90ce63d5 181
182# $^X and $0
ed37317b 183{
3e3baf6d 184 if ($^O eq 'qnx') {
7fbf1995 185 chomp($wd = `/usr/bin/fullpath -t`);
68dc0745 186 }
04fee9b5 187 elsif($Is_Cygwin || $Config{'d_procselfexe'}) {
1cab015a 188 # Cygwin turns the symlink into the real file
189 chomp($wd = `pwd`);
190 $wd =~ s#/t$##;
191 }
ed344e4f 192 elsif($Is_os2) {
193 $wd = Cwd::sys_cwd();
194 }
be708cc0 195 elsif($Is_MacOS) {
196 $wd = ':';
197 }
68dc0745 198 else {
199 $wd = '.';
200 }
c363d00c 201 my $perl = ($Is_MacOS || $Is_VMS) ? $^X : "$wd/perl";
ed37317b 202 my $headmaybe = '';
203 my $tailmaybe = '';
68dc0745 204 $script = "$wd/show-shebang";
ed37317b 205 if ($Is_MSWin32) {
206 chomp($wd = `cd`);
8ac9c18d 207 $wd =~ s|\\|/|g;
208 $perl = "$wd/perl.exe";
209 $script = "$wd/show-shebang.bat";
ed37317b 210 $headmaybe = <<EOH ;
211\@rem ='
212\@echo off
213$perl -x \%0
214goto endofperl
215\@rem ';
216EOH
217 $tailmaybe = <<EOT ;
218
219__END__
220:endofperl
221EOT
222 }
ed344e4f 223 elsif ($Is_os2) {
224 $script = "./show-shebang";
225 }
be708cc0 226 elsif ($Is_MacOS) {
227 $script = ":show-shebang";
228 }
c363d00c 229 elsif ($Is_VMS) {
230 $script = "[]show-shebang";
be708cc0 231 }
a1a0e61e 232 if ($^O eq 'os390' or $^O eq 'posix-bc' or $^O eq 'vmesa') { # no shebang
9d116dd7 233 $headmaybe = <<EOH ;
234 eval 'exec ./perl -S \$0 \${1+"\$\@"}'
235 if 0;
236EOH
237 }
2eecd615 238 $s1 = "\$^X is $perl, \$0 is $script\n";
0bee926d 239 ok open(SCRIPT, ">$script"), $!;
240 ok print(SCRIPT $headmaybe . <<EOB . <<'EOF' . $tailmaybe), $!;
774d564b 241#!$wd/perl
242EOB
90ce63d5 243print "\$^X is $^X, \$0 is $0\n";
244EOF
0bee926d 245 ok close(SCRIPT), $!;
246 ok chmod(0755, $script), $!;
c363d00c 247 $_ = ($Is_MacOS || $Is_VMS) ? `$perl $script` : `$script`;
ed344e4f 248 s/\.exe//i if $Is_Dos or $Is_Cygwin or $Is_os2;
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;
0bee926d 252 ok((($Is_MSWin32 || $Is_os2) ? uc($_) eq uc($s1) : $_ eq $s1), " :$_:!=:$s1:");
ed37317b 253 $_ = `$perl $script`;
ed344e4f 254 s/\.exe//i if $Is_Dos or $Is_os2;
a6c40364 255 s{\\}{/}g;
0bee926d 256 ok((($Is_MSWin32 || $Is_os2) ? uc($_) eq uc($s1) : $_ eq $s1), " :$_:!=:$s1: after `$perl $script`");
22fc9b38 257 ok unlink($script), $!;
68dc0745 258}
ed6116ce 259
90ce63d5 260# $], $^O, $^T
0bee926d 261ok $] >= 5.00319, $];
262ok $^O;
263ok $^T > 850000000, $^T;
66b1d557 264
be708cc0 265if ($Is_VMS || $Is_Dos || $Is_MacOS) {
09fdc078 266 skip("%ENV manipulations fail or aren't safe on $^O") for 1..3;
66b1d557 267}
268else {
3e3baf6d 269 $PATH = $ENV{PATH};
7ddf2a0a 270 $PDL = $ENV{PERL_DESTRUCT_LEVEL} || 0;
3e3baf6d 271 $ENV{foo} = "bar";
272 %ENV = ();
273 $ENV{PATH} = $PATH;
7ddf2a0a 274 $ENV{PERL_DESTRUCT_LEVEL} = $PDL || 0;
0bee926d 275 ok ($Is_MSWin32 ? (`set foo 2>NUL` eq "")
3e3baf6d 276 : (`echo \$foo` eq "\n") );
277
ec00bdd8 278 $ENV{__NoNeSuCh} = "foo";
3e3baf6d 279 $0 = "bar";
26f6e342 280# cmd.exe will echo 'variable=value' but 4nt will echo just the value
281# -- Nikola Knezevic
8efe09f7 282 ok ($Is_MSWin32 ? (`set __NoNeSuCh` =~ /^(?:__NoNeSuCh=)?foo$/)
ec00bdd8 283 : (`echo \$__NoNeSuCh` eq "foo\n") );
09fdc078 284 if ($^O =~ /^(linux|freebsd)$/ &&
285 open CMDLINE, "/proc/$$/cmdline") {
286 chomp(my $line = scalar <CMDLINE>);
287 my $me = (split /\0/, $line)[0];
e2975953 288 ok($me eq $0, 'altering $0 is effective');
09fdc078 289 close CMDLINE;
290 } else {
291 skip("\$0 check only on Linux and FreeBSD with /proc");
292 }
66b1d557 293}
3e3baf6d 294
c7213721 295{
a45269de 296 my $ok = 1;
297 my $warn = '';
298 local $SIG{'__WARN__'} = sub { $ok = 0; $warn = join '', @_; };
78987ded 299 $! = undef;
a45269de 300 ok($ok, $warn, $Is_VMS ? "'\$!=undef' does throw a warning" : '');
78987ded 301}
302
902173a3 303# test case-insignificance of %ENV (these tests must be enabled only
304# when perl is compiled with -DENV_IS_CASELESS)
2986a63f 305if ($Is_MSWin32 || $Is_NetWare) {
902173a3 306 %ENV = ();
307 $ENV{'Foo'} = 'bar';
308 $ENV{'fOo'} = 'baz';
0bee926d 309 ok (scalar(keys(%ENV)) == 1);
310 ok exists($ENV{'FOo'});
311 ok (delete($ENV{'foO'}) eq 'baz');
312 ok (scalar(keys(%ENV)) == 0);
902173a3 313}
314else {
0bee926d 315 skip('no caseless %ENV support') for 1..4;
902173a3 316}
d2c93421 317
318# Make sure Errno hasn't been prematurely autoloaded
319
0bee926d 320ok !defined %Errno::;
d2c93421 321
322# Test auto-loading of Errno when %! is used
323
0bee926d 324ok scalar eval q{
d2c93421 325 my $errs = %!;
326 defined %Errno::;
327}, $@;
328
329
330# Make sure that Errno loading doesn't clobber $!
331
332undef %Errno::;
333delete $INC{"Errno.pm"};
334
335open(FOO, "nonesuch"); # Generate ENOENT
336my %errs = %{"!"}; # Cause Errno.pm to be loaded at run-time
0bee926d 337ok ${"!"}{ENOENT};
a4268c0a 338
b0e6f864 339ok $^S == 0 && defined $^S;
0bee926d 340eval { ok $^S == 1 };
b0e6f864 341eval " BEGIN { ok ! defined \$^S } ";
342ok $^S == 0 && defined $^S;
7c36658b 343
344ok ${^TAINT} == 0;
345eval { ${^TAINT} = 1 };
346ok ${^TAINT} == 0;
9aa702ec 347
348# 5.6.1 had a bug: @+ and @- were not properly interpolated
349# into double-quoted strings
350# 20020414 mjd-perl-patch+@plover.com
b64ebf53 351"I like pie" =~ /(I) (like) (pie)/;
352ok "@-" eq "0 0 2 7";
353ok "@+" eq "10 1 6 10";
9aa702ec 354
f28098ff 355# Tests for the magic get of $\
356{
357 my $ok = 0;
358 # [perl #19330]
359 {
360 local $\ = undef;
361 $\++; $\++;
362 $ok = $\ eq 2;
363 }
364 ok $ok;
365 $ok = 0;
366 {
367 local $\ = "a\0b";
368 $ok = "a$\b" eq "aa\0bb";
369 }
370 ok $ok;
371}