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