Warn about numconvert.t in case somebody
[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';
93430cb4 7unshift @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;
18END { unlink $tmpfile if $tmpfile; }
19
68dc0745 20$CAT = (($^O eq 'MSWin32') ? '.\perl -e "print <>"' : 'cat');
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: $!";
29 print TEST $prog, "\n";
30 close TEST or die "Cannot close $tmpfile: $!";
31
68dc0745 32 if ($^O eq 'MSWin32') {
648cac19 33 $results = `.\\perl -I../lib $switch $tmpfile 2>&1`;
68dc0745 34 }
35 else {
648cac19 36 $results = `./perl $switch $tmpfile 2>&1`;
68dc0745 37 }
a0d0e21e 38 $status = $?;
a0d0e21e 39 $results =~ s/\n+$//;
648cac19 40 $results =~ s/at\s+misctmp\d+\s+line/at - line/g;
41 $results =~ s/of\s+misctmp\d+\s+aborted/of - aborted/g;
f0ec1f9a 42# bison says 'parse error' instead of 'syntax error',
d91e2bdb 43# various yaccs may or may not capitalize 'syntax'.
2a8ee232 44 $results =~ s/^(syntax|parse) error/syntax error/mig;
a0d0e21e 45 $expected =~ s/\n+$//;
8feb4e9f 46 if ( $results ne $expected ) {
a0d0e21e 47 print STDERR "PROG: $switch\n$prog\n";
48 print STDERR "EXPECTED:\n$expected\n";
49 print STDERR "GOT:\n$results\n";
50 print "not ";
51 }
52 print "ok ", ++$i, "\n";
53}
54
55__END__
2ace3117 56()=()
57########
44a8e56a 58$a = ":="; split /($a)/o, "a:=b:=c"; print "@_"
59EXPECT
60a := b := c
61########
ea12c2aa 62eval { $q = pack "q", 0 };
63if ($@) {
36477c24 64$cusp = ~0 ^ (~0 >> 1);
65$, = " ";
66print +($cusp - 1) % 8, $cusp % 8, -$cusp % 8, ($cusp + 1) % 8, "!\n";
ea12c2aa 67} else {
68# We are on a 64-bit platform: fake it.
69# (If we have long doubles we might not need to fake it.)
70# Background: the $cusp will get converted from a UV into an NV because of
71# the subtraction and addition. Taking away or adding 1 from such a large
72# NV doesn't actually change the NV, so the modulo fails.
73print "7 0 0 1 !\n";
74}
36477c24 75EXPECT
767 0 0 1 !
77########
a0d0e21e 78$foo=undef; $foo->go;
79EXPECT
72b5445b 80Can't call method "go" on an undefined value at - line 1.
a0d0e21e 81########
82BEGIN
83 {
84 "foo";
85 }
86########
a0d0e21e 87$array[128]=1
88########
89$x=0x0eabcd; print $x->ref;
90EXPECT
91Can't call method "ref" without a package or object reference at - line 1.
92########
648cac19 93chop ($str .= <DATA>);
a0d0e21e 94########
95close ($banana);
96########
97$x=2;$y=3;$x<$y ? $x : $y += 23;print $x;
98EXPECT
9925
100########
101eval {sub bar {print "In bar";}}
102########
68dc0745 103system './perl -ne "print if eof" /dev/null'
a0d0e21e 104########
648cac19 105chop($file = <DATA>);
a0d0e21e 106########
107package N;
108sub new {my ($obj,$n)=@_; bless \$n}
109$aa=new N 1;
110$aa=12345;
111print $aa;
112EXPECT
11312345
114########
115%@x=0;
116EXPECT
3fe9a6f1 117Can't modify hash deref in repeat at - line 1, near "0;"
118Execution of - aborted due to compilation errors.
a0d0e21e 119########
120$_="foo";
121printf(STDOUT "%s\n", $_);
122EXPECT
123foo
124########
125push(@a, 1, 2, 3,)
126########
127quotemeta ""
128########
129for ("ABCDE") {
130 &sub;
131s/./&sub($&)/eg;
132print;}
133sub sub {local($_) = @_;
134$_ x 4;}
135EXPECT
136Modification of a read-only value attempted at - line 3.
137########
138package FOO;sub new {bless {FOO => BAR}};
139package main;
140use strict vars;
141my $self = new FOO;
142print $$self{FOO};
143EXPECT
144BAR
145########
146$_="foo";
147s/.{1}//s;
148print;
149EXPECT
150oo
151########
152print scalar ("foo","bar")
153EXPECT
154bar
155########
156sub by_number { $a <=> $b; };# inline function for sort below
157$as_ary{0}="a0";
158@ordered_array=sort by_number keys(%as_ary);
159########
160sub NewShell
161{
162 local($Host) = @_;
163 my($m2) = $#Shells++;
164 $Shells[$m2]{HOST} = $Host;
165 return $m2;
166}
167
168sub ShowShell
169{
170 local($i) = @_;
171}
172
173&ShowShell(&NewShell(beach,Work,"+0+0"));
174&ShowShell(&NewShell(beach,Work,"+0+0"));
175&ShowShell(&NewShell(beach,Work,"+0+0"));
176########
177 {
178 package FAKEARRAY;
179
180 sub TIEARRAY
181 { print "TIEARRAY @_\n";
182 die "bomb out\n" unless $count ++ ;
183 bless ['foo']
184 }
185 sub FETCH { print "fetch @_\n"; $_[0]->[$_[1]] }
186 sub STORE { print "store @_\n"; $_[0]->[$_[1]] = $_[2] }
187 sub DESTROY { print "DESTROY \n"; undef @{$_[0]}; }
188 }
189
190eval 'tie @h, FAKEARRAY, fred' ;
191tie @h, FAKEARRAY, fred ;
192EXPECT
193TIEARRAY FAKEARRAY fred
194TIEARRAY FAKEARRAY fred
195DESTROY
196########
197BEGIN { die "phooey\n" }
198EXPECT
199phooey
200BEGIN failed--compilation aborted at - line 1.
201########
202BEGIN { 1/$zero }
203EXPECT
204Illegal division by zero at - line 1.
205BEGIN failed--compilation aborted at - line 1.
206########
207BEGIN { undef = 0 }
208EXPECT
209Modification of a read-only value attempted at - line 1.
210BEGIN failed--compilation aborted at - line 1.
a7adf1f0 211########
212{
213 package foo;
214 sub PRINT {
215 shift;
216 print join(' ', reverse @_)."\n";
217 }
46fc3d4c 218 sub PRINTF {
219 shift;
220 my $fmt = shift;
221 print sprintf($fmt, @_)."\n";
222 }
a7adf1f0 223 sub TIEHANDLE {
224 bless {}, shift;
225 }
58f51617 226 sub READLINE {
227 "Out of inspiration";
228 }
a7adf1f0 229 sub DESTROY {
230 print "and destroyed as well\n";
2ae324a7 231 }
232 sub READ {
233 shift;
234 print STDOUT "foo->can(READ)(@_)\n";
235 return 100;
236 }
237 sub GETC {
238 shift;
239 print STDOUT "Don't GETC, Get Perl\n";
240 return "a";
241 }
a7adf1f0 242}
243{
244 local(*FOO);
245 tie(*FOO,'foo');
246 print FOO "sentence.", "reversed", "a", "is", "This";
58f51617 247 print "-- ", <FOO>, " --\n";
2ae324a7 248 my($buf,$len,$offset);
249 $buf = "string";
250 $len = 10; $offset = 1;
251 read(FOO, $buf, $len, $offset) == 100 or die "foo->READ failed";
252 getc(FOO) eq "a" or die "foo->GETC failed";
46fc3d4c 253 printf "%s is number %d\n", "Perl", 1;
a7adf1f0 254}
255EXPECT
256This is a reversed sentence.
58f51617 257-- Out of inspiration --
2ae324a7 258foo->can(READ)(string 10 1)
259Don't GETC, Get Perl
46fc3d4c 260Perl is number 1
a7adf1f0 261and destroyed as well
a6006777 262########
263my @a; $a[2] = 1; for (@a) { $_ = 2 } print "@a\n"
264EXPECT
2652 2 2
266########
267@a = ($a, $b, $c, $d) = (5, 6);
268print "ok\n"
269 if ($a[0] == 5 and $a[1] == 6 and !defined $a[2] and !defined $a[3]);
270EXPECT
271ok
272########
273print "ok\n" if (1E2<<1 == 200 and 3E4<<3 == 240000);
274EXPECT
275ok
276########
8ebc5c01 277print "ok\n" if ("\0" lt "\xFF");
a6006777 278EXPECT
279ok
280########
281open(H,'op/misc.t'); # must be in the 't' directory
282stat(H);
283print "ok\n" if (-e _ and -f _ and -r _);
284EXPECT
285ok
286########
287sub thing { 0 || return qw(now is the time) }
288print thing(), "\n";
289EXPECT
290nowisthetime
291########
292$ren = 'joy';
293$stimpy = 'happy';
294{ local $main::{ren} = *stimpy; print $ren, ' ' }
295print $ren, "\n";
296EXPECT
297happy joy
298########
299$stimpy = 'happy';
300{ local $main::{ren} = *stimpy; print ${'ren'}, ' ' }
301print +(defined(${'ren'}) ? 'oops' : 'joy'), "\n";
302EXPECT
303happy joy
304########
305package p;
306sub func { print 'really ' unless wantarray; 'p' }
307sub groovy { 'groovy' }
308package main;
309print p::func()->groovy(), "\n"
310EXPECT
311really groovy
312########
d53f8f1c 313@list = ([ 'one', 1 ], [ 'two', 2 ]);
314sub func { $num = shift; (grep $_->[1] == $num, @list)[0] }
315print scalar(map &func($_), 1 .. 3), " ",
316 scalar(map scalar &func($_), 1 .. 3), "\n";
317EXPECT
3182 3
319########
44a8e56a 320($k, $s) = qw(x 0);
321@{$h{$k}} = qw(1 2 4);
322for (@{$h{$k}}) { $s += $_; delete $h{$k} if ($_ == 2) }
323print "bogus\n" unless $s == 7;
324########
325my $a = 'outer';
326eval q[ my $a = 'inner'; eval q[ print "$a " ] ];
327eval { my $x = 'peace'; eval q[ print "$x\n" ] }
328EXPECT
329inner peace
774d564b 330########
331-w
332$| = 1;
333sub foo {
334 print "In foo1\n";
335 eval 'sub foo { print "In foo2\n" }';
336 print "Exiting foo1\n";
337}
338foo;
339foo;
340EXPECT
341In foo1
342Subroutine foo redefined at (eval 1) line 1.
343Exiting foo1
344In foo2
345########
346$s = 0;
347map {#this newline here tickles the bug
348$s += $_} (1,2,4);
349print "eat flaming death\n" unless ($s == 7);
1ca7b98a 350########
351sub foo { local $_ = shift; split; @_ }
352@x = foo(' x y z ');
353print "you die joe!\n" unless "@x" eq 'x y z';
c277df42 354########
355/(?{"{"})/ # Check it outside of eval too
356EXPECT
2cd61cdb 357Sequence (?{...}) not terminated or not {}-balanced at - line 1, within pattern
358/(?{"{"})/: Sequence (?{...}) not terminated or not {}-balanced at - line 1.
c277df42 359########
360/(?{"{"}})/ # Check it outside of eval too
361EXPECT
d98d5fff 362Unmatched right curly bracket at (re_eval 1) line 1, at end of line
c277df42 363syntax error at (re_eval 1) line 1, near ""{"}"
2cd61cdb 364Compilation failed in regexp at - line 1.
0da4822f 365########
366BEGIN { @ARGV = qw(a b c) }
367BEGIN { print "argv <@ARGV>\nbegin <",shift,">\n" }
368END { print "end <",shift,">\nargv <@ARGV>\n" }
369INIT { print "init <",shift,">\n" }
370EXPECT
371argv <a b c>
372begin <a>
373init <b>
374end <c>
375argv <>
4599a1de 376########
3500f679 377-l
378# fdopen from a system descriptor to a system descriptor used to close
379# the former.
380open STDERR, '>&=STDOUT' or die $!;
381select STDOUT; $| = 1; print fileno STDOUT;
382select STDERR; $| = 1; print fileno STDERR;
383EXPECT
3841
3852
386########
20408e3c 387-w
388sub testme { my $a = "test"; { local $a = "new test"; print $a }}
389EXPECT
390Can't localize lexical variable $a at - line 2.
391########
51ae5c03 392package X;
393sub ascalar { my $r; bless \$r }
394sub DESTROY { print "destroyed\n" };
395package main;
396*s = ascalar X;
397EXPECT
398destroyed
399########
400package X;
401sub anarray { bless [] }
402sub DESTROY { print "destroyed\n" };
403package main;
404*a = anarray X;
405EXPECT
406destroyed
407########
408package X;
409sub ahash { bless {} }
410sub DESTROY { print "destroyed\n" };
411package main;
412*h = ahash X;
413EXPECT
414destroyed
415########
416package X;
417sub aclosure { my $x; bless sub { ++$x } }
418sub DESTROY { print "destroyed\n" };
419package main;
420*c = aclosure X;
421EXPECT
422destroyed
423########
424package X;
425sub any { bless {} }
426my $f = "FH000"; # just to thwart any future optimisations
ded8aa31 427sub afh { select select ++$f; my $r = *{$f}{IO}; delete $X::{$f}; bless $r }
51ae5c03 428sub DESTROY { print "destroyed\n" }
429package main;
430$x = any X; # to bump sv_objcount. IO objs aren't counted??
431*f = afh X;
432EXPECT
433destroyed
434destroyed
435########
ebf99b04 436BEGIN {
437 $| = 1;
438 $SIG{__WARN__} = sub {
439 eval { print $_[0] };
440 die "bar\n";
441 };
442 warn "foo\n";
443}
444EXPECT
445foo
446bar
447BEGIN failed--compilation aborted at - line 8.
8feb4e9f 448########
449package X;
450@ISA='Y';
451sub new {
452 my $class = shift;
453 my $self = { };
454 bless $self, $class;
455 my $init = shift;
456 $self->foo($init);
457 print "new", $init;
458 return $self;
459}
460sub DESTROY {
461 my $self = shift;
462 print "DESTROY", $self->foo;
463}
464package Y;
465sub attribute {
466 my $self = shift;
467 my $var = shift;
468 if (@_ == 0) {
469 return $self->{$var};
470 } elsif (@_ == 1) {
471 $self->{$var} = shift;
472 }
473}
474sub AUTOLOAD {
475 $AUTOLOAD =~ /::([^:]+)$/;
476 my $method = $1;
477 splice @_, 1, 0, $method;
478 goto &attribute;
479}
480package main;
481my $x = X->new(1);
482for (2..3) {
483 my $y = X->new($_);
484 print $y->foo;
485}
486print $x->foo;
487EXPECT
488new1new22DESTROY2new33DESTROY31DESTROY1
dfad63ad 489########
490re();
491sub re {
492 my $re = join '', eval 'qr/(?p{ $obj->method })/';
493 $re;
494}
495EXPECT
1aff0e91 496########
497use strict;
498my $foo = "ZZZ\n";
499END { print $foo }
500EXPECT
501ZZZ
502########
503eval '
504use strict;
505my $foo = "ZZZ\n";
506END { print $foo }
507';
508EXPECT
509ZZZ
7399586d 510########
511-w
512if (@ARGV) { print "" }
513else {
514 if ($x == 0) { print "" } else { print $x }
515}
516EXPECT
517Use of uninitialized value at - line 4.