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