Re: Why *not* use UNIVERSAL qw( isa can ) ; ??
[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;
11
0bee926d 12my $test = 1;
90ce63d5 13sub ok {
0bee926d 14 my($ok, $info, $todo) = @_;
15
16 # You have to do it this way or VMS will get confused.
17 printf "%s $test%s\n", $ok ? "ok" : "not ok",
18 $todo ? " # TODO $todo" : '';
19
20 unless( $ok ) {
21 printf "# Failed test at line %d\n", (caller)[2];
22 print "# $info" if defined $info;
90ce63d5 23 }
0bee926d 24
25 $test++;
26 return $ok;
27}
28
29sub skip {
30 my($reason) = @_;
31
32 printf "ok $test # skipped%s\n", defined $reason ? ": $reason" : '';
33
34 $test++;
35 return 1;
90ce63d5 36}
37
7c36658b 38print "1..43\n";
0bee926d 39
3e3baf6d 40$Is_MSWin32 = $^O eq 'MSWin32';
2986a63f 41$Is_NetWare = $^O eq 'NetWare';
3e3baf6d 42$Is_VMS = $^O eq 'VMS';
be708cc0 43$Is_Dos = $^O eq 'dos';
44$Is_os2 = $^O eq 'os2';
45$Is_Cygwin = $^O eq 'cygwin';
46$Is_MacOS = $^O eq 'MacOS';
dc22e1c4 47$Is_MPE = $^O eq 'mpeix';
be708cc0 48
c363d00c 49$PERL = ($Is_NetWare ? 'perl' :
50 ($Is_MacOS || $Is_VMS) ? $^X :
51 $Is_MSWin32 ? '.\perl' :
be708cc0 52 './perl');
68dc0745 53
39e571d4 54eval '$ENV{"FOO"} = "hi there";'; # check that ENV is inited inside eval
26f6e342 55# cmd.exe will echo 'variable=value' but 4nt will echo just the value
56# -- Nikola Knezevic
be708cc0 57if ($Is_MSWin32) { ok `set FOO` =~ /^(FOO=)?hi there$/; }
58elsif ($Is_MacOS) { ok "1 # skipped", 1; }
c363d00c 59elsif ($Is_VMS) { ok `write sys\$output f\$trnlnm("FOO")` eq "hi there\n"; }
be708cc0 60else { ok `echo \$FOO` eq "hi there\n"; }
8d063cd8 61
bf38876a 62unlink 'ajslkdfpqjsjfk';
8d063cd8 63$! = 0;
90ce63d5 64open(FOO,'ajslkdfpqjsjfk');
0bee926d 65ok $!, $!;
90ce63d5 66close FOO; # just mention it, squelch used-only-once
8d063cd8 67
be708cc0 68if ($Is_MSWin32 || $Is_NetWare || $Is_Dos || $Is_MPE || $Is_MacOS) {
124f2019 69 skip('SIGINT not safe on this platform') for 1..2;
68dc0745 70}
71else {
c363d00c 72 # the next tests are done in a subprocess because sh spits out a
73 # newline onto stderr when a child process kills itself with SIGINT.
74 # We use a pipe rather than system() because the VMS command buffer
75 # would overflow with a command that long.
76
77 open( CMDPIPE, "| $PERL");
78
79 print CMDPIPE <<'END';
378cc40b 80
79072805 81 $| = 1; # command buffering
378cc40b 82
b715f106 83 $SIG{"INT"} = "ok3"; kill "INT",$$; sleep 1;
84 $SIG{"INT"} = "IGNORE"; kill "INT",$$; sleep 1; print "ok 4\n";
0bee926d 85 $SIG{"INT"} = "DEFAULT"; kill "INT",$$; sleep 1; print "not ok 4\n";
79072805 86
87 sub ok3 {
88 if (($x = pop(@_)) eq "INT") {
89 print "ok 3\n";
90 }
91 else {
652ed9f8 92 print "not ok 3 ($x @_)\n";
79072805 93 }
94 }
95
96END
c363d00c 97
98 close CMDPIPE;
99
fae3d9ea 100 $test += 2;
68dc0745 101}
a687059c 102
68dc0745 103# can we slice ENV?
104@val1 = @ENV{keys(%ENV)};
a687059c 105@val2 = values(%ENV);
0bee926d 106ok join(':',@val1) eq join(':',@val2);
107ok @val1 > 1;
90ce63d5 108
109# regex vars
110'foobarbaz' =~ /b(a)r/;
0bee926d 111ok $` eq 'foo', $`;
112ok $& eq 'bar', $&;
113ok $' eq 'baz', $';
114ok $+ eq 'a', $+;
90ce63d5 115
116# $"
117@a = qw(foo bar baz);
0bee926d 118ok "@a" eq "foo bar baz", "@a";
90ce63d5 119{
120 local $" = ',';
0bee926d 121 ok "@a" eq "foo,bar,baz", "@a";
90ce63d5 122}
a687059c 123
90ce63d5 124# $;
125%h = ();
126$h{'foo', 'bar'} = 1;
0bee926d 127ok((keys %h)[0] eq "foo\034bar", (keys %h)[0]);
90ce63d5 128{
129 local $; = 'x';
130 %h = ();
131 $h{'foo', 'bar'} = 1;
0bee926d 132 ok((keys %h)[0] eq 'fooxbar', (keys %h)[0]);
90ce63d5 133}
ed6116ce 134
90ce63d5 135# $?, $@, $$
5aabfad6 136system qq[$PERL -e "exit(0)"];
0bee926d 137ok $? == 0, $?;
5aabfad6 138system qq[$PERL -e "exit(1)"];
0bee926d 139ok $? != 0, $?;
90ce63d5 140
141eval { die "foo\n" };
0bee926d 142ok $@ eq "foo\n", $@;
90ce63d5 143
0bee926d 144ok $$ > 0, $$;
90ce63d5 145
146# $^X and $0
ed37317b 147{
3e3baf6d 148 if ($^O eq 'qnx') {
7fbf1995 149 chomp($wd = `/usr/bin/fullpath -t`);
68dc0745 150 }
1cab015a 151 elsif($Is_Cygwin) {
152 # Cygwin turns the symlink into the real file
153 chomp($wd = `pwd`);
154 $wd =~ s#/t$##;
155 }
ed344e4f 156 elsif($Is_os2) {
157 $wd = Cwd::sys_cwd();
158 }
be708cc0 159 elsif($Is_MacOS) {
160 $wd = ':';
161 }
68dc0745 162 else {
163 $wd = '.';
164 }
c363d00c 165 my $perl = ($Is_MacOS || $Is_VMS) ? $^X : "$wd/perl";
ed37317b 166 my $headmaybe = '';
167 my $tailmaybe = '';
68dc0745 168 $script = "$wd/show-shebang";
ed37317b 169 if ($Is_MSWin32) {
170 chomp($wd = `cd`);
8ac9c18d 171 $wd =~ s|\\|/|g;
172 $perl = "$wd/perl.exe";
173 $script = "$wd/show-shebang.bat";
ed37317b 174 $headmaybe = <<EOH ;
175\@rem ='
176\@echo off
177$perl -x \%0
178goto endofperl
179\@rem ';
180EOH
181 $tailmaybe = <<EOT ;
182
183__END__
184:endofperl
185EOT
186 }
ed344e4f 187 elsif ($Is_os2) {
188 $script = "./show-shebang";
189 }
be708cc0 190 elsif ($Is_MacOS) {
191 $script = ":show-shebang";
192 }
c363d00c 193 elsif ($Is_VMS) {
194 $script = "[]show-shebang";
be708cc0 195 }
a1a0e61e 196 if ($^O eq 'os390' or $^O eq 'posix-bc' or $^O eq 'vmesa') { # no shebang
9d116dd7 197 $headmaybe = <<EOH ;
198 eval 'exec ./perl -S \$0 \${1+"\$\@"}'
199 if 0;
200EOH
201 }
2eecd615 202 $s1 = "\$^X is $perl, \$0 is $script\n";
0bee926d 203 ok open(SCRIPT, ">$script"), $!;
204 ok print(SCRIPT $headmaybe . <<EOB . <<'EOF' . $tailmaybe), $!;
774d564b 205#!$wd/perl
206EOB
90ce63d5 207print "\$^X is $^X, \$0 is $0\n";
208EOF
0bee926d 209 ok close(SCRIPT), $!;
210 ok chmod(0755, $script), $!;
c363d00c 211 $_ = ($Is_MacOS || $Is_VMS) ? `$perl $script` : `$script`;
ed344e4f 212 s/\.exe//i if $Is_Dos or $Is_Cygwin or $Is_os2;
68dc0745 213 s{\bminiperl\b}{perl}; # so that test doesn't fail with miniperl
ed37317b 214 s{is perl}{is $perl}; # for systems where $^X is only a basename
a6c40364 215 s{\\}{/}g;
0bee926d 216 ok((($Is_MSWin32 || $Is_os2) ? uc($_) eq uc($s1) : $_ eq $s1), " :$_:!=:$s1:");
ed37317b 217 $_ = `$perl $script`;
ed344e4f 218 s/\.exe//i if $Is_Dos or $Is_os2;
a6c40364 219 s{\\}{/}g;
0bee926d 220 ok((($Is_MSWin32 || $Is_os2) ? uc($_) eq uc($s1) : $_ eq $s1), " :$_:!=:$s1: after `$perl $script`");
221 ok unlink($script), $!;
68dc0745 222}
ed6116ce 223
90ce63d5 224# $], $^O, $^T
0bee926d 225ok $] >= 5.00319, $];
226ok $^O;
227ok $^T > 850000000, $^T;
66b1d557 228
be708cc0 229if ($Is_VMS || $Is_Dos || $Is_MacOS) {
c363d00c 230 skip("%ENV manipulations fail or aren't safe on $^O") for 1..2;
66b1d557 231}
232else {
3e3baf6d 233 $PATH = $ENV{PATH};
7ddf2a0a 234 $PDL = $ENV{PERL_DESTRUCT_LEVEL} || 0;
3e3baf6d 235 $ENV{foo} = "bar";
236 %ENV = ();
237 $ENV{PATH} = $PATH;
7ddf2a0a 238 $ENV{PERL_DESTRUCT_LEVEL} = $PDL || 0;
0bee926d 239 ok ($Is_MSWin32 ? (`set foo 2>NUL` eq "")
3e3baf6d 240 : (`echo \$foo` eq "\n") );
241
ec00bdd8 242 $ENV{__NoNeSuCh} = "foo";
3e3baf6d 243 $0 = "bar";
26f6e342 244# cmd.exe will echo 'variable=value' but 4nt will echo just the value
245# -- Nikola Knezevic
0bee926d 246 ok ($Is_MSWin32 ? (`set __NoNeSuCh` =~ /^(__NoNeSuCh=)?foo$/)
ec00bdd8 247 : (`echo \$__NoNeSuCh` eq "foo\n") );
66b1d557 248}
3e3baf6d 249
c363d00c 250if ($Is_VMS) {
251 ok(1,0,"'\$!=undef' does throw a warning");
252}
253else {
2eecd615 254 local $SIG{'__WARN__'} = sub { print "# @_\nnot " };
78987ded 255 $! = undef;
0bee926d 256 ok 1;
78987ded 257}
258
902173a3 259# test case-insignificance of %ENV (these tests must be enabled only
260# when perl is compiled with -DENV_IS_CASELESS)
2986a63f 261if ($Is_MSWin32 || $Is_NetWare) {
902173a3 262 %ENV = ();
263 $ENV{'Foo'} = 'bar';
264 $ENV{'fOo'} = 'baz';
0bee926d 265 ok (scalar(keys(%ENV)) == 1);
266 ok exists($ENV{'FOo'});
267 ok (delete($ENV{'foO'}) eq 'baz');
268 ok (scalar(keys(%ENV)) == 0);
902173a3 269}
270else {
0bee926d 271 skip('no caseless %ENV support') for 1..4;
902173a3 272}
d2c93421 273
274# Make sure Errno hasn't been prematurely autoloaded
275
0bee926d 276ok !defined %Errno::;
d2c93421 277
278# Test auto-loading of Errno when %! is used
279
0bee926d 280ok scalar eval q{
d2c93421 281 my $errs = %!;
282 defined %Errno::;
283}, $@;
284
285
286# Make sure that Errno loading doesn't clobber $!
287
288undef %Errno::;
289delete $INC{"Errno.pm"};
290
291open(FOO, "nonesuch"); # Generate ENOENT
292my %errs = %{"!"}; # Cause Errno.pm to be loaded at run-time
0bee926d 293ok ${"!"}{ENOENT};
a4268c0a 294
0bee926d 295ok $^S == 0;
296eval { ok $^S == 1 };
297ok $^S == 0;
7c36658b 298
299ok ${^TAINT} == 0;
300eval { ${^TAINT} = 1 };
301ok ${^TAINT} == 0;