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