*time_r fixes
[p5sagit/p5-mst-13.2.git] / t / op / misc.t
CommitLineData
a0d0e21e 1#!./perl
2
fb73857a 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
a0d0e21e 6chdir 't' if -d 't';
20822f61 7@INC = '../lib';
a0d0e21e 8$ENV{PERL5LIB} = "../lib";
9
10$|=1;
11
12undef $/;
13@prgs = split "\n########\n", <DATA>;
14print "1..", scalar @prgs, "\n";
15
16$tmpfile = "misctmp000";
171 while -f ++$tmpfile;
ed6b3797 18END { while($tmpfile && unlink $tmpfile){} }
a0d0e21e 19
2986a63f 20$CAT = (($^O eq 'MSWin32') ? '.\perl -e "print <>"' : (($^O eq 'NetWare') ? 'perl -e "print <>"' : 'cat'));
68dc0745 21
a0d0e21e 22for (@prgs){
23 my $switch;
fb73857a 24 if (s/^\s*(-\w.*)//){
25 $switch = $1;
a0d0e21e 26 }
27 my($prog,$expected) = split(/\nEXPECT\n/, $_);
648cac19 28 open TEST, ">$tmpfile" or die "Cannot open $tmpfile: $!";
ed6b3797 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
648cac19 32 print TEST $prog, "\n";
33 close TEST or die "Cannot close $tmpfile: $!";
34
68dc0745 35 if ($^O eq 'MSWin32') {
648cac19 36 $results = `.\\perl -I../lib $switch $tmpfile 2>&1`;
68dc0745 37 }
2986a63f 38 elsif ($^O eq 'NetWare') {
39 $results = `perl -I../lib $switch $tmpfile 2>&1`;
40 }
68dc0745 41 else {
648cac19 42 $results = `./perl $switch $tmpfile 2>&1`;
68dc0745 43 }
a0d0e21e 44 $status = $?;
a0d0e21e 45 $results =~ s/\n+$//;
648cac19 46 $results =~ s/at\s+misctmp\d+\s+line/at - line/g;
47 $results =~ s/of\s+misctmp\d+\s+aborted/of - aborted/g;
f0ec1f9a 48# bison says 'parse error' instead of 'syntax error',
d91e2bdb 49# various yaccs may or may not capitalize 'syntax'.
2a8ee232 50 $results =~ s/^(syntax|parse) error/syntax error/mig;
c2f0b9b9 51 $results =~ s/\n\n/\n/ if $^O eq 'VMS'; # pipes double these sometimes
a0d0e21e 52 $expected =~ s/\n+$//;
8feb4e9f 53 if ( $results ne $expected ) {
a0d0e21e 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__
2ace3117 63()=()
64########
44a8e56a 65$a = ":="; split /($a)/o, "a:=b:=c"; print "@_"
66EXPECT
67a := b := c
68########
36477c24 69$cusp = ~0 ^ (~0 >> 1);
85e0ebd8 70use integer;
36477c24 71$, = " ";
85e0ebd8 72print +($cusp - 1) % 8, $cusp % 8, -$cusp % 8, 8 | (($cusp + 1) % 8 + 7), "!\n";
36477c24 73EXPECT
85e0ebd8 747 0 0 8 !
36477c24 75########
a0d0e21e 76$foo=undef; $foo->go;
77EXPECT
72b5445b 78Can't call method "go" on an undefined value at - line 1.
a0d0e21e 79########
80BEGIN
81 {
82 "foo";
83 }
84########
a0d0e21e 85$array[128]=1
86########
87$x=0x0eabcd; print $x->ref;
88EXPECT
89Can't call method "ref" without a package or object reference at - line 1.
90########
648cac19 91chop ($str .= <DATA>);
a0d0e21e 92########
93close ($banana);
94########
95$x=2;$y=3;$x<$y ? $x : $y += 23;print $x;
96EXPECT
9725
98########
99eval {sub bar {print "In bar";}}
100########
68dc0745 101system './perl -ne "print if eof" /dev/null'
a0d0e21e 102########
648cac19 103chop($file = <DATA>);
a0d0e21e 104########
105package N;
106sub new {my ($obj,$n)=@_; bless \$n}
107$aa=new N 1;
108$aa=12345;
109print $aa;
110EXPECT
11112345
112########
113%@x=0;
114EXPECT
f1612b5c 115Can't modify hash dereference in repeat (x) at - line 1, near "0;"
3fe9a6f1 116Execution of - aborted due to compilation errors.
a0d0e21e 117########
118$_="foo";
119printf(STDOUT "%s\n", $_);
120EXPECT
121foo
122########
123push(@a, 1, 2, 3,)
124########
125quotemeta ""
126########
127for ("ABCDE") {
128 &sub;
129s/./&sub($&)/eg;
130print;}
131sub sub {local($_) = @_;
132$_ x 4;}
133EXPECT
134Modification of a read-only value attempted at - line 3.
135########
136package FOO;sub new {bless {FOO => BAR}};
137package main;
138use strict vars;
139my $self = new FOO;
140print $$self{FOO};
141EXPECT
142BAR
143########
144$_="foo";
145s/.{1}//s;
146print;
147EXPECT
148oo
149########
150print scalar ("foo","bar")
151EXPECT
152bar
153########
154sub by_number { $a <=> $b; };# inline function for sort below
155$as_ary{0}="a0";
156@ordered_array=sort by_number keys(%as_ary);
157########
158sub NewShell
159{
160 local($Host) = @_;
161 my($m2) = $#Shells++;
162 $Shells[$m2]{HOST} = $Host;
163 return $m2;
164}
165
166sub 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
188eval 'tie @h, FAKEARRAY, fred' ;
189tie @h, FAKEARRAY, fred ;
190EXPECT
191TIEARRAY FAKEARRAY fred
192TIEARRAY FAKEARRAY fred
193DESTROY
194########
195BEGIN { die "phooey\n" }
196EXPECT
197phooey
198BEGIN failed--compilation aborted at - line 1.
199########
200BEGIN { 1/$zero }
201EXPECT
202Illegal division by zero at - line 1.
203BEGIN failed--compilation aborted at - line 1.
204########
205BEGIN { undef = 0 }
206EXPECT
207Modification of a read-only value attempted at - line 1.
208BEGIN failed--compilation aborted at - line 1.
a7adf1f0 209########
210{
211 package foo;
212 sub PRINT {
213 shift;
214 print join(' ', reverse @_)."\n";
215 }
46fc3d4c 216 sub PRINTF {
217 shift;
218 my $fmt = shift;
219 print sprintf($fmt, @_)."\n";
220 }
a7adf1f0 221 sub TIEHANDLE {
222 bless {}, shift;
223 }
58f51617 224 sub READLINE {
225 "Out of inspiration";
226 }
a7adf1f0 227 sub DESTROY {
228 print "and destroyed as well\n";
2ae324a7 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 }
a7adf1f0 240}
241{
242 local(*FOO);
243 tie(*FOO,'foo');
244 print FOO "sentence.", "reversed", "a", "is", "This";
58f51617 245 print "-- ", <FOO>, " --\n";
2ae324a7 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";
46fc3d4c 251 printf "%s is number %d\n", "Perl", 1;
a7adf1f0 252}
253EXPECT
254This is a reversed sentence.
58f51617 255-- Out of inspiration --
2ae324a7 256foo->can(READ)(string 10 1)
257Don't GETC, Get Perl
46fc3d4c 258Perl is number 1
a7adf1f0 259and destroyed as well
a6006777 260########
261my @a; $a[2] = 1; for (@a) { $_ = 2 } print "@a\n"
262EXPECT
2632 2 2
264########
8ff950ac 265# used to attach defelem magic to all immortal values,
8b530633 266# which made restore of local $_ fail.
267foo(2>1);
268sub foo { bar() for @_; }
269sub bar { local $_; }
270print "ok\n";
271EXPECT
272ok
273########
a6006777 274@a = ($a, $b, $c, $d) = (5, 6);
275print "ok\n"
276 if ($a[0] == 5 and $a[1] == 6 and !defined $a[2] and !defined $a[3]);
277EXPECT
278ok
279########
280print "ok\n" if (1E2<<1 == 200 and 3E4<<3 == 240000);
281EXPECT
282ok
283########
8ebc5c01 284print "ok\n" if ("\0" lt "\xFF");
a6006777 285EXPECT
286ok
287########
288open(H,'op/misc.t'); # must be in the 't' directory
289stat(H);
290print "ok\n" if (-e _ and -f _ and -r _);
291EXPECT
292ok
293########
294sub thing { 0 || return qw(now is the time) }
295print thing(), "\n";
296EXPECT
297nowisthetime
298########
299$ren = 'joy';
300$stimpy = 'happy';
301{ local $main::{ren} = *stimpy; print $ren, ' ' }
302print $ren, "\n";
303EXPECT
304happy joy
305########
306$stimpy = 'happy';
307{ local $main::{ren} = *stimpy; print ${'ren'}, ' ' }
308print +(defined(${'ren'}) ? 'oops' : 'joy'), "\n";
309EXPECT
310happy joy
311########
312package p;
313sub func { print 'really ' unless wantarray; 'p' }
314sub groovy { 'groovy' }
315package main;
316print p::func()->groovy(), "\n"
317EXPECT
318really groovy
319########
d53f8f1c 320@list = ([ 'one', 1 ], [ 'two', 2 ]);
321sub func { $num = shift; (grep $_->[1] == $num, @list)[0] }
322print scalar(map &func($_), 1 .. 3), " ",
323 scalar(map scalar &func($_), 1 .. 3), "\n";
324EXPECT
3252 3
326########
44a8e56a 327($k, $s) = qw(x 0);
328@{$h{$k}} = qw(1 2 4);
329for (@{$h{$k}}) { $s += $_; delete $h{$k} if ($_ == 2) }
330print "bogus\n" unless $s == 7;
331########
332my $a = 'outer';
333eval q[ my $a = 'inner'; eval q[ print "$a " ] ];
334eval { my $x = 'peace'; eval q[ print "$x\n" ] }
335EXPECT
336inner peace
774d564b 337########
338-w
339$| = 1;
340sub foo {
341 print "In foo1\n";
342 eval 'sub foo { print "In foo2\n" }';
343 print "Exiting foo1\n";
344}
345foo;
346foo;
347EXPECT
348In foo1
349Subroutine foo redefined at (eval 1) line 1.
350Exiting foo1
351In foo2
352########
353$s = 0;
354map {#this newline here tickles the bug
355$s += $_} (1,2,4);
356print "eat flaming death\n" unless ($s == 7);
1ca7b98a 357########
358sub foo { local $_ = shift; split; @_ }
359@x = foo(' x y z ');
360print "you die joe!\n" unless "@x" eq 'x y z';
c277df42 361########
362/(?{"{"})/ # Check it outside of eval too
363EXPECT
2cd61cdb 364Sequence (?{...}) not terminated or not {}-balanced at - line 1, within pattern
7253e4e3 365Sequence (?{...}) not terminated or not {}-balanced in regex; marked by <-- HERE in m/(?{ <-- HERE "{"})/ at - line 1.
c277df42 366########
367/(?{"{"}})/ # Check it outside of eval too
368EXPECT
d98d5fff 369Unmatched right curly bracket at (re_eval 1) line 1, at end of line
c277df42 370syntax error at (re_eval 1) line 1, near ""{"}"
2cd61cdb 371Compilation failed in regexp at - line 1.
0da4822f 372########
ff689196 373BEGIN { @ARGV = qw(a b c d e) }
0da4822f 374BEGIN { print "argv <@ARGV>\nbegin <",shift,">\n" }
375END { print "end <",shift,">\nargv <@ARGV>\n" }
376INIT { print "init <",shift,">\n" }
7d30b5c4 377CHECK { print "check <",shift,">\n" }
0da4822f 378EXPECT
ff689196 379argv <a b c d e>
0da4822f 380begin <a>
7d30b5c4 381check <b>
ff689196 382init <c>
383end <d>
384argv <e>
4599a1de 385########
3500f679 386-l
387# fdopen from a system descriptor to a system descriptor used to close
388# the former.
389open STDERR, '>&=STDOUT' or die $!;
75642110 390select STDOUT; $| = 1; print fileno STDOUT or die $!;
391select STDERR; $| = 1; print fileno STDERR or die $!;
3500f679 392EXPECT
3931
3942
395########
20408e3c 396-w
397sub testme { my $a = "test"; { local $a = "new test"; print $a }}
398EXPECT
399Can't localize lexical variable $a at - line 2.
400########
51ae5c03 401package X;
402sub ascalar { my $r; bless \$r }
403sub DESTROY { print "destroyed\n" };
404package main;
405*s = ascalar X;
406EXPECT
407destroyed
408########
409package X;
410sub anarray { bless [] }
411sub DESTROY { print "destroyed\n" };
412package main;
413*a = anarray X;
414EXPECT
415destroyed
416########
417package X;
418sub ahash { bless {} }
419sub DESTROY { print "destroyed\n" };
420package main;
421*h = ahash X;
422EXPECT
423destroyed
424########
425package X;
426sub aclosure { my $x; bless sub { ++$x } }
427sub DESTROY { print "destroyed\n" };
428package main;
429*c = aclosure X;
430EXPECT
431destroyed
432########
433package X;
434sub any { bless {} }
435my $f = "FH000"; # just to thwart any future optimisations
ded8aa31 436sub afh { select select ++$f; my $r = *{$f}{IO}; delete $X::{$f}; bless $r }
51ae5c03 437sub DESTROY { print "destroyed\n" }
438package main;
439$x = any X; # to bump sv_objcount. IO objs aren't counted??
440*f = afh X;
441EXPECT
442destroyed
443destroyed
444########
ebf99b04 445BEGIN {
446 $| = 1;
447 $SIG{__WARN__} = sub {
448 eval { print $_[0] };
449 die "bar\n";
450 };
451 warn "foo\n";
452}
453EXPECT
454foo
455bar
456BEGIN failed--compilation aborted at - line 8.
8feb4e9f 457########
458package X;
459@ISA='Y';
460sub 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}
469sub DESTROY {
470 my $self = shift;
471 print "DESTROY", $self->foo;
472}
473package Y;
474sub 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}
483sub AUTOLOAD {
484 $AUTOLOAD =~ /::([^:]+)$/;
485 my $method = $1;
486 splice @_, 1, 0, $method;
487 goto &attribute;
488}
489package main;
490my $x = X->new(1);
491for (2..3) {
492 my $y = X->new($_);
493 print $y->foo;
494}
495print $x->foo;
496EXPECT
497new1new22DESTROY2new33DESTROY31DESTROY1
dfad63ad 498########
499re();
500sub re {
14455d6c 501 my $re = join '', eval 'qr/(??{ $obj->method })/';
dfad63ad 502 $re;
503}
504EXPECT
1aff0e91 505########
506use strict;
507my $foo = "ZZZ\n";
508END { print $foo }
509EXPECT
510ZZZ
511########
512eval '
513use strict;
514my $foo = "ZZZ\n";
515END { print $foo }
516';
517EXPECT
518ZZZ
7399586d 519########
520-w
521if (@ARGV) { print "" }
522else {
523 if ($x == 0) { print "" } else { print $x }
524}
525EXPECT
b89fed5f 526Use of uninitialized value in numeric eq (==) at - line 4.
1d76a5c3 527########
528$x = sub {};
529foo();
530sub foo { eval { return }; }
531print "ok\n";
532EXPECT
533ok
07447971 534########
b0f2b690 535# moved to op/lc.t
07447971 536EXPECT
92d29cee 537########
538sub f { my $a = 1; my $b = 2; my $c = 3; my $d = 4; next }
539my $x = "foo";
540{ f } continue { print $x, "\n" }
541EXPECT
542foo
6a7129a1 543########
544sub C () { 1 }
545sub M { $_[0] = 2; }
546eval "C";
547M(C);
548EXPECT
549Modification of a read-only value attempted at - line 2.
00c29ff8 550########
551print qw(ab a\b a\\b);
552EXPECT
553aba\ba\b
dd8482fc 554########
c71fccf1 555# lexicals declared after the myeval() definition should not be visible
556# within it
557sub myeval { eval $_[0] }
558my $foo = "ok 2\n";
559myeval('sub foo { local $foo = "ok 1\n"; print $foo; }');
560die $@ if $@;
561foo();
562print $foo;
563EXPECT
564ok 1
565ok 2
566########
2090ab20 567# lexicals outside an eval"" should be visible inside subroutine definitions
568# within it
569eval <<'EOT'; die $@ if $@;
570{
571 my $X = "ok\n";
572 eval 'sub Y { print $X }'; die $@ if $@;
573 Y();
574}
575EOT
576EXPECT
577ok
578########
c975facc 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}();
585print $x;
586EXPECT
587ok 1
588########
dd8482fc 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".
c9f931b8 593BEGIN {
594 eval { require POSIX };
595 if ($@) {
596 exit(0); # running minitest?
597 }
598}
dd8482fc 599use Config;
600my $have_setlocale = $Config{d_setlocale} eq 'define';
dd8482fc 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
2986a63f 604$have_setlocale = 0 if (($^O eq 'MSWin32' || $^O eq 'NetWare') && $Config{cc} =~ /^(cl|gcc)/i);
dd8482fc 605exit(0) unless $have_setlocale;
606my @locales;
607if (-x "/usr/bin/locale" && open(LOCALES, "/usr/bin/locale -a|")) {
608 while(<LOCALES>) {
609 chomp;
610 push(@locales, $_);
611 }
612 close(LOCALES);
613}
614exit(0) unless @locales;
615for (@locales) {
616 use POSIX qw(locale_h);
617 use locale;
88e7acd2 618 setlocale(LC_NUMERIC, $_) or next;
dd8482fc 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}
623EXPECT
b927783e 624########
625die qr(x)
626EXPECT
627(?-xism:x) at - line 1.
628########
7ef822cd 629# 20001210.003 mjd@plover.com
630format REMITOUT_TOP =
631FOO
632.
633
634format REMITOUT =
635BAR
636.
637
638# This loop causes a segv in 5.6.0
639for $lineno (1..61) {
640 write REMITOUT;
641}
642
643print "It's OK!";
644EXPECT
645It's OK!
cb55de95 646########
647# Inaba Hiroto
648reset;
649if (0) {
650 if ("" =~ //) {
651 }
652}
653########
654# Nicholas Clark
655$ENV{TERM} = 0;
656reset;
657// if 0;
658########
659# Vadim Konovalov
660use strict;
661sub new_pmop($) {
662 my $pm = shift;
663 return eval "sub {shift=~/$pm/}";
664}
665new_pmop "abcdef"; reset;
666new_pmop "abcdef"; reset;
667new_pmop "abcdef"; reset;
668new_pmop "abcdef"; reset;
880649cd 669########
670# David Dyck
671# coredump in 5.7.1
672close STDERR; die;
673EXPECT
dba9804b 674########
99799961 675-w
676"x" =~ /(\G?x)?/; # core dump in 20000716.007
677EXPECT
678Quantifier unexpected on zero-length expression in regex; marked by <-- HERE in m/(\G?x)? <-- HERE / at - line 2.
679########
dba9804b 680# Bug 20010515.004
681my @h = 1 .. 10;
682bad(@h);
683sub bad {
684 undef @h;
685 print "O";
686 print for @_;
687 print "K";
688}
689EXPECT
690OK
97b9a4cb 691########
692# Bug 20010506.041
693"abcd\x{1234}" =~ /(a)(b[c])(d+)?/i and print "ok\n";
694EXPECT
695ok
0b490c9c 696########
697# Bug 20010422.005
698{s//${}/; //}
699EXPECT
2172ddaf 700syntax error at - line 2, near "${}"
0b490c9c 701Execution of - aborted due to compilation errors.
585602fa 702########
703# Bug 20010528.007
704"\x{"
705EXPECT
706Missing right brace on \x{} at - line 2, within string
707Execution of - aborted due to compilation errors.
08b362fd 708########
709my $foo = Bar->new();
710my @dst;
711END {
712 ($_ = "@dst") =~ s/\(0x.+?\)/(0x...)/;
713 print $_, "\n";
714}
715package Bar;
716sub new {
717 my Bar $self = bless [], Bar;
718 eval '$self';
719 return $self;
720}
721sub DESTROY {
722 push @dst, "$_[0]";
723}
724EXPECT
725Bar=ARRAY(0x...)
1f96ff2a 726########
ecbfecb9 727printf "%x %x", unpack "U*", sprintf "\x{1234}%s", "\x{5678}"
728EXPECT
7291234 5678
730########
731# keep this last - doesn't seem to work otherwise?
1f96ff2a 732eval "a.b.c.d.e.f;sub"
733EXPECT