Re: [perl #36130] chr(-1) should probably return undef
[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 use Config;
12
13 my $test = 1;
14 sub ok {
15     my($ok, $info, $todo) = @_;
16
17     # You have to do it this way or VMS will get confused.
18     printf "%s $test%s\n", $ok ? "ok" : "not ok",
19                            $todo ? " # TODO $todo" : '';
20
21     unless( $ok ) {
22         printf "# Failed test at line %d\n", (caller)[2];
23         print  "# $info\n" if defined $info;
24     }
25
26     $test++;
27     return $ok;
28 }
29
30 sub skip {
31     my($reason) = @_;
32
33     printf "ok $test # skipped%s\n", defined $reason ? ": $reason" : '';
34
35     $test++;
36     return 1;
37 }
38
39 print "1..54\n";
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_MacOS    = $^O eq 'MacOS';
48 $Is_MPE      = $^O eq 'mpeix';          
49 $Is_miniperl = $ENV{PERL_CORE_MINITEST};
50 $Is_BeOS     = $^O eq 'beos';
51
52 $PERL = ($Is_NetWare            ? 'perl'   :
53          ($Is_MacOS || $Is_VMS) ? $^X      :
54          $Is_MSWin32            ? '.\perl' :
55          './perl');
56
57 eval '$ENV{"FOO"} = "hi there";';       # check that ENV is inited inside eval
58 # cmd.exe will echo 'variable=value' but 4nt will echo just the value
59 # -- Nikola Knezevic
60 if ($Is_MSWin32)  { ok `set FOO` =~ /^(?:FOO=)?hi there$/; }
61 elsif ($Is_MacOS) { ok "1 # skipped", 1; }
62 elsif ($Is_VMS)   { ok `write sys\$output f\$trnlnm("FOO")` eq "hi there\n"; }
63 else              { ok `echo \$FOO` eq "hi there\n"; }
64
65 unlink 'ajslkdfpqjsjfk';
66 $! = 0;
67 open(FOO,'ajslkdfpqjsjfk');
68 ok $!, $!;
69 close FOO; # just mention it, squelch used-only-once
70
71 if ($Is_MSWin32 || $Is_NetWare || $Is_Dos || $Is_MPE || $Is_MacOS) {
72     skip('SIGINT not safe on this platform') for 1..4;
73 }
74 else {
75   # the next tests are done in a subprocess because sh spits out a
76   # newline onto stderr when a child process kills itself with SIGINT.
77   # We use a pipe rather than system() because the VMS command buffer
78   # would overflow with a command that long.
79
80     open( CMDPIPE, "| $PERL");
81
82     print CMDPIPE <<'END';
83
84     $| = 1;             # command buffering
85
86     $SIG{"INT"} = "ok3";     kill "INT",$$; sleep 1;
87     $SIG{"INT"} = "IGNORE";  kill "INT",$$; sleep 1; print "ok 4\n";
88     $SIG{"INT"} = "DEFAULT"; kill "INT",$$; sleep 1; print "not ok 4\n";
89
90     sub ok3 {
91         if (($x = pop(@_)) eq "INT") {
92             print "ok 3\n";
93         }
94         else {
95             print "not ok 3 ($x @_)\n";
96         }
97     }
98
99 END
100
101     close CMDPIPE;
102
103     open( CMDPIPE, "| $PERL");
104     print CMDPIPE <<'END';
105
106     { package X;
107         sub DESTROY {
108             kill "INT",$$;
109         }
110     }
111     sub x {
112         my $x=bless [], 'X';
113         return sub { $x };
114     }
115     $| = 1;             # command buffering
116     $SIG{"INT"} = "ok5";
117     {
118         local $SIG{"INT"}=x();
119         print ""; # Needed to expose failure in 5.8.0 (why?)
120     }
121     sleep 1;
122     delete $SIG{"INT"};
123     kill "INT",$$; sleep 1;
124     sub ok5 {
125         print "ok 5\n";
126     }
127 END
128     close CMDPIPE;
129     $? >>= 8 if $^O eq 'VMS'; # POSIX status hiding in 2nd byte
130     my $todo = ($^O eq 'os2' ? ' # TODO: EMX v0.9d_fix4 bug: wrong nibble? ' : '');
131     print $? & 0xFF ? "ok 6$todo\n" : "not ok 6$todo\n";
132
133     $test += 4;
134 }
135
136 # can we slice ENV?
137 @val1 = @ENV{keys(%ENV)};
138 @val2 = values(%ENV);
139 ok join(':',@val1) eq join(':',@val2);
140 ok @val1 > 1;
141
142 # regex vars
143 'foobarbaz' =~ /b(a)r/;
144 ok $` eq 'foo', $`;
145 ok $& eq 'bar', $&;
146 ok $' eq 'baz', $';
147 ok $+ eq 'a', $+;
148
149 # $"
150 @a = qw(foo bar baz);
151 ok "@a" eq "foo bar baz", "@a";
152 {
153     local $" = ',';
154     ok "@a" eq "foo,bar,baz", "@a";
155 }
156
157 # $;
158 %h = ();
159 $h{'foo', 'bar'} = 1;
160 ok((keys %h)[0] eq "foo\034bar", (keys %h)[0]);
161 {
162     local $; = 'x';
163     %h = ();
164     $h{'foo', 'bar'} = 1;
165     ok((keys %h)[0] eq 'fooxbar', (keys %h)[0]);
166 }
167
168 # $?, $@, $$
169 if ($Is_MacOS) {
170     skip('$? + system are broken on MacPerl') for 1..2;
171 }
172 else {
173     system qq[$PERL "-I../lib" -e "use vmsish qw(hushed); exit(0)"];
174     ok $? == 0, $?;
175     system qq[$PERL "-I../lib" -e "use vmsish qw(hushed); exit(1)"];
176     ok $? != 0, $?;
177 }
178
179 eval { die "foo\n" };
180 ok $@ eq "foo\n", $@;
181
182 ok $$ > 0, $$;
183 eval { $$++ };
184 ok $@ =~ /^Modification of a read-only value attempted/;
185
186 # $^X and $0
187 {
188     if ($^O eq 'qnx') {
189         chomp($wd = `/usr/bin/fullpath -t`);
190     }
191     elsif($Is_Cygwin || $Config{'d_procselfexe'}) {
192        # Cygwin turns the symlink into the real file
193        chomp($wd = `pwd`);
194        $wd =~ s#/t$##;
195     }
196     elsif($Is_os2) {
197        $wd = Cwd::sys_cwd();
198     }
199     elsif($Is_MacOS) {
200        $wd = ':';
201     }
202     else {
203         $wd = '.';
204     }
205     my $perl = ($Is_MacOS || $Is_VMS) ? $^X : "$wd/perl";
206     my $headmaybe = '';
207     my $tailmaybe = '';
208     $script = "$wd/show-shebang";
209     if ($Is_MSWin32) {
210         chomp($wd = `cd`);
211         $wd =~ s|\\|/|g;
212         $perl = "$wd/perl.exe";
213         $script = "$wd/show-shebang.bat";
214         $headmaybe = <<EOH ;
215 \@rem ='
216 \@echo off
217 $perl -x \%0
218 goto endofperl
219 \@rem ';
220 EOH
221         $tailmaybe = <<EOT ;
222
223 __END__
224 :endofperl
225 EOT
226     }
227     elsif ($Is_os2) {
228       $script = "./show-shebang";
229     }
230     elsif ($Is_MacOS) {
231       $script = ":show-shebang";
232     }
233     elsif ($Is_VMS) {
234       $script = "[]show-shebang";
235     }
236     if ($^O eq 'os390' or $^O eq 'posix-bc' or $^O eq 'vmesa') {  # no shebang
237         $headmaybe = <<EOH ;
238     eval 'exec ./perl -S \$0 \${1+"\$\@"}'
239         if 0;
240 EOH
241     }
242     $s1 = "\$^X is $perl, \$0 is $script\n";
243     ok open(SCRIPT, ">$script"), $!;
244     ok print(SCRIPT $headmaybe . <<EOB . <<'EOF' . $tailmaybe), $!;
245 #!$wd/perl
246 EOB
247 print "\$^X is $^X, \$0 is $0\n";
248 EOF
249     ok close(SCRIPT), $!;
250     ok chmod(0755, $script), $!;
251     $_ = ($Is_MacOS || $Is_VMS) ? `$perl $script` : `$script`;
252     s/\.exe//i if $Is_Dos or $Is_Cygwin or $Is_os2;
253     s{./$script}{$script} if $Is_BeOS; # revert BeOS execvp() side-effect
254     s{\bminiperl\b}{perl}; # so that test doesn't fail with miniperl
255     s{is perl}{is $perl}; # for systems where $^X is only a basename
256     s{\\}{/}g;
257     ok((($Is_MSWin32 || $Is_os2) ? uc($_) eq uc($s1) : $_ eq $s1), " :$_:!=:$s1:");
258     $_ = `$perl $script`;
259     s/\.exe//i if $Is_Dos or $Is_os2;
260     s{./$perl}{$perl} if $Is_BeOS; # revert BeOS execvp() side-effect
261     s{\\}{/}g;
262     ok((($Is_MSWin32 || $Is_os2) ? uc($_) eq uc($s1) : $_ eq $s1), " :$_:!=:$s1: after `$perl $script`");
263     ok unlink($script), $!;
264 }
265
266 # $], $^O, $^T
267 ok $] >= 5.00319, $];
268 ok $^O;
269 ok $^T > 850000000, $^T;
270
271 if ($Is_VMS || $Is_Dos || $Is_MacOS) {
272     skip("%ENV manipulations fail or aren't safe on $^O") for 1..4;
273 }
274 else {
275         if ($ENV{PERL_VALGRIND}) {
276             skip("clearing \%ENV is not safe when running under valgrind");
277         } else {
278             $PATH = $ENV{PATH};
279             $PDL = $ENV{PERL_DESTRUCT_LEVEL} || 0;
280             $ENV{foo} = "bar";
281             %ENV = ();
282             $ENV{PATH} = $PATH;
283             $ENV{PERL_DESTRUCT_LEVEL} = $PDL || 0;
284             ok ($Is_MSWin32 ? (`set foo 2>NUL` eq "")
285                             : (`echo \$foo` eq "\n") );
286         }
287
288         $ENV{__NoNeSuCh} = "foo";
289         $0 = "bar";
290 # cmd.exe will echo 'variable=value' but 4nt will echo just the value
291 # -- Nikola Knezevic
292        ok ($Is_MSWin32 ? (`set __NoNeSuCh` =~ /^(?:__NoNeSuCh=)?foo$/)
293                             : (`echo \$__NoNeSuCh` eq "foo\n") );
294         if ($^O =~ /^(linux|freebsd)$/ &&
295             open CMDLINE, "/proc/$$/cmdline") {
296             chomp(my $line = scalar <CMDLINE>);
297             my $me = (split /\0/, $line)[0];
298             ok($me eq $0, 'altering $0 is effective (testing with /proc/)');
299             close CMDLINE;
300             # perlbug #22811
301             my $mydollarzero = sub {
302               my($arg) = shift;
303               $0 = $arg if defined $arg;
304               # In FreeBSD the ps -o command= will cause
305               # an empty header line, grab only the last line.
306               my $ps = (`ps -o command= -p $$`)[-1];
307               return if $?;
308               chomp $ps;
309               printf "# 0[%s]ps[%s]\n", $0, $ps;
310               $ps;
311             };
312             my $ps = $mydollarzero->("x");
313             ok(!$ps  # we allow that something goes wrong with the ps command
314                # In Linux 2.4 we would get an exact match ($ps eq 'x') but
315                # in Linux 2.2 there seems to be something funny going on:
316                # it seems as if the original length of the argv[] would
317                # be stored in the proc struct and then used by ps(1),
318                # no matter what characters we use to pad the argv[].
319                # (And if we use \0:s, they are shown as spaces.)  Sigh.
320                || $ps =~ /^x\s*$/
321                # FreeBSD cannot get rid of both the leading "perl :"
322                # and the trailing " (perl)": some FreeBSD versions
323                # can get rid of the first one.
324                || ($^O eq 'freebsd' && $ps =~ m/^(?:perl: )?x(?: \(perl\))?$/),
325                        'altering $0 is effective (testing with `ps`)');
326         } else {
327             skip("\$0 check only on Linux and FreeBSD") for 0, 1;
328         }
329 }
330
331 {
332     my $ok = 1;
333     my $warn = '';
334     local $SIG{'__WARN__'} = sub { $ok = 0; $warn = join '', @_; };
335     $! = undef;
336     ok($ok, $warn, $Is_VMS ? "'\$!=undef' does throw a warning" : '');
337 }
338
339 # test case-insignificance of %ENV (these tests must be enabled only
340 # when perl is compiled with -DENV_IS_CASELESS)
341 if ($Is_MSWin32 || $Is_NetWare) {
342     %ENV = ();
343     $ENV{'Foo'} = 'bar';
344     $ENV{'fOo'} = 'baz';
345     ok (scalar(keys(%ENV)) == 1);
346     ok exists($ENV{'FOo'});
347     ok (delete($ENV{'foO'}) eq 'baz');
348     ok (scalar(keys(%ENV)) == 0);
349 }
350 else {
351     skip('no caseless %ENV support') for 1..4;
352 }
353
354 if ($Is_miniperl) {
355     skip ("miniperl can't rely on loading %Errno") for 1..2;
356 } else {
357    no warnings 'void';
358
359 # Make sure Errno hasn't been prematurely autoloaded
360
361    ok !keys %Errno::;
362
363 # Test auto-loading of Errno when %! is used
364
365    ok scalar eval q{
366       %!;
367       defined %Errno::;
368    }, $@;
369 }
370
371 if ($Is_miniperl) {
372     skip ("miniperl can't rely on loading %Errno");
373 } else {
374     # Make sure that Errno loading doesn't clobber $!
375
376     undef %Errno::;
377     delete $INC{"Errno.pm"};
378
379     open(FOO, "nonesuch"); # Generate ENOENT
380     my %errs = %{"!"}; # Cause Errno.pm to be loaded at run-time
381     ok ${"!"}{ENOENT};
382 }
383
384 ok $^S == 0 && defined $^S;
385 eval { ok $^S == 1 };
386 eval " BEGIN { ok ! defined \$^S } ";
387 ok $^S == 0 && defined $^S;
388
389 ok ${^TAINT} == 0;
390 eval { ${^TAINT} = 1 };
391 ok ${^TAINT} == 0;
392
393 # 5.6.1 had a bug: @+ and @- were not properly interpolated
394 # into double-quoted strings
395 # 20020414 mjd-perl-patch+@plover.com
396 "I like pie" =~ /(I) (like) (pie)/;
397 ok "@-" eq  "0 0 2 7";
398 ok "@+" eq "10 1 6 10";
399
400 # Tests for the magic get of $\
401 {
402     my $ok = 0;
403     # [perl #19330]
404     {
405         local $\ = undef;
406         $\++; $\++;
407         $ok = $\ eq 2;
408     }
409     ok $ok;
410     $ok = 0;
411     {
412         local $\ = "a\0b";
413         $ok = "a$\b" eq "aa\0bb";
414     }
415     ok $ok;
416 }
417
418 # Test for bug [perl #27839]
419 {
420     my $x;
421     sub f {
422         "abc" =~ /(.)./;
423         $x = "@+";
424         return @+;
425     };
426     my @y = f();
427     ok( $x eq "@y", "return a magic array ($x) vs (@y)" );
428 }