[patch: perl@8211]VMS: add -Duseperlio capacity to configure.com
[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     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 use integer;
64 $, = " ";
65 print +($cusp - 1) % 8, $cusp % 8, -$cusp % 8, 8 | (($cusp + 1) % 8 + 7), "!\n";
66 EXPECT
67 7 0 0 8 !
68 ########
69 $foo=undef; $foo->go;
70 EXPECT
71 Can't call method "go" on an undefined value at - line 1.
72 ########
73 BEGIN
74         {
75             "foo";
76         }
77 ########
78 $array[128]=1
79 ########
80 $x=0x0eabcd; print $x->ref;
81 EXPECT
82 Can't call method "ref" without a package or object reference at - line 1.
83 ########
84 chop ($str .= <DATA>);
85 ########
86 close ($banana);
87 ########
88 $x=2;$y=3;$x<$y ? $x : $y += 23;print $x;
89 EXPECT
90 25
91 ########
92 eval {sub bar {print "In bar";}}
93 ########
94 system './perl -ne "print if eof" /dev/null'
95 ########
96 chop($file = <DATA>);
97 ########
98 package N;
99 sub new {my ($obj,$n)=@_; bless \$n}  
100 $aa=new N 1;
101 $aa=12345;
102 print $aa;
103 EXPECT
104 12345
105 ########
106 %@x=0;
107 EXPECT
108 Can't modify hash dereference in repeat (x) at - line 1, near "0;"
109 Execution of - aborted due to compilation errors.
110 ########
111 $_="foo";
112 printf(STDOUT "%s\n", $_);
113 EXPECT
114 foo
115 ########
116 push(@a, 1, 2, 3,)
117 ########
118 quotemeta ""
119 ########
120 for ("ABCDE") {
121  &sub;
122 s/./&sub($&)/eg;
123 print;}
124 sub sub {local($_) = @_;
125 $_ x 4;}
126 EXPECT
127 Modification of a read-only value attempted at - line 3.
128 ########
129 package FOO;sub new {bless {FOO => BAR}};
130 package main;
131 use strict vars;   
132 my $self = new FOO;
133 print $$self{FOO};
134 EXPECT
135 BAR
136 ########
137 $_="foo";
138 s/.{1}//s;
139 print;
140 EXPECT
141 oo
142 ########
143 print scalar ("foo","bar")
144 EXPECT
145 bar
146 ########
147 sub by_number { $a <=> $b; };# inline function for sort below
148 $as_ary{0}="a0";
149 @ordered_array=sort by_number keys(%as_ary);
150 ########
151 sub NewShell
152 {
153   local($Host) = @_;
154   my($m2) = $#Shells++;
155   $Shells[$m2]{HOST} = $Host;
156   return $m2;
157 }
158  
159 sub 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    
181 eval 'tie @h, FAKEARRAY, fred' ;
182 tie @h, FAKEARRAY, fred ;
183 EXPECT
184 TIEARRAY FAKEARRAY fred
185 TIEARRAY FAKEARRAY fred
186 DESTROY 
187 ########
188 BEGIN { die "phooey\n" }
189 EXPECT
190 phooey
191 BEGIN failed--compilation aborted at - line 1.
192 ########
193 BEGIN { 1/$zero }
194 EXPECT
195 Illegal division by zero at - line 1.
196 BEGIN failed--compilation aborted at - line 1.
197 ########
198 BEGIN { undef = 0 }
199 EXPECT
200 Modification of a read-only value attempted at - line 1.
201 BEGIN failed--compilation aborted at - line 1.
202 ########
203 {
204     package foo;
205     sub PRINT {
206         shift;
207         print join(' ', reverse @_)."\n";
208     }
209     sub PRINTF {
210         shift;
211           my $fmt = shift;
212         print sprintf($fmt, @_)."\n";
213     }
214     sub TIEHANDLE {
215         bless {}, shift;
216     }
217     sub READLINE {
218         "Out of inspiration";
219     }
220     sub DESTROY {
221         print "and destroyed as well\n";
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   }    
233 }
234 {
235     local(*FOO);
236     tie(*FOO,'foo');
237     print FOO "sentence.", "reversed", "a", "is", "This";
238     print "-- ", <FOO>, " --\n";
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";
244     printf "%s is number %d\n", "Perl", 1;
245 }
246 EXPECT
247 This is a reversed sentence.
248 -- Out of inspiration --
249 foo->can(READ)(string 10 1)
250 Don't GETC, Get Perl
251 Perl is number 1
252 and destroyed as well
253 ########
254 my @a; $a[2] = 1; for (@a) { $_ = 2 } print "@a\n"
255 EXPECT
256 2 2 2
257 ########
258 @a = ($a, $b, $c, $d) = (5, 6);
259 print "ok\n"
260   if ($a[0] == 5 and $a[1] == 6 and !defined $a[2] and !defined $a[3]);
261 EXPECT
262 ok
263 ########
264 print "ok\n" if (1E2<<1 == 200 and 3E4<<3 == 240000);
265 EXPECT
266 ok
267 ########
268 print "ok\n" if ("\0" lt "\xFF");
269 EXPECT
270 ok
271 ########
272 open(H,'op/misc.t'); # must be in the 't' directory
273 stat(H);
274 print "ok\n" if (-e _ and -f _ and -r _);
275 EXPECT
276 ok
277 ########
278 sub thing { 0 || return qw(now is the time) }
279 print thing(), "\n";
280 EXPECT
281 nowisthetime
282 ########
283 $ren = 'joy';
284 $stimpy = 'happy';
285 { local $main::{ren} = *stimpy; print $ren, ' ' }
286 print $ren, "\n";
287 EXPECT
288 happy joy
289 ########
290 $stimpy = 'happy';
291 { local $main::{ren} = *stimpy; print ${'ren'}, ' ' }
292 print +(defined(${'ren'}) ? 'oops' : 'joy'), "\n";
293 EXPECT
294 happy joy
295 ########
296 package p;
297 sub func { print 'really ' unless wantarray; 'p' }
298 sub groovy { 'groovy' }
299 package main;
300 print p::func()->groovy(), "\n"
301 EXPECT
302 really groovy
303 ########
304 @list = ([ 'one', 1 ], [ 'two', 2 ]);
305 sub func { $num = shift; (grep $_->[1] == $num, @list)[0] }
306 print scalar(map &func($_), 1 .. 3), " ",
307       scalar(map scalar &func($_), 1 .. 3), "\n";
308 EXPECT
309 2 3
310 ########
311 ($k, $s)  = qw(x 0);
312 @{$h{$k}} = qw(1 2 4);
313 for (@{$h{$k}}) { $s += $_; delete $h{$k} if ($_ == 2) }
314 print "bogus\n" unless $s == 7;
315 ########
316 my $a = 'outer';
317 eval q[ my $a = 'inner'; eval q[ print "$a " ] ];
318 eval { my $x = 'peace'; eval q[ print "$x\n" ] }
319 EXPECT
320 inner peace
321 ########
322 -w
323 $| = 1;
324 sub foo {
325     print "In foo1\n";
326     eval 'sub foo { print "In foo2\n" }';
327     print "Exiting foo1\n";
328 }
329 foo;
330 foo;
331 EXPECT
332 In foo1
333 Subroutine foo redefined at (eval 1) line 1.
334 Exiting foo1
335 In foo2
336 ########
337 $s = 0;
338 map {#this newline here tickles the bug
339 $s += $_} (1,2,4);
340 print "eat flaming death\n" unless ($s == 7);
341 ########
342 sub foo { local $_ = shift; split; @_ }
343 @x = foo(' x  y  z ');
344 print "you die joe!\n" unless "@x" eq 'x y z';
345 ########
346 /(?{"{"})/      # Check it outside of eval too
347 EXPECT
348 Sequence (?{...}) not terminated or not {}-balanced at - line 1, within pattern
349 Sequence (?{...}) not terminated or not {}-balanced before HERE mark in regex m/(?{ << HERE "{"})/ at - line 1.
350 ########
351 /(?{"{"}})/     # Check it outside of eval too
352 EXPECT
353 Unmatched right curly bracket at (re_eval 1) line 1, at end of line
354 syntax error at (re_eval 1) line 1, near ""{"}"
355 Compilation failed in regexp at - line 1.
356 ########
357 BEGIN { @ARGV = qw(a b c d e) }
358 BEGIN { print "argv <@ARGV>\nbegin <",shift,">\n" }
359 END { print "end <",shift,">\nargv <@ARGV>\n" }
360 INIT { print "init <",shift,">\n" }
361 CHECK { print "check <",shift,">\n" }
362 EXPECT
363 argv <a b c d e>
364 begin <a>
365 check <b>
366 init <c>
367 end <d>
368 argv <e>
369 ########
370 -l
371 # fdopen from a system descriptor to a system descriptor used to close
372 # the former.
373 open STDERR, '>&=STDOUT' or die $!;
374 select STDOUT; $| = 1; print fileno STDOUT or die $!;
375 select STDERR; $| = 1; print fileno STDERR or die $!;
376 EXPECT
377 1
378 2
379 ########
380 -w
381 sub testme { my $a = "test"; { local $a = "new test"; print $a }}
382 EXPECT
383 Can't localize lexical variable $a at - line 2.
384 ########
385 package X;
386 sub ascalar { my $r; bless \$r }
387 sub DESTROY { print "destroyed\n" };
388 package main;
389 *s = ascalar X;
390 EXPECT
391 destroyed
392 ########
393 package X;
394 sub anarray { bless [] }
395 sub DESTROY { print "destroyed\n" };
396 package main;
397 *a = anarray X;
398 EXPECT
399 destroyed
400 ########
401 package X;
402 sub ahash { bless {} }
403 sub DESTROY { print "destroyed\n" };
404 package main;
405 *h = ahash X;
406 EXPECT
407 destroyed
408 ########
409 package X;
410 sub aclosure { my $x; bless sub { ++$x } }
411 sub DESTROY { print "destroyed\n" };
412 package main;
413 *c = aclosure X;
414 EXPECT
415 destroyed
416 ########
417 package X;
418 sub any { bless {} }
419 my $f = "FH000"; # just to thwart any future optimisations
420 sub afh { select select ++$f; my $r = *{$f}{IO}; delete $X::{$f}; bless $r }
421 sub DESTROY { print "destroyed\n" }
422 package main;
423 $x = any X; # to bump sv_objcount. IO objs aren't counted??
424 *f = afh X;
425 EXPECT
426 destroyed
427 destroyed
428 ########
429 BEGIN {
430   $| = 1;
431   $SIG{__WARN__} = sub {
432     eval { print $_[0] };
433     die "bar\n";
434   };
435   warn "foo\n";
436 }
437 EXPECT
438 foo
439 bar
440 BEGIN failed--compilation aborted at - line 8.
441 ########
442 package X;
443 @ISA='Y';
444 sub 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 }
453 sub DESTROY {
454     my $self = shift;
455     print "DESTROY", $self->foo;
456 }
457 package Y;
458 sub 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 }
467 sub AUTOLOAD {
468     $AUTOLOAD =~ /::([^:]+)$/;
469     my $method = $1;
470     splice @_, 1, 0, $method;
471     goto &attribute;
472 }
473 package main;
474 my $x = X->new(1);
475 for (2..3) {
476     my $y = X->new($_);
477     print $y->foo;
478 }
479 print $x->foo;
480 EXPECT
481 new1new22DESTROY2new33DESTROY31DESTROY1
482 ########
483 re();
484 sub re {
485     my $re = join '', eval 'qr/(??{ $obj->method })/';
486     $re;
487 }
488 EXPECT
489 ########
490 use strict;
491 my $foo = "ZZZ\n";
492 END { print $foo }
493 EXPECT
494 ZZZ
495 ########
496 eval '
497 use strict;
498 my $foo = "ZZZ\n";
499 END { print $foo }
500 ';
501 EXPECT
502 ZZZ
503 ########
504 -w
505 if (@ARGV) { print "" }
506 else {
507   if ($x == 0) { print "" } else { print $x }
508 }
509 EXPECT
510 Use of uninitialized value in numeric eq (==) at - line 4.
511 ########
512 $x = sub {};
513 foo();
514 sub foo { eval { return }; }
515 print "ok\n";
516 EXPECT
517 ok
518 ########
519 my @l = qw(hello.* world);
520 my $x;
521
522 foreach $x (@l) {
523     print "before - $x\n";
524     $x = "\Q$x\E";
525     print "quotemeta - $x\n";
526     $x = "\u$x";
527     print "ucfirst - $x\n";
528     $x = "\l$x";
529     print "lcfirst - $x\n";
530     $x = "\U$x\E";
531     print "uc - $x\n";
532     $x = "\L$x\E";
533     print "lc - $x\n";
534 }
535 EXPECT
536 before - hello.*
537 quotemeta - hello\.\*
538 ucfirst - Hello\.\*
539 lcfirst - hello\.\*
540 uc - HELLO\.\*
541 lc - hello\.\*
542 before - world
543 quotemeta - world
544 ucfirst - World
545 lcfirst - world
546 uc - WORLD
547 lc - world
548 ########
549 sub f { my $a = 1; my $b = 2; my $c = 3; my $d = 4; next }
550 my $x = "foo";
551 { f } continue { print $x, "\n" }
552 EXPECT
553 foo
554 ########
555 sub C () { 1 }
556 sub M { $_[0] = 2; }
557 eval "C";
558 M(C);
559 EXPECT
560 Modification of a read-only value attempted at - line 2.
561 ########
562 print qw(ab a\b a\\b);
563 EXPECT
564 aba\ba\b
565 ########
566 # This test is here instead of pragma/locale.t because
567 # the bug depends on in the internal state of the locale
568 # settings and pragma/locale messes up that state pretty badly.
569 # We need a "fresh run".
570 use Config;
571 my $have_setlocale = $Config{d_setlocale} eq 'define';
572 eval {
573     require POSIX;
574 };
575 $have_setlocale = 0 if $@;
576 # Visual C's CRT goes silly on strings of the form "en_US.ISO8859-1"
577 # and mingw32 uses said silly CRT
578 $have_setlocale = 0 if $^O eq 'MSWin32' && $Config{cc} =~ /^(cl|gcc)/i;
579 exit(0) unless $have_setlocale;
580 my @locales;
581 if (-x "/usr/bin/locale" && open(LOCALES, "/usr/bin/locale -a|")) {
582     while(<LOCALES>) {
583         chomp;
584         push(@locales, $_);
585     }
586     close(LOCALES);
587 }
588 exit(0) unless @locales;
589 for (@locales) {
590     use POSIX qw(locale_h);
591     use locale;
592     setlocale(LC_NUMERIC, $_) or next;
593     my $s = sprintf "%g %g", 3.1, 3.1;
594     next if $s eq '3.1 3.1' || $s =~ /^(3.+1) \1$/;
595     print "$_ $s\n";
596 }
597 EXPECT
598 ########
599 die qr(x)
600 EXPECT
601 (?-xism:x) at - line 1.
602 ########