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