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