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