Integrate macperl changes from Chris Nandor:
[p5sagit/p5-mst-13.2.git] / t / op / magic.t
1 #!./perl
2
3 BEGIN {
4     $| = 1;
5     chdir 't' if -d 't';
6     @INC = '../lib';
7     $SIG{__WARN__} = sub { die "Dying on warning: ", @_ };
8 }
9
10 use warnings;
11
12 my $test = 1;
13 sub ok {
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;
23     }
24
25     $test++;
26     return $ok;
27 }
28
29 sub skip {
30     my($reason) = @_;
31
32     printf "ok $test # skipped%s\n", defined $reason ? ": $reason" : '';
33
34     $test++;
35     return 1;
36 }
37
38 print "1..41\n";
39
40 $Is_MSWin32 = $^O eq 'MSWin32';
41 $Is_NetWare = $^O eq 'NetWare';
42 $Is_VMS     = $^O eq 'VMS';
43 $Is_Dos     = $^O eq 'dos';
44 $Is_os2     = $^O eq 'os2';
45 $Is_Cygwin  = $^O eq 'cygwin';
46 $Is_MacOS   = $^O eq 'MacOS';
47 $Is_MPE     = $^O eq 'mpeix';           
48
49 $PERL = ($Is_NetWare ? 'perl'   :
50          $Is_MacOS   ? $^X      :
51          $Is_MSWin32 ? '.\perl' :
52          './perl');
53
54 eval '$ENV{"FOO"} = "hi there";';       # check that ENV is inited inside eval
55 # cmd.exe will echo 'variable=value' but 4nt will echo just the value
56 # -- Nikola Knezevic
57 if ($Is_MSWin32)  { ok `set FOO` =~ /^(FOO=)?hi there$/; }
58 elsif ($Is_MacOS) { ok "1 # skipped", 1; }
59 else              { ok `echo \$FOO` eq "hi there\n"; }
60
61 unlink 'ajslkdfpqjsjfk';
62 $! = 0;
63 open(FOO,'ajslkdfpqjsjfk');
64 ok $!, $!;
65 close FOO; # just mention it, squelch used-only-once
66
67 if ($Is_MSWin32 || $Is_NetWare || $Is_Dos || $Is_MPE || $Is_MacOS) {
68     skip() for 1..2;
69 }
70 else {
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.
73     system './perl', '-e', <<'END';
74
75     $| = 1;             # command buffering
76
77     $SIG{"INT"} = "ok3";     kill "INT",$$; sleep 1;
78     $SIG{"INT"} = "IGNORE";  kill "INT",$$; sleep 1; print "ok 4\n";
79     $SIG{"INT"} = "DEFAULT"; kill "INT",$$; sleep 1; print "not ok 4\n";
80
81     sub ok3 {
82         if (($x = pop(@_)) eq "INT") {
83             print "ok 3\n";
84         }
85         else {
86             print "not ok 3 ($x @_)\n";
87         }
88     }
89
90 END
91     $test += 2;
92 }
93
94 # can we slice ENV?
95 @val1 = @ENV{keys(%ENV)};
96 @val2 = values(%ENV);
97 ok join(':',@val1) eq join(':',@val2);
98 ok @val1 > 1;
99
100 # regex vars
101 'foobarbaz' =~ /b(a)r/;
102 ok $` eq 'foo', $`;
103 ok $& eq 'bar', $&;
104 ok $' eq 'baz', $';
105 ok $+ eq 'a', $+;
106
107 # $"
108 @a = qw(foo bar baz);
109 ok "@a" eq "foo bar baz", "@a";
110 {
111     local $" = ',';
112     ok "@a" eq "foo,bar,baz", "@a";
113 }
114
115 # $;
116 %h = ();
117 $h{'foo', 'bar'} = 1;
118 ok((keys %h)[0] eq "foo\034bar", (keys %h)[0]);
119 {
120     local $; = 'x';
121     %h = ();
122     $h{'foo', 'bar'} = 1;
123     ok((keys %h)[0] eq 'fooxbar', (keys %h)[0]);
124 }
125
126 # $?, $@, $$
127 system qq[$PERL -e "exit(0)"];
128 ok $? == 0, $?;
129 system qq[$PERL -e "exit(1)"];
130 ok $? != 0, $?;
131
132 eval { die "foo\n" };
133 ok $@ eq "foo\n", $@;
134
135 ok $$ > 0, $$;
136
137 # $^X and $0
138 {
139     if ($^O eq 'qnx') {
140         chomp($wd = `/usr/bin/fullpath -t`);
141     }
142     elsif($Is_Cygwin) {
143        # Cygwin turns the symlink into the real file
144        chomp($wd = `pwd`);
145        $wd =~ s#/t$##;
146     }
147     elsif($Is_os2) {
148        $wd = Cwd::sys_cwd();
149     }
150     elsif($Is_MacOS) {
151        $wd = ':';
152     }
153     else {
154         $wd = '.';
155     }
156     my $perl = $Is_MacOS ? $^X : "$wd/perl";
157     my $headmaybe = '';
158     my $tailmaybe = '';
159     $script = "$wd/show-shebang";
160     if ($Is_MSWin32) {
161         chomp($wd = `cd`);
162         $wd =~ s|\\|/|g;
163         $perl = "$wd/perl.exe";
164         $script = "$wd/show-shebang.bat";
165         $headmaybe = <<EOH ;
166 \@rem ='
167 \@echo off
168 $perl -x \%0
169 goto endofperl
170 \@rem ';
171 EOH
172         $tailmaybe = <<EOT ;
173
174 __END__
175 :endofperl
176 EOT
177     }
178     elsif ($Is_os2) {
179       $script = "./show-shebang";
180     }
181     elsif ($Is_MacOS) {
182       $script = ":show-shebang";
183     }
184     elsif ($Is_MacOS) {
185       $script = ":show-shebang";
186     }
187     if ($^O eq 'os390' or $^O eq 'posix-bc' or $^O eq 'vmesa') {  # no shebang
188         $headmaybe = <<EOH ;
189     eval 'exec ./perl -S \$0 \${1+"\$\@"}'
190         if 0;
191 EOH
192     }
193     $s1 = "\$^X is $perl, \$0 is $script\n";
194     ok open(SCRIPT, ">$script"), $!;
195     ok print(SCRIPT $headmaybe . <<EOB . <<'EOF' . $tailmaybe), $!;
196 #!$wd/perl
197 EOB
198 print "\$^X is $^X, \$0 is $0\n";
199 EOF
200     ok close(SCRIPT), $!;
201     ok chmod(0755, $script), $!;
202     $_ = $Is_MacOS ? `$perl $script` : `$script`;
203     s/\.exe//i if $Is_Dos or $Is_Cygwin or $Is_os2;
204     s{\bminiperl\b}{perl}; # so that test doesn't fail with miniperl
205     s{is perl}{is $perl}; # for systems where $^X is only a basename
206     s{\\}{/}g;
207     ok((($Is_MSWin32 || $Is_os2) ? uc($_) eq uc($s1) : $_ eq $s1), " :$_:!=:$s1:");
208     $_ = `$perl $script`;
209     s/\.exe//i if $Is_Dos or $Is_os2;
210     s{\\}{/}g;
211     ok((($Is_MSWin32 || $Is_os2) ? uc($_) eq uc($s1) : $_ eq $s1), " :$_:!=:$s1: after `$perl $script`");
212     ok unlink($script), $!;
213 }
214
215 # $], $^O, $^T
216 ok $] >= 5.00319, $];
217 ok $^O;
218 ok $^T > 850000000, $^T;
219
220 if ($Is_VMS || $Is_Dos || $Is_MacOS) {
221     skip() for 1..2;
222 }
223 else {
224         $PATH = $ENV{PATH};
225         $PDL = $ENV{PERL_DESTRUCT_LEVEL} || 0;
226         $ENV{foo} = "bar";
227         %ENV = ();
228         $ENV{PATH} = $PATH;
229         $ENV{PERL_DESTRUCT_LEVEL} = $PDL || 0;
230         ok ($Is_MSWin32 ? (`set foo 2>NUL` eq "")
231                                 : (`echo \$foo` eq "\n") );
232
233         $ENV{__NoNeSuCh} = "foo";
234         $0 = "bar";
235 # cmd.exe will echo 'variable=value' but 4nt will echo just the value
236 # -- Nikola Knezevic
237         ok ($Is_MSWin32 ? (`set __NoNeSuCh` =~ /^(__NoNeSuCh=)?foo$/)
238                             : (`echo \$__NoNeSuCh` eq "foo\n") );
239 }
240
241 {
242     local $SIG{'__WARN__'} = sub { print "# @_\nnot " };
243     $! = undef;
244     ok 1;
245 }
246
247 # test case-insignificance of %ENV (these tests must be enabled only
248 # when perl is compiled with -DENV_IS_CASELESS)
249 if ($Is_MSWin32 || $Is_NetWare) {
250     %ENV = ();
251     $ENV{'Foo'} = 'bar';
252     $ENV{'fOo'} = 'baz';
253     ok (scalar(keys(%ENV)) == 1);
254     ok exists($ENV{'FOo'});
255     ok (delete($ENV{'foO'}) eq 'baz');
256     ok (scalar(keys(%ENV)) == 0);
257 }
258 else {
259     skip('no caseless %ENV support') for 1..4;
260 }
261
262 # Make sure Errno hasn't been prematurely autoloaded
263
264 ok !defined %Errno::;
265
266 # Test auto-loading of Errno when %! is used
267
268 ok scalar eval q{
269    my $errs = %!;
270    defined %Errno::;
271 }, $@;
272
273
274 # Make sure that Errno loading doesn't clobber $!
275
276 undef %Errno::;
277 delete $INC{"Errno.pm"};
278
279 open(FOO, "nonesuch"); # Generate ENOENT
280 my %errs = %{"!"}; # Cause Errno.pm to be loaded at run-time
281 ok ${"!"}{ENOENT};
282
283 ok $^S == 0;
284 eval { ok $^S == 1 };
285 ok $^S == 0;