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