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