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