This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
NeXT doesn't have FD_CLOEXEC (suggested by Hans Mulder)
[perl5.git] / t / op / magic.t
CommitLineData
8d063cd8
LW
1#!./perl
2
90ce63d5
RS
3BEGIN {
4 $^W = 1;
5 $| = 1;
6 chdir 't' if -d 't';
93430cb4 7 unshift @INC, '../lib';
774d564b 8 $SIG{__WARN__} = sub { die "Dying on warning: ", @_ };
90ce63d5 9}
8d063cd8 10
90ce63d5
RS
11sub ok {
12 my ($n, $result, $info) = @_;
13 if ($result) {
14 print "ok $n\n";
15 }
16 else {
17 print "not ok $n\n";
18 print "# $info\n" if $info;
19 }
20}
21
3e3baf6d
TB
22$Is_MSWin32 = $^O eq 'MSWin32';
23$Is_VMS = $^O eq 'VMS';
39e571d4 24$Is_Dos = $^O eq 'dos';
68dc0745 25$PERL = ($Is_MSWin32 ? '.\perl' : './perl');
26
78987ded 27print "1..35\n";
378cc40b 28
39e571d4
LM
29eval '$ENV{"FOO"} = "hi there";'; # check that ENV is inited inside eval
30if ($Is_MSWin32) { ok 1, `cmd /x /c set FOO` eq "FOO=hi there\n"; }
31else { ok 1, `echo \$FOO` eq "hi there\n"; }
8d063cd8 32
bf38876a 33unlink 'ajslkdfpqjsjfk';
8d063cd8 34$! = 0;
90ce63d5
RS
35open(FOO,'ajslkdfpqjsjfk');
36ok 2, $!, $!;
37close FOO; # just mention it, squelch used-only-once
8d063cd8 38
39e571d4 39if ($Is_MSWin32 || $Is_Dos) {
902173a3
GS
40 ok "3 # skipped",1;
41 ok "4 # skipped",1;
68dc0745 42}
43else {
44 # the next tests are embedded inside system simply because sh spits out
45 # a newline onto stderr when a child process kills itself with SIGINT.
46 system './perl', '-e', <<'END';
378cc40b 47
79072805 48 $| = 1; # command buffering
378cc40b 49
b715f106
MB
50 $SIG{"INT"} = "ok3"; kill "INT",$$; sleep 1;
51 $SIG{"INT"} = "IGNORE"; kill "INT",$$; sleep 1; print "ok 4\n";
52 $SIG{"INT"} = "DEFAULT"; kill "INT",$$; sleep 1; print "not ok\n";
79072805
LW
53
54 sub ok3 {
55 if (($x = pop(@_)) eq "INT") {
56 print "ok 3\n";
57 }
58 else {
652ed9f8 59 print "not ok 3 ($x @_)\n";
79072805
LW
60 }
61 }
62
63END
68dc0745 64}
a687059c 65
68dc0745 66# can we slice ENV?
67@val1 = @ENV{keys(%ENV)};
a687059c 68@val2 = values(%ENV);
90ce63d5
RS
69ok 5, join(':',@val1) eq join(':',@val2);
70ok 6, @val1 > 1;
71
72# regex vars
73'foobarbaz' =~ /b(a)r/;
74ok 7, $` eq 'foo', $`;
75ok 8, $& eq 'bar', $&;
76ok 9, $' eq 'baz', $';
77ok 10, $+ eq 'a', $+;
78
79# $"
80@a = qw(foo bar baz);
81ok 11, "@a" eq "foo bar baz", "@a";
82{
83 local $" = ',';
84 ok 12, "@a" eq "foo,bar,baz", "@a";
85}
a687059c 86
90ce63d5
RS
87# $;
88%h = ();
89$h{'foo', 'bar'} = 1;
90ok 13, (keys %h)[0] eq "foo\034bar", (keys %h)[0];
91{
92 local $; = 'x';
93 %h = ();
94 $h{'foo', 'bar'} = 1;
95 ok 14, (keys %h)[0] eq 'fooxbar', (keys %h)[0];
96}
ed6116ce 97
90ce63d5 98# $?, $@, $$
5aabfad6 99system qq[$PERL -e "exit(0)"];
90ce63d5 100ok 15, $? == 0, $?;
5aabfad6 101system qq[$PERL -e "exit(1)"];
90ce63d5
RS
102ok 16, $? != 0, $?;
103
104eval { die "foo\n" };
105ok 17, $@ eq "foo\n", $@;
106
107ok 18, $$ > 0, $$;
108
109# $^X and $0
ed37317b 110{
3e3baf6d 111 if ($^O eq 'qnx') {
7fbf1995 112 chomp($wd = `/usr/bin/fullpath -t`);
68dc0745 113 }
114 else {
115 $wd = '.';
116 }
ed37317b
TB
117 my $perl = "$wd/perl";
118 my $headmaybe = '';
119 my $tailmaybe = '';
68dc0745 120 $script = "$wd/show-shebang";
ed37317b
TB
121 if ($Is_MSWin32) {
122 chomp($wd = `cd`);
8ac9c18d
GS
123 $wd =~ s|\\|/|g;
124 $perl = "$wd/perl.exe";
125 $script = "$wd/show-shebang.bat";
ed37317b
TB
126 $headmaybe = <<EOH ;
127\@rem ='
128\@echo off
129$perl -x \%0
130goto endofperl
131\@rem ';
132EOH
133 $tailmaybe = <<EOT ;
134
135__END__
136:endofperl
137EOT
138 }
a1a0e61e 139 if ($^O eq 'os390' or $^O eq 'posix-bc' or $^O eq 'vmesa') { # no shebang
9d116dd7
JH
140 $headmaybe = <<EOH ;
141 eval 'exec ./perl -S \$0 \${1+"\$\@"}'
142 if 0;
143EOH
144 }
2eecd615 145 $s1 = "\$^X is $perl, \$0 is $script\n";
68dc0745 146 ok 19, open(SCRIPT, ">$script"), $!;
ed37317b 147 ok 20, print(SCRIPT $headmaybe . <<EOB . <<'EOF' . $tailmaybe), $!;
774d564b 148#!$wd/perl
149EOB
90ce63d5
RS
150print "\$^X is $^X, \$0 is $0\n";
151EOF
68dc0745 152 ok 21, close(SCRIPT), $!;
153 ok 22, chmod(0755, $script), $!;
154 $_ = `$script`;
2eecd615 155 s/\.exe//i if $Is_Dos;
68dc0745 156 s{\bminiperl\b}{perl}; # so that test doesn't fail with miniperl
ed37317b 157 s{is perl}{is $perl}; # for systems where $^X is only a basename
a6c40364 158 s{\\}{/}g;
2eecd615 159 ok 23, ($Is_MSWin32 ? uc($_) eq uc($s1) : $_ eq $s1), ":$_:!=:$s1:";
ed37317b 160 $_ = `$perl $script`;
2eecd615 161 s/\.exe//i if $Is_Dos;
a6c40364 162 s{\\}{/}g;
ed37317b 163 ok 24, ($Is_MSWin32 ? uc($_) eq uc($s1) : $_ eq $s1), ":$_:!=:$s1: after `$perl $script`";
68dc0745 164 ok 25, unlink($script), $!;
165}
ed6116ce 166
90ce63d5
RS
167# $], $^O, $^T
168ok 26, $] >= 5.00319, $];
169ok 27, $^O;
170ok 28, $^T > 850000000, $^T;
66b1d557 171
39e571d4 172if ($Is_VMS || $Is_Dos) {
902173a3
GS
173 ok "29 # skipped", 1;
174 ok "30 # skipped", 1;
66b1d557
HM
175}
176else {
3e3baf6d
TB
177 $PATH = $ENV{PATH};
178 $ENV{foo} = "bar";
179 %ENV = ();
180 $ENV{PATH} = $PATH;
181 ok 29, ($Is_MSWin32 ? (`cmd /x /c set foo 2>NUL` eq "")
182 : (`echo \$foo` eq "\n") );
183
184 $ENV{NoNeSuCh} = "foo";
185 $0 = "bar";
186 ok 30, ($Is_MSWin32 ? (`cmd /x /c set NoNeSuCh` eq "NoNeSuCh=foo\n")
187 : (`echo \$NoNeSuCh` eq "foo\n") );
66b1d557 188}
3e3baf6d 189
78987ded 190{
2eecd615 191 local $SIG{'__WARN__'} = sub { print "# @_\nnot " };
78987ded
HS
192 $! = undef;
193 print "ok 31\n";
194}
195
902173a3
GS
196# test case-insignificance of %ENV (these tests must be enabled only
197# when perl is compiled with -DENV_IS_CASELESS)
198if ($Is_MSWin32) {
199 %ENV = ();
200 $ENV{'Foo'} = 'bar';
201 $ENV{'fOo'} = 'baz';
78987ded
HS
202 ok 32, (scalar(keys(%ENV)) == 1);
203 ok 33, exists($ENV{'FOo'});
204 ok 34, (delete($ENV{'foO'}) eq 'baz');
205 ok 35, (scalar(keys(%ENV)) == 0);
902173a3
GS
206}
207else {
902173a3
GS
208 ok "32 # skipped",1;
209 ok "33 # skipped",1;
210 ok "34 # skipped",1;
78987ded 211 ok "35 # skipped",1;
902173a3 212}