Change #11828 wasn't complete, this updates to intest path
[p5sagit/p5-mst-13.2.git] / t / run / kill_perl.t
1 #!./perl
2
3 # This is for tests that will normally cause segfaults, and other nasty
4 # errors that might kill the interpreter and for some reason you can't
5 # use an eval().
6 #
7 # New tests are added to the bottom.  For example.
8 #
9 #       ######## perlbug ID 20020831.001
10 #       ($a, b) = (1,2)
11 #       EXPECT
12 #       Can't modify constant item in list assignment - at line 1
13 #
14 # to test that the code "($a, b) = (1,2)" causes the appropriate syntax
15 # error, rather than just segfaulting as reported in perlbug ID
16 # 20020831.001
17 #
18 #
19 # NOTE: Please don't add tests to this file unless they *need* to be
20 # run in separate executable and can't simply use eval.
21
22 BEGIN {
23     chdir 't' if -d 't';
24     @INC = '../lib';
25 }
26
27 use strict;
28
29 $|=1;
30
31 my @prgs = ();
32 while(<DATA>) { 
33     if(m/^#{8,}\s*(.*)/) { 
34         push @prgs, ['', $1];
35     }
36     else { 
37         $prgs[-1][0] .= $_;
38     }
39 }
40 print "1..", scalar @prgs, "\n";
41
42 my $tmpfile = "misctmp000";
43 1 while -f ++$tmpfile;
44 END { while($tmpfile && unlink $tmpfile){} }
45
46 my $test = 1;
47 foreach my $prog (@prgs) {
48     my($raw_prog, $name) = @$prog;
49
50     my $switch;
51     if ($raw_prog =~ s/^\s*(-\w.*)//){
52         $switch = $1;
53     }
54
55     my($prog,$expected) = split(/\nEXPECT\n/, $raw_prog);
56
57     open TEST, ">$tmpfile" or die "Cannot open $tmpfile: $!";
58
59     # VMS adjustments
60     if( $^O eq 'VMS' ) {
61         $prog =~ s#/dev/null#NL:#;
62
63         # VMS file locking 
64         $prog =~ s{if \(-e _ and -f _ and -r _\)}
65                   {if (-e _ and -f _)}
66     }
67
68     print TEST $prog, "\n";
69     close TEST or die "Cannot close $tmpfile: $!";
70
71     my $results;
72     if ($^O eq 'MSWin32') {
73         $results = `.\\perl -I../lib $switch $tmpfile 2>&1`;
74     }
75     elsif ($^O eq 'NetWare') {
76         $results = `perl -I../lib $switch $tmpfile 2>&1`;
77     }
78     else {
79       $results = `./perl -I../lib $switch $tmpfile 2>&1`;
80     }
81     my $status = $?;
82
83     # Clean up the results into something a bit more predictable.
84     $results =~ s/\n+$//;
85     $results =~ s/at\s+misctmp\d+\s+line/at - line/g;
86     $results =~ s/of\s+misctmp\d+\s+aborted/of - aborted/g;
87
88     # bison says 'parse error' instead of 'syntax error',
89     # various yaccs may or may not capitalize 'syntax'.
90     $results =~ s/^(syntax|parse) error/syntax error/mig;
91
92     $results =~ s/\n\n/\n/ if $^O eq 'VMS'; # pipes double these sometimes
93
94     $expected =~ s/\n+$//;
95     my $ok = $results eq $expected;
96
97     unless( $ok ) {
98         print STDERR "# PROG: $switch\n$prog\n";
99         print STDERR "# EXPECTED:\n$expected\n";
100         print STDERR "# GOT:\n$results\n";
101     }
102     printf "%sok %d%s\n", ($ok ? '' : "not "), $test, 
103                           length $name ? " - $name" : $name;
104     $test++;
105 }
106
107 __END__
108 ########
109 $a = ":="; split /($a)/o, "a:=b:=c"; print "@_"
110 EXPECT
111 a := b := c
112 ########
113 $cusp = ~0 ^ (~0 >> 1);
114 use integer;
115 $, = " ";
116 print +($cusp - 1) % 8, $cusp % 8, -$cusp % 8, 8 | (($cusp + 1) % 8 + 7), "!\n";
117 EXPECT
118 7 0 0 8 !
119 ########
120 $foo=undef; $foo->go;
121 EXPECT
122 Can't call method "go" on an undefined value at - line 1.
123 ########
124 BEGIN
125         {
126             "foo";
127         }
128 ########
129 $array[128]=1
130 ########
131 $x=0x0eabcd; print $x->ref;
132 EXPECT
133 Can't call method "ref" without a package or object reference at - line 1.
134 ########
135 chop ($str .= <DATA>);
136 ########
137 close ($banana);
138 ########
139 $x=2;$y=3;$x<$y ? $x : $y += 23;print $x;
140 EXPECT
141 25
142 ########
143 eval {sub bar {print "In bar";}}
144 ########
145 system './perl -ne "print if eof" /dev/null'
146 ########
147 chop($file = <DATA>);
148 ########
149 package N;
150 sub new {my ($obj,$n)=@_; bless \$n}  
151 $aa=new N 1;
152 $aa=12345;
153 print $aa;
154 EXPECT
155 12345
156 ########
157 %@x=0;
158 EXPECT
159 Can't modify hash dereference in repeat (x) at - line 1, near "0;"
160 Execution of - aborted due to compilation errors.
161 ########
162 $_="foo";
163 printf(STDOUT "%s\n", $_);
164 EXPECT
165 foo
166 ########
167 push(@a, 1, 2, 3,)
168 ########
169 quotemeta ""
170 ########
171 for ("ABCDE") {
172  &sub;
173 s/./&sub($&)/eg;
174 print;}
175 sub sub {local($_) = @_;
176 $_ x 4;}
177 EXPECT
178 Modification of a read-only value attempted at - line 3.
179 ########
180 package FOO;sub new {bless {FOO => BAR}};
181 package main;
182 use strict vars;   
183 my $self = new FOO;
184 print $$self{FOO};
185 EXPECT
186 BAR
187 ########
188 $_="foo";
189 s/.{1}//s;
190 print;
191 EXPECT
192 oo
193 ########
194 print scalar ("foo","bar")
195 EXPECT
196 bar
197 ########
198 sub by_number { $a <=> $b; };# inline function for sort below
199 $as_ary{0}="a0";
200 @ordered_array=sort by_number keys(%as_ary);
201 ########
202 sub NewShell
203 {
204   local($Host) = @_;
205   my($m2) = $#Shells++;
206   $Shells[$m2]{HOST} = $Host;
207   return $m2;
208 }
209  
210 sub ShowShell
211 {
212   local($i) = @_;
213 }
214  
215 &ShowShell(&NewShell(beach,Work,"+0+0"));
216 &ShowShell(&NewShell(beach,Work,"+0+0"));
217 &ShowShell(&NewShell(beach,Work,"+0+0"));
218 ########
219    {
220        package FAKEARRAY;
221    
222        sub TIEARRAY
223        { print "TIEARRAY @_\n"; 
224          die "bomb out\n" unless $count ++ ;
225          bless ['foo'] 
226        }
227        sub FETCH { print "fetch @_\n"; $_[0]->[$_[1]] }
228        sub STORE { print "store @_\n"; $_[0]->[$_[1]] = $_[2] }
229        sub DESTROY { print "DESTROY \n"; undef @{$_[0]}; }
230    }
231    
232 eval 'tie @h, FAKEARRAY, fred' ;
233 tie @h, FAKEARRAY, fred ;
234 EXPECT
235 TIEARRAY FAKEARRAY fred
236 TIEARRAY FAKEARRAY fred
237 DESTROY 
238 ########
239 BEGIN { die "phooey\n" }
240 EXPECT
241 phooey
242 BEGIN failed--compilation aborted at - line 1.
243 ########
244 BEGIN { 1/$zero }
245 EXPECT
246 Illegal division by zero at - line 1.
247 BEGIN failed--compilation aborted at - line 1.
248 ########
249 BEGIN { undef = 0 }
250 EXPECT
251 Modification of a read-only value attempted at - line 1.
252 BEGIN failed--compilation aborted at - line 1.
253 ########
254 {
255     package foo;
256     sub PRINT {
257         shift;
258         print join(' ', reverse @_)."\n";
259     }
260     sub PRINTF {
261         shift;
262           my $fmt = shift;
263         print sprintf($fmt, @_)."\n";
264     }
265     sub TIEHANDLE {
266         bless {}, shift;
267     }
268     sub READLINE {
269         "Out of inspiration";
270     }
271     sub DESTROY {
272         print "and destroyed as well\n";
273   }
274   sub READ {
275       shift;
276       print STDOUT "foo->can(READ)(@_)\n";
277       return 100; 
278   }
279   sub GETC {
280       shift;
281       print STDOUT "Don't GETC, Get Perl\n";
282       return "a"; 
283   }    
284 }
285 {
286     local(*FOO);
287     tie(*FOO,'foo');
288     print FOO "sentence.", "reversed", "a", "is", "This";
289     print "-- ", <FOO>, " --\n";
290     my($buf,$len,$offset);
291     $buf = "string";
292     $len = 10; $offset = 1;
293     read(FOO, $buf, $len, $offset) == 100 or die "foo->READ failed";
294     getc(FOO) eq "a" or die "foo->GETC failed";
295     printf "%s is number %d\n", "Perl", 1;
296 }
297 EXPECT
298 This is a reversed sentence.
299 -- Out of inspiration --
300 foo->can(READ)(string 10 1)
301 Don't GETC, Get Perl
302 Perl is number 1
303 and destroyed as well
304 ########
305 my @a; $a[2] = 1; for (@a) { $_ = 2 } print "@a\n"
306 EXPECT
307 2 2 2
308 ########
309 # used to attach defelem magic to all immortal values,
310 # which made restore of local $_ fail.
311 foo(2>1);
312 sub foo { bar() for @_;  }
313 sub bar { local $_; }
314 print "ok\n";
315 EXPECT
316 ok
317 ########
318 @a = ($a, $b, $c, $d) = (5, 6);
319 print "ok\n"
320   if ($a[0] == 5 and $a[1] == 6 and !defined $a[2] and !defined $a[3]);
321 EXPECT
322 ok
323 ########
324 print "ok\n" if (1E2<<1 == 200 and 3E4<<3 == 240000);
325 EXPECT
326 ok
327 ########
328 print "ok\n" if ("\0" lt "\xFF");
329 EXPECT
330 ok
331 ########
332 open(H,'run/kill_perl.t'); # must be in the 't' directory
333 stat(H);
334 print "ok\n" if (-e _ and -f _ and -r _);
335 EXPECT
336 ok
337 ########
338 sub thing { 0 || return qw(now is the time) }
339 print thing(), "\n";
340 EXPECT
341 nowisthetime
342 ########
343 $ren = 'joy';
344 $stimpy = 'happy';
345 { local $main::{ren} = *stimpy; print $ren, ' ' }
346 print $ren, "\n";
347 EXPECT
348 happy joy
349 ########
350 $stimpy = 'happy';
351 { local $main::{ren} = *stimpy; print ${'ren'}, ' ' }
352 print +(defined(${'ren'}) ? 'oops' : 'joy'), "\n";
353 EXPECT
354 happy joy
355 ########
356 package p;
357 sub func { print 'really ' unless wantarray; 'p' }
358 sub groovy { 'groovy' }
359 package main;
360 print p::func()->groovy(), "\n"
361 EXPECT
362 really groovy
363 ########
364 @list = ([ 'one', 1 ], [ 'two', 2 ]);
365 sub func { $num = shift; (grep $_->[1] == $num, @list)[0] }
366 print scalar(map &func($_), 1 .. 3), " ",
367       scalar(map scalar &func($_), 1 .. 3), "\n";
368 EXPECT
369 2 3
370 ########
371 ($k, $s)  = qw(x 0);
372 @{$h{$k}} = qw(1 2 4);
373 for (@{$h{$k}}) { $s += $_; delete $h{$k} if ($_ == 2) }
374 print "bogus\n" unless $s == 7;
375 ########
376 my $a = 'outer';
377 eval q[ my $a = 'inner'; eval q[ print "$a " ] ];
378 eval { my $x = 'peace'; eval q[ print "$x\n" ] }
379 EXPECT
380 inner peace
381 ########
382 -w
383 $| = 1;
384 sub foo {
385     print "In foo1\n";
386     eval 'sub foo { print "In foo2\n" }';
387     print "Exiting foo1\n";
388 }
389 foo;
390 foo;
391 EXPECT
392 In foo1
393 Subroutine foo redefined at (eval 1) line 1.
394 Exiting foo1
395 In foo2
396 ########
397 $s = 0;
398 map {#this newline here tickles the bug
399 $s += $_} (1,2,4);
400 print "eat flaming death\n" unless ($s == 7);
401 ########
402 sub foo { local $_ = shift; split; @_ }
403 @x = foo(' x  y  z ');
404 print "you die joe!\n" unless "@x" eq 'x y z';
405 ########
406 /(?{"{"})/      # Check it outside of eval too
407 EXPECT
408 Sequence (?{...}) not terminated or not {}-balanced at - line 1, within pattern
409 Sequence (?{...}) not terminated or not {}-balanced in regex; marked by <-- HERE in m/(?{ <-- HERE "{"})/ at - line 1.
410 ########
411 /(?{"{"}})/     # Check it outside of eval too
412 EXPECT
413 Unmatched right curly bracket at (re_eval 1) line 1, at end of line
414 syntax error at (re_eval 1) line 1, near ""{"}"
415 Compilation failed in regexp at - line 1.
416 ########
417 BEGIN { @ARGV = qw(a b c d e) }
418 BEGIN { print "argv <@ARGV>\nbegin <",shift,">\n" }
419 END { print "end <",shift,">\nargv <@ARGV>\n" }
420 INIT { print "init <",shift,">\n" }
421 CHECK { print "check <",shift,">\n" }
422 EXPECT
423 argv <a b c d e>
424 begin <a>
425 check <b>
426 init <c>
427 end <d>
428 argv <e>
429 ########
430 -l
431 # fdopen from a system descriptor to a system descriptor used to close
432 # the former.
433 open STDERR, '>&=STDOUT' or die $!;
434 select STDOUT; $| = 1; print fileno STDOUT or die $!;
435 select STDERR; $| = 1; print fileno STDERR or die $!;
436 EXPECT
437 1
438 2
439 ########
440 -w
441 sub testme { my $a = "test"; { local $a = "new test"; print $a }}
442 EXPECT
443 Can't localize lexical variable $a at - line 2.
444 ########
445 package X;
446 sub ascalar { my $r; bless \$r }
447 sub DESTROY { print "destroyed\n" };
448 package main;
449 *s = ascalar X;
450 EXPECT
451 destroyed
452 ########
453 package X;
454 sub anarray { bless [] }
455 sub DESTROY { print "destroyed\n" };
456 package main;
457 *a = anarray X;
458 EXPECT
459 destroyed
460 ########
461 package X;
462 sub ahash { bless {} }
463 sub DESTROY { print "destroyed\n" };
464 package main;
465 *h = ahash X;
466 EXPECT
467 destroyed
468 ########
469 package X;
470 sub aclosure { my $x; bless sub { ++$x } }
471 sub DESTROY { print "destroyed\n" };
472 package main;
473 *c = aclosure X;
474 EXPECT
475 destroyed
476 ########
477 package X;
478 sub any { bless {} }
479 my $f = "FH000"; # just to thwart any future optimisations
480 sub afh { select select ++$f; my $r = *{$f}{IO}; delete $X::{$f}; bless $r }
481 sub DESTROY { print "destroyed\n" }
482 package main;
483 $x = any X; # to bump sv_objcount. IO objs aren't counted??
484 *f = afh X;
485 EXPECT
486 destroyed
487 destroyed
488 ########
489 BEGIN {
490   $| = 1;
491   $SIG{__WARN__} = sub {
492     eval { print $_[0] };
493     die "bar\n";
494   };
495   warn "foo\n";
496 }
497 EXPECT
498 foo
499 bar
500 BEGIN failed--compilation aborted at - line 8.
501 ########
502 package X;
503 @ISA='Y';
504 sub new {
505     my $class = shift;
506     my $self = { };
507     bless $self, $class;
508     my $init = shift;
509     $self->foo($init);
510     print "new", $init;
511     return $self;
512 }
513 sub DESTROY {
514     my $self = shift;
515     print "DESTROY", $self->foo;
516 }
517 package Y;
518 sub attribute {
519     my $self = shift;
520     my $var = shift;
521     if (@_ == 0) {
522         return $self->{$var};
523     } elsif (@_ == 1) {
524         $self->{$var} = shift;
525     }
526 }
527 sub AUTOLOAD {
528     $AUTOLOAD =~ /::([^:]+)$/;
529     my $method = $1;
530     splice @_, 1, 0, $method;
531     goto &attribute;
532 }
533 package main;
534 my $x = X->new(1);
535 for (2..3) {
536     my $y = X->new($_);
537     print $y->foo;
538 }
539 print $x->foo;
540 EXPECT
541 new1new22DESTROY2new33DESTROY31DESTROY1
542 ########
543 re();
544 sub re {
545     my $re = join '', eval 'qr/(??{ $obj->method })/';
546     $re;
547 }
548 EXPECT
549 ########
550 use strict;
551 my $foo = "ZZZ\n";
552 END { print $foo }
553 EXPECT
554 ZZZ
555 ########
556 eval '
557 use strict;
558 my $foo = "ZZZ\n";
559 END { print $foo }
560 ';
561 EXPECT
562 ZZZ
563 ########
564 -w
565 if (@ARGV) { print "" }
566 else {
567   if ($x == 0) { print "" } else { print $x }
568 }
569 EXPECT
570 Use of uninitialized value in numeric eq (==) at - line 4.
571 ########
572 $x = sub {};
573 foo();
574 sub foo { eval { return }; }
575 print "ok\n";
576 EXPECT
577 ok
578 ########
579 # moved to op/lc.t
580 EXPECT
581 ########
582 sub f { my $a = 1; my $b = 2; my $c = 3; my $d = 4; next }
583 my $x = "foo";
584 { f } continue { print $x, "\n" }
585 EXPECT
586 foo
587 ########
588 sub C () { 1 }
589 sub M { $_[0] = 2; }
590 eval "C";
591 M(C);
592 EXPECT
593 Modification of a read-only value attempted at - line 2.
594 ########
595 print qw(ab a\b a\\b);
596 EXPECT
597 aba\ba\b
598 ########
599 # lexicals declared after the myeval() definition should not be visible
600 # within it
601 sub myeval { eval $_[0] }
602 my $foo = "ok 2\n";
603 myeval('sub foo { local $foo = "ok 1\n"; print $foo; }');
604 die $@ if $@;
605 foo();
606 print $foo;
607 EXPECT
608 ok 1
609 ok 2
610 ########
611 # lexicals outside an eval"" should be visible inside subroutine definitions
612 # within it
613 eval <<'EOT'; die $@ if $@;
614 {
615     my $X = "ok\n";
616     eval 'sub Y { print $X }'; die $@ if $@;
617     Y();
618 }
619 EOT
620 EXPECT
621 ok
622 ########
623 # test that closures generated by eval"" hold on to the CV of the eval""
624 # for their entire lifetime
625 $code = eval q[
626   sub { eval '$x = "ok 1\n"'; }
627 ];
628 &{$code}();
629 print $x;
630 EXPECT
631 ok 1
632 ########
633 # This test is here instead of pragma/locale.t because
634 # the bug depends on in the internal state of the locale
635 # settings and pragma/locale messes up that state pretty badly.
636 # We need a "fresh run".
637 BEGIN {
638     eval { require POSIX };
639     if ($@) {
640         exit(0); # running minitest?
641     }
642 }
643 use Config;
644 my $have_setlocale = $Config{d_setlocale} eq 'define';
645 $have_setlocale = 0 if $@;
646 # Visual C's CRT goes silly on strings of the form "en_US.ISO8859-1"
647 # and mingw32 uses said silly CRT
648 $have_setlocale = 0 if (($^O eq 'MSWin32' || $^O eq 'NetWare') && $Config{cc} =~ /^(cl|gcc)/i);
649 exit(0) unless $have_setlocale;
650 my @locales;
651 if (-x "/usr/bin/locale" && open(LOCALES, "/usr/bin/locale -a|")) {
652     while(<LOCALES>) {
653         chomp;
654         push(@locales, $_);
655     }
656     close(LOCALES);
657 }
658 exit(0) unless @locales;
659 for (@locales) {
660     use POSIX qw(locale_h);
661     use locale;
662     setlocale(LC_NUMERIC, $_) or next;
663     my $s = sprintf "%g %g", 3.1, 3.1;
664     next if $s eq '3.1 3.1' || $s =~ /^(3.+1) \1$/;
665     print "$_ $s\n";
666 }
667 EXPECT
668 ########
669 die qr(x)
670 EXPECT
671 (?-xism:x) at - line 1.
672 ########
673 # 20001210.003 mjd@plover.com
674 format REMITOUT_TOP =
675 FOO
676 .
677
678 format REMITOUT =
679 BAR
680 .
681
682 # This loop causes a segv in 5.6.0
683 for $lineno (1..61) {
684    write REMITOUT;
685 }
686
687 print "It's OK!";
688 EXPECT
689 It's OK!
690 ########
691 # Inaba Hiroto
692 reset;
693 if (0) {
694   if ("" =~ //) {
695   }
696 }
697 ########
698 # Nicholas Clark
699 $ENV{TERM} = 0;
700 reset;
701 // if 0;
702 ########
703 # Vadim Konovalov
704 use strict;
705 sub new_pmop($) {
706     my $pm = shift;
707     return eval "sub {shift=~/$pm/}";
708 }
709 new_pmop "abcdef"; reset;
710 new_pmop "abcdef"; reset;
711 new_pmop "abcdef"; reset;
712 new_pmop "abcdef"; reset;
713 ########
714 # David Dyck
715 # coredump in 5.7.1
716 close STDERR; die;
717 EXPECT
718 ########
719 -w
720 "x" =~ /(\G?x)?/;       # core dump in 20000716.007
721 EXPECT
722 Quantifier unexpected on zero-length expression in regex; marked by <-- HERE in m/(\G?x)? <-- HERE / at - line 2.
723 ########
724 # Bug 20010515.004
725 my @h = 1 .. 10;
726 bad(@h);
727 sub bad {
728    undef @h;
729    print "O";
730    print for @_;
731    print "K";
732 }
733 EXPECT
734 OK
735 ########
736 # Bug 20010506.041
737 "abcd\x{1234}" =~ /(a)(b[c])(d+)?/i and print "ok\n";
738 EXPECT
739 ok
740 ########
741 # Bug 20010422.005
742 {s//${}/; //}
743 EXPECT
744 syntax error at - line 2, near "${}"
745 Execution of - aborted due to compilation errors.
746 ########
747 # Bug 20010528.007
748 "\x{"
749 EXPECT
750 Missing right brace on \x{} at - line 2, within string
751 Execution of - aborted due to compilation errors.
752 ########
753 my $foo = Bar->new();
754 my @dst;
755 END {
756     ($_ = "@dst") =~ s/\(0x.+?\)/(0x...)/;
757     print $_, "\n";
758 }
759 package Bar;
760 sub new {
761     my Bar $self = bless [], Bar;
762     eval '$self';
763     return $self;
764 }
765 sub DESTROY { 
766     push @dst, "$_[0]";
767 }
768 EXPECT
769 Bar=ARRAY(0x...)
770 ########
771 # 20010407.008 sprintf removes utf8-ness
772 $a = sprintf "\x{1234}";
773 printf "%x %d\n", unpack("U*", $a), length($a);
774 $a = sprintf "%s", "\x{5678}";
775 printf "%x %d\n", unpack("U*", $a), length($a);
776 $a = sprintf "\x{1234}%s", "\x{5678}";
777 printf "%x %x %d\n", unpack("U*", $a), length($a);
778 EXPECT
779 1234 1
780 5678 1
781 1234 5678 2
782 ########
783 # keep this last - doesn't seem to work otherwise?
784 eval "a.b.c.d.e.f;sub"
785 EXPECT
786
787 ######## perlbug ID 20010831.001
788 ($a, b) = (1, 2);
789 EXPECT
790 Can't modify constant item in list assignment at - line 1, near ");"
791 Execution of - aborted due to compilation errors.