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