This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[dummy merge]
[perl5.git] / t / op / misc.t
CommitLineData
a0d0e21e
LW
1#!./perl
2
3chdir 't' if -d 't';
4@INC = "../lib";
5$ENV{PERL5LIB} = "../lib";
6
7$|=1;
8
9undef $/;
10@prgs = split "\n########\n", <DATA>;
11print "1..", scalar @prgs, "\n";
12
13$tmpfile = "misctmp000";
141 while -f ++$tmpfile;
15END { unlink $tmpfile if $tmpfile; }
16
17for (@prgs){
18 my $switch;
19 if (s/^\s*-\w+//){
20 $switch = $&;
21 }
22 my($prog,$expected) = split(/\nEXPECT\n/, $_);
23 open TEST, "| sh -c './perl $switch' >$tmpfile 2>&1";
24 print TEST $prog, "\n";
25 close TEST;
26 $status = $?;
27 $results = `cat $tmpfile`;
28 $results =~ s/\n+$//;
29 $expected =~ s/\n+$//;
30 if ( $results ne $expected){
31 print STDERR "PROG: $switch\n$prog\n";
32 print STDERR "EXPECTED:\n$expected\n";
33 print STDERR "GOT:\n$results\n";
34 print "not ";
35 }
36 print "ok ", ++$i, "\n";
37}
38
39__END__
2ace3117
CS
40()=()
41########
44a8e56a
PP
42$a = ":="; split /($a)/o, "a:=b:=c"; print "@_"
43EXPECT
44a := b := c
45########
36477c24
PP
46$cusp = ~0 ^ (~0 >> 1);
47$, = " ";
48print +($cusp - 1) % 8, $cusp % 8, -$cusp % 8, ($cusp + 1) % 8, "!\n";
49EXPECT
507 0 0 1 !
51########
a0d0e21e
LW
52$foo=undef; $foo->go;
53EXPECT
54Can't call method "go" without a package or object reference at - line 1.
55########
56BEGIN
57 {
58 "foo";
59 }
60########
a0d0e21e
LW
61$array[128]=1
62########
63$x=0x0eabcd; print $x->ref;
64EXPECT
65Can't call method "ref" without a package or object reference at - line 1.
66########
67chop ($str .= <STDIN>);
68########
69close ($banana);
70########
71$x=2;$y=3;$x<$y ? $x : $y += 23;print $x;
72EXPECT
7325
74########
75eval {sub bar {print "In bar";}}
76########
77system "./perl -ne 'print if eof' /dev/null"
78########
79chop($file = <>);
80########
81package N;
82sub new {my ($obj,$n)=@_; bless \$n}
83$aa=new N 1;
84$aa=12345;
85print $aa;
86EXPECT
8712345
88########
89%@x=0;
90EXPECT
91Can't coerce HASH to string in repeat at - line 1.
92########
93$_="foo";
94printf(STDOUT "%s\n", $_);
95EXPECT
96foo
97########
98push(@a, 1, 2, 3,)
99########
100quotemeta ""
101########
102for ("ABCDE") {
103 &sub;
104s/./&sub($&)/eg;
105print;}
106sub sub {local($_) = @_;
107$_ x 4;}
108EXPECT
109Modification of a read-only value attempted at - line 3.
110########
111package FOO;sub new {bless {FOO => BAR}};
112package main;
113use strict vars;
114my $self = new FOO;
115print $$self{FOO};
116EXPECT
117BAR
118########
119$_="foo";
120s/.{1}//s;
121print;
122EXPECT
123oo
124########
125print scalar ("foo","bar")
126EXPECT
127bar
128########
129sub by_number { $a <=> $b; };# inline function for sort below
130$as_ary{0}="a0";
131@ordered_array=sort by_number keys(%as_ary);
132########
133sub NewShell
134{
135 local($Host) = @_;
136 my($m2) = $#Shells++;
137 $Shells[$m2]{HOST} = $Host;
138 return $m2;
139}
140
141sub ShowShell
142{
143 local($i) = @_;
144}
145
146&ShowShell(&NewShell(beach,Work,"+0+0"));
147&ShowShell(&NewShell(beach,Work,"+0+0"));
148&ShowShell(&NewShell(beach,Work,"+0+0"));
149########
150 {
151 package FAKEARRAY;
152
153 sub TIEARRAY
154 { print "TIEARRAY @_\n";
155 die "bomb out\n" unless $count ++ ;
156 bless ['foo']
157 }
158 sub FETCH { print "fetch @_\n"; $_[0]->[$_[1]] }
159 sub STORE { print "store @_\n"; $_[0]->[$_[1]] = $_[2] }
160 sub DESTROY { print "DESTROY \n"; undef @{$_[0]}; }
161 }
162
163eval 'tie @h, FAKEARRAY, fred' ;
164tie @h, FAKEARRAY, fred ;
165EXPECT
166TIEARRAY FAKEARRAY fred
167TIEARRAY FAKEARRAY fred
168DESTROY
169########
170BEGIN { die "phooey\n" }
171EXPECT
172phooey
173BEGIN failed--compilation aborted at - line 1.
174########
175BEGIN { 1/$zero }
176EXPECT
177Illegal division by zero at - line 1.
178BEGIN failed--compilation aborted at - line 1.
179########
180BEGIN { undef = 0 }
181EXPECT
182Modification of a read-only value attempted at - line 1.
183BEGIN failed--compilation aborted at - line 1.
a7adf1f0
PP
184########
185{
186 package foo;
187 sub PRINT {
188 shift;
189 print join(' ', reverse @_)."\n";
190 }
191 sub TIEHANDLE {
192 bless {}, shift;
193 }
58f51617
SV
194 sub READLINE {
195 "Out of inspiration";
196 }
a7adf1f0
PP
197 sub DESTROY {
198 print "and destroyed as well\n";
199 }
200}
201{
202 local(*FOO);
203 tie(*FOO,'foo');
204 print FOO "sentence.", "reversed", "a", "is", "This";
58f51617 205 print "-- ", <FOO>, " --\n";
a7adf1f0
PP
206}
207EXPECT
208This is a reversed sentence.
58f51617 209-- Out of inspiration --
a7adf1f0 210and destroyed as well
a6006777
PP
211########
212my @a; $a[2] = 1; for (@a) { $_ = 2 } print "@a\n"
213EXPECT
2142 2 2
215########
216@a = ($a, $b, $c, $d) = (5, 6);
217print "ok\n"
218 if ($a[0] == 5 and $a[1] == 6 and !defined $a[2] and !defined $a[3]);
219EXPECT
220ok
221########
222print "ok\n" if (1E2<<1 == 200 and 3E4<<3 == 240000);
223EXPECT
224ok
225########
8ebc5c01 226print "ok\n" if ("\0" lt "\xFF");
a6006777
PP
227EXPECT
228ok
229########
230open(H,'op/misc.t'); # must be in the 't' directory
231stat(H);
232print "ok\n" if (-e _ and -f _ and -r _);
233EXPECT
234ok
235########
236sub thing { 0 || return qw(now is the time) }
237print thing(), "\n";
238EXPECT
239nowisthetime
240########
241$ren = 'joy';
242$stimpy = 'happy';
243{ local $main::{ren} = *stimpy; print $ren, ' ' }
244print $ren, "\n";
245EXPECT
246happy joy
247########
248$stimpy = 'happy';
249{ local $main::{ren} = *stimpy; print ${'ren'}, ' ' }
250print +(defined(${'ren'}) ? 'oops' : 'joy'), "\n";
251EXPECT
252happy joy
253########
254package p;
255sub func { print 'really ' unless wantarray; 'p' }
256sub groovy { 'groovy' }
257package main;
258print p::func()->groovy(), "\n"
259EXPECT
260really groovy
261########
d53f8f1c
HS
262@list = ([ 'one', 1 ], [ 'two', 2 ]);
263sub func { $num = shift; (grep $_->[1] == $num, @list)[0] }
264print scalar(map &func($_), 1 .. 3), " ",
265 scalar(map scalar &func($_), 1 .. 3), "\n";
266EXPECT
2672 3
268########
44a8e56a
PP
269($k, $s) = qw(x 0);
270@{$h{$k}} = qw(1 2 4);
271for (@{$h{$k}}) { $s += $_; delete $h{$k} if ($_ == 2) }
272print "bogus\n" unless $s == 7;
273########
274my $a = 'outer';
275eval q[ my $a = 'inner'; eval q[ print "$a " ] ];
276eval { my $x = 'peace'; eval q[ print "$x\n" ] }
277EXPECT
278inner peace
774d564b
PP
279########
280-w
281$| = 1;
282sub foo {
283 print "In foo1\n";
284 eval 'sub foo { print "In foo2\n" }';
285 print "Exiting foo1\n";
286}
287foo;
288foo;
289EXPECT
290In foo1
291Subroutine foo redefined at (eval 1) line 1.
292Exiting foo1
293In foo2
294########
295$s = 0;
296map {#this newline here tickles the bug
297$s += $_} (1,2,4);
298print "eat flaming death\n" unless ($s == 7);
1ca7b98a
CS
299########
300sub foo { local $_ = shift; split; @_ }
301@x = foo(' x y z ');
302print "you die joe!\n" unless "@x" eq 'x y z';
f86702cc
PP
303########
304sub foo { local(@_) = ('p', 'q', 'r'); }
305sub bar { unshift @_, 'D'; @_ }
306sub baz { push @_, 'E'; return @_ }
307for (1..3) { print foo('a', 'b', 'c'), bar('d'), baz('e'), "\n" }
308EXPECT
309pqrDdeE
310pqrDdeE
311pqrDdeE