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