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