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