(?>) broken in RE
[p5sagit/p5-mst-13.2.git] / t / op / misc.t
CommitLineData
a0d0e21e 1#!./perl
2
fb73857a 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
a0d0e21e 6chdir 't' if -d 't';
7@INC = "../lib";
8$ENV{PERL5LIB} = "../lib";
9
10$|=1;
11
12undef $/;
13@prgs = split "\n########\n", <DATA>;
14print "1..", scalar @prgs, "\n";
15
16$tmpfile = "misctmp000";
171 while -f ++$tmpfile;
18END { unlink $tmpfile if $tmpfile; }
19
68dc0745 20$CAT = (($^O eq 'MSWin32') ? '.\perl -e "print <>"' : 'cat');
21
a0d0e21e 22for (@prgs){
23 my $switch;
fb73857a 24 if (s/^\s*(-\w.*)//){
25 $switch = $1;
a0d0e21e 26 }
27 my($prog,$expected) = split(/\nEXPECT\n/, $_);
68dc0745 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 }
a0d0e21e 34 print TEST $prog, "\n";
35 close TEST;
36 $status = $?;
68dc0745 37 $results = `$CAT $tmpfile`;
a0d0e21e 38 $results =~ s/\n+$//;
9d116dd7 39 $results =~ s/syntax error/syntax error/i;
a0d0e21e 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__
2ace3117 51()=()
52########
44a8e56a 53$a = ":="; split /($a)/o, "a:=b:=c"; print "@_"
54EXPECT
55a := b := c
56########
36477c24 57$cusp = ~0 ^ (~0 >> 1);
58$, = " ";
59print +($cusp - 1) % 8, $cusp % 8, -$cusp % 8, ($cusp + 1) % 8, "!\n";
60EXPECT
617 0 0 1 !
62########
a0d0e21e 63$foo=undef; $foo->go;
64EXPECT
72b5445b 65Can't call method "go" on an undefined value at - line 1.
a0d0e21e 66########
67BEGIN
68 {
69 "foo";
70 }
71########
a0d0e21e 72$array[128]=1
73########
74$x=0x0eabcd; print $x->ref;
75EXPECT
76Can't call method "ref" without a package or object reference at - line 1.
77########
78chop ($str .= <STDIN>);
79########
80close ($banana);
81########
82$x=2;$y=3;$x<$y ? $x : $y += 23;print $x;
83EXPECT
8425
85########
86eval {sub bar {print "In bar";}}
87########
68dc0745 88system './perl -ne "print if eof" /dev/null'
a0d0e21e 89########
90chop($file = <>);
91########
92package N;
93sub new {my ($obj,$n)=@_; bless \$n}
94$aa=new N 1;
95$aa=12345;
96print $aa;
97EXPECT
9812345
99########
100%@x=0;
101EXPECT
3fe9a6f1 102Can't modify hash deref in repeat at - line 1, near "0;"
103Execution of - aborted due to compilation errors.
a0d0e21e 104########
105$_="foo";
106printf(STDOUT "%s\n", $_);
107EXPECT
108foo
109########
110push(@a, 1, 2, 3,)
111########
112quotemeta ""
113########
114for ("ABCDE") {
115 &sub;
116s/./&sub($&)/eg;
117print;}
118sub sub {local($_) = @_;
119$_ x 4;}
120EXPECT
121Modification of a read-only value attempted at - line 3.
122########
123package FOO;sub new {bless {FOO => BAR}};
124package main;
125use strict vars;
126my $self = new FOO;
127print $$self{FOO};
128EXPECT
129BAR
130########
131$_="foo";
132s/.{1}//s;
133print;
134EXPECT
135oo
136########
137print scalar ("foo","bar")
138EXPECT
139bar
140########
141sub by_number { $a <=> $b; };# inline function for sort below
142$as_ary{0}="a0";
143@ordered_array=sort by_number keys(%as_ary);
144########
145sub NewShell
146{
147 local($Host) = @_;
148 my($m2) = $#Shells++;
149 $Shells[$m2]{HOST} = $Host;
150 return $m2;
151}
152
153sub 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
175eval 'tie @h, FAKEARRAY, fred' ;
176tie @h, FAKEARRAY, fred ;
177EXPECT
178TIEARRAY FAKEARRAY fred
179TIEARRAY FAKEARRAY fred
180DESTROY
181########
182BEGIN { die "phooey\n" }
183EXPECT
184phooey
185BEGIN failed--compilation aborted at - line 1.
186########
187BEGIN { 1/$zero }
188EXPECT
189Illegal division by zero at - line 1.
190BEGIN failed--compilation aborted at - line 1.
191########
192BEGIN { undef = 0 }
193EXPECT
194Modification of a read-only value attempted at - line 1.
195BEGIN failed--compilation aborted at - line 1.
a7adf1f0 196########
197{
198 package foo;
199 sub PRINT {
200 shift;
201 print join(' ', reverse @_)."\n";
202 }
46fc3d4c 203 sub PRINTF {
204 shift;
205 my $fmt = shift;
206 print sprintf($fmt, @_)."\n";
207 }
a7adf1f0 208 sub TIEHANDLE {
209 bless {}, shift;
210 }
58f51617 211 sub READLINE {
212 "Out of inspiration";
213 }
a7adf1f0 214 sub DESTROY {
215 print "and destroyed as well\n";
2ae324a7 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 }
a7adf1f0 227}
228{
229 local(*FOO);
230 tie(*FOO,'foo');
231 print FOO "sentence.", "reversed", "a", "is", "This";
58f51617 232 print "-- ", <FOO>, " --\n";
2ae324a7 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";
46fc3d4c 238 printf "%s is number %d\n", "Perl", 1;
a7adf1f0 239}
240EXPECT
241This is a reversed sentence.
58f51617 242-- Out of inspiration --
2ae324a7 243foo->can(READ)(string 10 1)
244Don't GETC, Get Perl
46fc3d4c 245Perl is number 1
a7adf1f0 246and destroyed as well
a6006777 247########
248my @a; $a[2] = 1; for (@a) { $_ = 2 } print "@a\n"
249EXPECT
2502 2 2
251########
252@a = ($a, $b, $c, $d) = (5, 6);
253print "ok\n"
254 if ($a[0] == 5 and $a[1] == 6 and !defined $a[2] and !defined $a[3]);
255EXPECT
256ok
257########
258print "ok\n" if (1E2<<1 == 200 and 3E4<<3 == 240000);
259EXPECT
260ok
261########
8ebc5c01 262print "ok\n" if ("\0" lt "\xFF");
a6006777 263EXPECT
264ok
265########
266open(H,'op/misc.t'); # must be in the 't' directory
267stat(H);
268print "ok\n" if (-e _ and -f _ and -r _);
269EXPECT
270ok
271########
272sub thing { 0 || return qw(now is the time) }
273print thing(), "\n";
274EXPECT
275nowisthetime
276########
277$ren = 'joy';
278$stimpy = 'happy';
279{ local $main::{ren} = *stimpy; print $ren, ' ' }
280print $ren, "\n";
281EXPECT
282happy joy
283########
284$stimpy = 'happy';
285{ local $main::{ren} = *stimpy; print ${'ren'}, ' ' }
286print +(defined(${'ren'}) ? 'oops' : 'joy'), "\n";
287EXPECT
288happy joy
289########
290package p;
291sub func { print 'really ' unless wantarray; 'p' }
292sub groovy { 'groovy' }
293package main;
294print p::func()->groovy(), "\n"
295EXPECT
296really groovy
297########
d53f8f1c 298@list = ([ 'one', 1 ], [ 'two', 2 ]);
299sub func { $num = shift; (grep $_->[1] == $num, @list)[0] }
300print scalar(map &func($_), 1 .. 3), " ",
301 scalar(map scalar &func($_), 1 .. 3), "\n";
302EXPECT
3032 3
304########
44a8e56a 305($k, $s) = qw(x 0);
306@{$h{$k}} = qw(1 2 4);
307for (@{$h{$k}}) { $s += $_; delete $h{$k} if ($_ == 2) }
308print "bogus\n" unless $s == 7;
309########
310my $a = 'outer';
311eval q[ my $a = 'inner'; eval q[ print "$a " ] ];
312eval { my $x = 'peace'; eval q[ print "$x\n" ] }
313EXPECT
314inner peace
774d564b 315########
316-w
317$| = 1;
318sub foo {
319 print "In foo1\n";
320 eval 'sub foo { print "In foo2\n" }';
321 print "Exiting foo1\n";
322}
323foo;
324foo;
325EXPECT
326In foo1
327Subroutine foo redefined at (eval 1) line 1.
328Exiting foo1
329In foo2
330########
331$s = 0;
332map {#this newline here tickles the bug
333$s += $_} (1,2,4);
334print "eat flaming death\n" unless ($s == 7);
1ca7b98a 335########
336sub foo { local $_ = shift; split; @_ }
337@x = foo(' x y z ');
338print "you die joe!\n" unless "@x" eq 'x y z';
c277df42 339########
340/(?{"{"})/ # Check it outside of eval too
341EXPECT
2cd61cdb 342Sequence (?{...}) not terminated or not {}-balanced at - line 1, within pattern
343/(?{"{"})/: Sequence (?{...}) not terminated or not {}-balanced at - line 1.
c277df42 344########
345/(?{"{"}})/ # Check it outside of eval too
346EXPECT
347Unmatched right bracket at (re_eval 1) line 1, at end of line
348syntax error at (re_eval 1) line 1, near ""{"}"
2cd61cdb 349Compilation failed in regexp at - line 1.
0da4822f 350########
351BEGIN { @ARGV = qw(a b c) }
352BEGIN { print "argv <@ARGV>\nbegin <",shift,">\n" }
353END { print "end <",shift,">\nargv <@ARGV>\n" }
354INIT { print "init <",shift,">\n" }
355EXPECT
356argv <a b c>
357begin <a>
358init <b>
359end <c>
360argv <>
4599a1de 361########
3500f679 362-l
363# fdopen from a system descriptor to a system descriptor used to close
364# the former.
365open STDERR, '>&=STDOUT' or die $!;
366select STDOUT; $| = 1; print fileno STDOUT;
367select STDERR; $| = 1; print fileno STDERR;
368EXPECT
3691
3702
371########
20408e3c 372-w
373sub testme { my $a = "test"; { local $a = "new test"; print $a }}
374EXPECT
375Can't localize lexical variable $a at - line 2.
376########
51ae5c03 377package X;
378sub ascalar { my $r; bless \$r }
379sub DESTROY { print "destroyed\n" };
380package main;
381*s = ascalar X;
382EXPECT
383destroyed
384########
385package X;
386sub anarray { bless [] }
387sub DESTROY { print "destroyed\n" };
388package main;
389*a = anarray X;
390EXPECT
391destroyed
392########
393package X;
394sub ahash { bless {} }
395sub DESTROY { print "destroyed\n" };
396package main;
397*h = ahash X;
398EXPECT
399destroyed
400########
401package X;
402sub aclosure { my $x; bless sub { ++$x } }
403sub DESTROY { print "destroyed\n" };
404package main;
405*c = aclosure X;
406EXPECT
407destroyed
408########
409package X;
410sub any { bless {} }
411my $f = "FH000"; # just to thwart any future optimisations
412sub afh { select select ++$f; my $r = *{$f}{IO}; delete $X::{$f}; bless $r }
413sub DESTROY { print "destroyed\n" }
414package main;
415$x = any X; # to bump sv_objcount. IO objs aren't counted??
416*f = afh X;
417EXPECT
418destroyed
419destroyed
420########