This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
foreach defelem magic should only be applied to PL_sv_undef
[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';
20822f61 7@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;
ed6b3797 18END { while($tmpfile && unlink $tmpfile){} }
a0d0e21e 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 28 open TEST, ">$tmpfile" or die "Cannot open $tmpfile: $!";
ed6b3797
CL
29 $prog =~ s#/dev/null#NL:# if $^O eq 'VMS';
30 $prog =~ s#if \(-e _ and -f _ and -r _\)#if (-e _ and -f _)# if $^O eq 'VMS'; # VMS file locking
31
648cac19
IZ
32 print TEST $prog, "\n";
33 close TEST or die "Cannot close $tmpfile: $!";
34
68dc0745 35 if ($^O eq 'MSWin32') {
648cac19 36 $results = `.\\perl -I../lib $switch $tmpfile 2>&1`;
68dc0745
PP
37 }
38 else {
648cac19 39 $results = `./perl $switch $tmpfile 2>&1`;
68dc0745 40 }
a0d0e21e 41 $status = $?;
a0d0e21e 42 $results =~ s/\n+$//;
648cac19
IZ
43 $results =~ s/at\s+misctmp\d+\s+line/at - line/g;
44 $results =~ s/of\s+misctmp\d+\s+aborted/of - aborted/g;
f0ec1f9a 45# bison says 'parse error' instead of 'syntax error',
d91e2bdb 46# various yaccs may or may not capitalize 'syntax'.
2a8ee232 47 $results =~ s/^(syntax|parse) error/syntax error/mig;
a0d0e21e 48 $expected =~ s/\n+$//;
8feb4e9f 49 if ( $results ne $expected ) {
a0d0e21e
LW
50 print STDERR "PROG: $switch\n$prog\n";
51 print STDERR "EXPECTED:\n$expected\n";
52 print STDERR "GOT:\n$results\n";
53 print "not ";
54 }
55 print "ok ", ++$i, "\n";
56}
57
58__END__
2ace3117
CS
59()=()
60########
44a8e56a
PP
61$a = ":="; split /($a)/o, "a:=b:=c"; print "@_"
62EXPECT
63a := b := c
64########
36477c24 65$cusp = ~0 ^ (~0 >> 1);
85e0ebd8 66use integer;
36477c24 67$, = " ";
85e0ebd8 68print +($cusp - 1) % 8, $cusp % 8, -$cusp % 8, 8 | (($cusp + 1) % 8 + 7), "!\n";
36477c24 69EXPECT
85e0ebd8 707 0 0 8 !
36477c24 71########
a0d0e21e
LW
72$foo=undef; $foo->go;
73EXPECT
72b5445b 74Can't call method "go" on an undefined value at - line 1.
a0d0e21e
LW
75########
76BEGIN
77 {
78 "foo";
79 }
80########
a0d0e21e
LW
81$array[128]=1
82########
83$x=0x0eabcd; print $x->ref;
84EXPECT
85Can't call method "ref" without a package or object reference at - line 1.
86########
648cac19 87chop ($str .= <DATA>);
a0d0e21e
LW
88########
89close ($banana);
90########
91$x=2;$y=3;$x<$y ? $x : $y += 23;print $x;
92EXPECT
9325
94########
95eval {sub bar {print "In bar";}}
96########
68dc0745 97system './perl -ne "print if eof" /dev/null'
a0d0e21e 98########
648cac19 99chop($file = <DATA>);
a0d0e21e
LW
100########
101package N;
102sub new {my ($obj,$n)=@_; bless \$n}
103$aa=new N 1;
104$aa=12345;
105print $aa;
106EXPECT
10712345
108########
109%@x=0;
110EXPECT
f1612b5c 111Can't modify hash dereference in repeat (x) at - line 1, near "0;"
3fe9a6f1 112Execution of - aborted due to compilation errors.
a0d0e21e
LW
113########
114$_="foo";
115printf(STDOUT "%s\n", $_);
116EXPECT
117foo
118########
119push(@a, 1, 2, 3,)
120########
121quotemeta ""
122########
123for ("ABCDE") {
124 &sub;
125s/./&sub($&)/eg;
126print;}
127sub sub {local($_) = @_;
128$_ x 4;}
129EXPECT
130Modification of a read-only value attempted at - line 3.
131########
132package FOO;sub new {bless {FOO => BAR}};
133package main;
134use strict vars;
135my $self = new FOO;
136print $$self{FOO};
137EXPECT
138BAR
139########
140$_="foo";
141s/.{1}//s;
142print;
143EXPECT
144oo
145########
146print scalar ("foo","bar")
147EXPECT
148bar
149########
150sub by_number { $a <=> $b; };# inline function for sort below
151$as_ary{0}="a0";
152@ordered_array=sort by_number keys(%as_ary);
153########
154sub NewShell
155{
156 local($Host) = @_;
157 my($m2) = $#Shells++;
158 $Shells[$m2]{HOST} = $Host;
159 return $m2;
160}
161
162sub ShowShell
163{
164 local($i) = @_;
165}
166
167&ShowShell(&NewShell(beach,Work,"+0+0"));
168&ShowShell(&NewShell(beach,Work,"+0+0"));
169&ShowShell(&NewShell(beach,Work,"+0+0"));
170########
171 {
172 package FAKEARRAY;
173
174 sub TIEARRAY
175 { print "TIEARRAY @_\n";
176 die "bomb out\n" unless $count ++ ;
177 bless ['foo']
178 }
179 sub FETCH { print "fetch @_\n"; $_[0]->[$_[1]] }
180 sub STORE { print "store @_\n"; $_[0]->[$_[1]] = $_[2] }
181 sub DESTROY { print "DESTROY \n"; undef @{$_[0]}; }
182 }
183
184eval 'tie @h, FAKEARRAY, fred' ;
185tie @h, FAKEARRAY, fred ;
186EXPECT
187TIEARRAY FAKEARRAY fred
188TIEARRAY FAKEARRAY fred
189DESTROY
190########
191BEGIN { die "phooey\n" }
192EXPECT
193phooey
194BEGIN failed--compilation aborted at - line 1.
195########
196BEGIN { 1/$zero }
197EXPECT
198Illegal division by zero at - line 1.
199BEGIN failed--compilation aborted at - line 1.
200########
201BEGIN { undef = 0 }
202EXPECT
203Modification of a read-only value attempted at - line 1.
204BEGIN failed--compilation aborted at - line 1.
a7adf1f0
PP
205########
206{
207 package foo;
208 sub PRINT {
209 shift;
210 print join(' ', reverse @_)."\n";
211 }
46fc3d4c
PP
212 sub PRINTF {
213 shift;
214 my $fmt = shift;
215 print sprintf($fmt, @_)."\n";
216 }
a7adf1f0
PP
217 sub TIEHANDLE {
218 bless {}, shift;
219 }
58f51617
SV
220 sub READLINE {
221 "Out of inspiration";
222 }
a7adf1f0
PP
223 sub DESTROY {
224 print "and destroyed as well\n";
2ae324a7
PP
225 }
226 sub READ {
227 shift;
228 print STDOUT "foo->can(READ)(@_)\n";
229 return 100;
230 }
231 sub GETC {
232 shift;
233 print STDOUT "Don't GETC, Get Perl\n";
234 return "a";
235 }
a7adf1f0
PP
236}
237{
238 local(*FOO);
239 tie(*FOO,'foo');
240 print FOO "sentence.", "reversed", "a", "is", "This";
58f51617 241 print "-- ", <FOO>, " --\n";
2ae324a7
PP
242 my($buf,$len,$offset);
243 $buf = "string";
244 $len = 10; $offset = 1;
245 read(FOO, $buf, $len, $offset) == 100 or die "foo->READ failed";
246 getc(FOO) eq "a" or die "foo->GETC failed";
46fc3d4c 247 printf "%s is number %d\n", "Perl", 1;
a7adf1f0
PP
248}
249EXPECT
250This is a reversed sentence.
58f51617 251-- Out of inspiration --
2ae324a7
PP
252foo->can(READ)(string 10 1)
253Don't GETC, Get Perl
46fc3d4c 254Perl is number 1
a7adf1f0 255and destroyed as well
a6006777
PP
256########
257my @a; $a[2] = 1; for (@a) { $_ = 2 } print "@a\n"
258EXPECT
2592 2 2
260########
8b530633
GA
261# used to attach defelem magic too all immortal values,
262# which made restore of local $_ fail.
263foo(2>1);
264sub foo { bar() for @_; }
265sub bar { local $_; }
266print "ok\n";
267EXPECT
268ok
269########
a6006777
PP
270@a = ($a, $b, $c, $d) = (5, 6);
271print "ok\n"
272 if ($a[0] == 5 and $a[1] == 6 and !defined $a[2] and !defined $a[3]);
273EXPECT
274ok
275########
276print "ok\n" if (1E2<<1 == 200 and 3E4<<3 == 240000);
277EXPECT
278ok
279########
8ebc5c01 280print "ok\n" if ("\0" lt "\xFF");
a6006777
PP
281EXPECT
282ok
283########
284open(H,'op/misc.t'); # must be in the 't' directory
285stat(H);
286print "ok\n" if (-e _ and -f _ and -r _);
287EXPECT
288ok
289########
290sub thing { 0 || return qw(now is the time) }
291print thing(), "\n";
292EXPECT
293nowisthetime
294########
295$ren = 'joy';
296$stimpy = 'happy';
297{ local $main::{ren} = *stimpy; print $ren, ' ' }
298print $ren, "\n";
299EXPECT
300happy joy
301########
302$stimpy = 'happy';
303{ local $main::{ren} = *stimpy; print ${'ren'}, ' ' }
304print +(defined(${'ren'}) ? 'oops' : 'joy'), "\n";
305EXPECT
306happy joy
307########
308package p;
309sub func { print 'really ' unless wantarray; 'p' }
310sub groovy { 'groovy' }
311package main;
312print p::func()->groovy(), "\n"
313EXPECT
314really groovy
315########
d53f8f1c
HS
316@list = ([ 'one', 1 ], [ 'two', 2 ]);
317sub func { $num = shift; (grep $_->[1] == $num, @list)[0] }
318print scalar(map &func($_), 1 .. 3), " ",
319 scalar(map scalar &func($_), 1 .. 3), "\n";
320EXPECT
3212 3
322########
44a8e56a
PP
323($k, $s) = qw(x 0);
324@{$h{$k}} = qw(1 2 4);
325for (@{$h{$k}}) { $s += $_; delete $h{$k} if ($_ == 2) }
326print "bogus\n" unless $s == 7;
327########
328my $a = 'outer';
329eval q[ my $a = 'inner'; eval q[ print "$a " ] ];
330eval { my $x = 'peace'; eval q[ print "$x\n" ] }
331EXPECT
332inner peace
774d564b
PP
333########
334-w
335$| = 1;
336sub foo {
337 print "In foo1\n";
338 eval 'sub foo { print "In foo2\n" }';
339 print "Exiting foo1\n";
340}
341foo;
342foo;
343EXPECT
344In foo1
345Subroutine foo redefined at (eval 1) line 1.
346Exiting foo1
347In foo2
348########
349$s = 0;
350map {#this newline here tickles the bug
351$s += $_} (1,2,4);
352print "eat flaming death\n" unless ($s == 7);
1ca7b98a
CS
353########
354sub foo { local $_ = shift; split; @_ }
355@x = foo(' x y z ');
356print "you die joe!\n" unless "@x" eq 'x y z';
c277df42
IZ
357########
358/(?{"{"})/ # Check it outside of eval too
359EXPECT
2cd61cdb 360Sequence (?{...}) not terminated or not {}-balanced at - line 1, within pattern
79eeca27 361Sequence (?{...}) not terminated or not {}-balanced before HERE mark in regex m/(?{ << HERE "{"})/ at - line 1.
c277df42
IZ
362########
363/(?{"{"}})/ # Check it outside of eval too
364EXPECT
d98d5fff 365Unmatched right curly bracket at (re_eval 1) line 1, at end of line
c277df42 366syntax error at (re_eval 1) line 1, near ""{"}"
2cd61cdb 367Compilation failed in regexp at - line 1.
0da4822f 368########
ff689196 369BEGIN { @ARGV = qw(a b c d e) }
0da4822f
GS
370BEGIN { print "argv <@ARGV>\nbegin <",shift,">\n" }
371END { print "end <",shift,">\nargv <@ARGV>\n" }
372INIT { print "init <",shift,">\n" }
7d30b5c4 373CHECK { print "check <",shift,">\n" }
0da4822f 374EXPECT
ff689196 375argv <a b c d e>
0da4822f 376begin <a>
7d30b5c4 377check <b>
ff689196
GS
378init <c>
379end <d>
380argv <e>
4599a1de 381########
3500f679
RS
382-l
383# fdopen from a system descriptor to a system descriptor used to close
384# the former.
385open STDERR, '>&=STDOUT' or die $!;
75642110
NC
386select STDOUT; $| = 1; print fileno STDOUT or die $!;
387select STDERR; $| = 1; print fileno STDERR or die $!;
3500f679
RS
388EXPECT
3891
3902
391########
20408e3c
GS
392-w
393sub testme { my $a = "test"; { local $a = "new test"; print $a }}
394EXPECT
395Can't localize lexical variable $a at - line 2.
396########
51ae5c03
JPC
397package X;
398sub ascalar { my $r; bless \$r }
399sub DESTROY { print "destroyed\n" };
400package main;
401*s = ascalar X;
402EXPECT
403destroyed
404########
405package X;
406sub anarray { bless [] }
407sub DESTROY { print "destroyed\n" };
408package main;
409*a = anarray X;
410EXPECT
411destroyed
412########
413package X;
414sub ahash { bless {} }
415sub DESTROY { print "destroyed\n" };
416package main;
417*h = ahash X;
418EXPECT
419destroyed
420########
421package X;
422sub aclosure { my $x; bless sub { ++$x } }
423sub DESTROY { print "destroyed\n" };
424package main;
425*c = aclosure X;
426EXPECT
427destroyed
428########
429package X;
430sub any { bless {} }
431my $f = "FH000"; # just to thwart any future optimisations
ded8aa31 432sub afh { select select ++$f; my $r = *{$f}{IO}; delete $X::{$f}; bless $r }
51ae5c03
JPC
433sub DESTROY { print "destroyed\n" }
434package main;
435$x = any X; # to bump sv_objcount. IO objs aren't counted??
436*f = afh X;
437EXPECT
438destroyed
439destroyed
440########
ebf99b04
GS
441BEGIN {
442 $| = 1;
443 $SIG{__WARN__} = sub {
444 eval { print $_[0] };
445 die "bar\n";
446 };
447 warn "foo\n";
448}
449EXPECT
450foo
451bar
452BEGIN failed--compilation aborted at - line 8.
8feb4e9f
JH
453########
454package X;
455@ISA='Y';
456sub new {
457 my $class = shift;
458 my $self = { };
459 bless $self, $class;
460 my $init = shift;
461 $self->foo($init);
462 print "new", $init;
463 return $self;
464}
465sub DESTROY {
466 my $self = shift;
467 print "DESTROY", $self->foo;
468}
469package Y;
470sub attribute {
471 my $self = shift;
472 my $var = shift;
473 if (@_ == 0) {
474 return $self->{$var};
475 } elsif (@_ == 1) {
476 $self->{$var} = shift;
477 }
478}
479sub AUTOLOAD {
480 $AUTOLOAD =~ /::([^:]+)$/;
481 my $method = $1;
482 splice @_, 1, 0, $method;
483 goto &attribute;
484}
485package main;
486my $x = X->new(1);
487for (2..3) {
488 my $y = X->new($_);
489 print $y->foo;
490}
491print $x->foo;
492EXPECT
493new1new22DESTROY2new33DESTROY31DESTROY1
dfad63ad
HS
494########
495re();
496sub re {
14455d6c 497 my $re = join '', eval 'qr/(??{ $obj->method })/';
dfad63ad
HS
498 $re;
499}
500EXPECT
1aff0e91
GS
501########
502use strict;
503my $foo = "ZZZ\n";
504END { print $foo }
505EXPECT
506ZZZ
507########
508eval '
509use strict;
510my $foo = "ZZZ\n";
511END { print $foo }
512';
513EXPECT
514ZZZ
7399586d
HS
515########
516-w
517if (@ARGV) { print "" }
518else {
519 if ($x == 0) { print "" } else { print $x }
520}
521EXPECT
b89fed5f 522Use of uninitialized value in numeric eq (==) at - line 4.
1d76a5c3
GS
523########
524$x = sub {};
525foo();
526sub foo { eval { return }; }
527print "ok\n";
528EXPECT
529ok
07447971
GS
530########
531my @l = qw(hello.* world);
532my $x;
533
534foreach $x (@l) {
535 print "before - $x\n";
536 $x = "\Q$x\E";
537 print "quotemeta - $x\n";
538 $x = "\u$x";
539 print "ucfirst - $x\n";
540 $x = "\l$x";
541 print "lcfirst - $x\n";
542 $x = "\U$x\E";
543 print "uc - $x\n";
544 $x = "\L$x\E";
545 print "lc - $x\n";
546}
547EXPECT
548before - hello.*
549quotemeta - hello\.\*
550ucfirst - Hello\.\*
551lcfirst - hello\.\*
552uc - HELLO\.\*
553lc - hello\.\*
554before - world
555quotemeta - world
556ucfirst - World
557lcfirst - world
558uc - WORLD
559lc - world
92d29cee
JH
560########
561sub f { my $a = 1; my $b = 2; my $c = 3; my $d = 4; next }
562my $x = "foo";
563{ f } continue { print $x, "\n" }
564EXPECT
565foo
6a7129a1
GS
566########
567sub C () { 1 }
568sub M { $_[0] = 2; }
569eval "C";
570M(C);
571EXPECT
572Modification of a read-only value attempted at - line 2.
00c29ff8
TH
573########
574print qw(ab a\b a\\b);
575EXPECT
576aba\ba\b
dd8482fc
JH
577########
578# This test is here instead of pragma/locale.t because
579# the bug depends on in the internal state of the locale
580# settings and pragma/locale messes up that state pretty badly.
581# We need a "fresh run".
c9f931b8
JH
582BEGIN {
583 eval { require POSIX };
584 if ($@) {
585 exit(0); # running minitest?
586 }
587}
dd8482fc
JH
588use Config;
589my $have_setlocale = $Config{d_setlocale} eq 'define';
dd8482fc
JH
590$have_setlocale = 0 if $@;
591# Visual C's CRT goes silly on strings of the form "en_US.ISO8859-1"
592# and mingw32 uses said silly CRT
593$have_setlocale = 0 if $^O eq 'MSWin32' && $Config{cc} =~ /^(cl|gcc)/i;
594exit(0) unless $have_setlocale;
595my @locales;
596if (-x "/usr/bin/locale" && open(LOCALES, "/usr/bin/locale -a|")) {
597 while(<LOCALES>) {
598 chomp;
599 push(@locales, $_);
600 }
601 close(LOCALES);
602}
603exit(0) unless @locales;
604for (@locales) {
605 use POSIX qw(locale_h);
606 use locale;
88e7acd2 607 setlocale(LC_NUMERIC, $_) or next;
dd8482fc
JH
608 my $s = sprintf "%g %g", 3.1, 3.1;
609 next if $s eq '3.1 3.1' || $s =~ /^(3.+1) \1$/;
610 print "$_ $s\n";
611}
612EXPECT
b927783e
JH
613########
614die qr(x)
615EXPECT
616(?-xism:x) at - line 1.
617########
7ef822cd
JH
618# 20001210.003 mjd@plover.com
619format REMITOUT_TOP =
620FOO
621.
622
623format REMITOUT =
624BAR
625.
626
627# This loop causes a segv in 5.6.0
628for $lineno (1..61) {
629 write REMITOUT;
630}
631
632print "It's OK!";
633EXPECT
634It's OK!
cb55de95
JH
635########
636# Inaba Hiroto
637reset;
638if (0) {
639 if ("" =~ //) {
640 }
641}
642########
643# Nicholas Clark
644$ENV{TERM} = 0;
645reset;
646// if 0;
647########
648# Vadim Konovalov
649use strict;
650sub new_pmop($) {
651 my $pm = shift;
652 return eval "sub {shift=~/$pm/}";
653}
654new_pmop "abcdef"; reset;
655new_pmop "abcdef"; reset;
656new_pmop "abcdef"; reset;
657new_pmop "abcdef"; reset;
658