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