[inseparable changes from match from perl-5.003_92 to perl-5.003_93]
[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
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 40()=()
41########
44a8e56a 42$a = ":="; split /($a)/o, "a:=b:=c"; print "@_"
43EXPECT
44a := b := c
45########
36477c24 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 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 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 184########
185{
186 package foo;
187 sub PRINT {
188 shift;
189 print join(' ', reverse @_)."\n";
190 }
191 sub TIEHANDLE {
192 bless {}, shift;
193 }
58f51617 194 sub READLINE {
195 "Out of inspiration";
196 }
a7adf1f0 197 sub DESTROY {
198 print "and destroyed as well\n";
2ae324a7 199 }
200 sub READ {
201 shift;
202 print STDOUT "foo->can(READ)(@_)\n";
203 return 100;
204 }
205 sub GETC {
206 shift;
207 print STDOUT "Don't GETC, Get Perl\n";
208 return "a";
209 }
a7adf1f0 210}
211{
212 local(*FOO);
213 tie(*FOO,'foo');
214 print FOO "sentence.", "reversed", "a", "is", "This";
58f51617 215 print "-- ", <FOO>, " --\n";
2ae324a7 216 my($buf,$len,$offset);
217 $buf = "string";
218 $len = 10; $offset = 1;
219 read(FOO, $buf, $len, $offset) == 100 or die "foo->READ failed";
220 getc(FOO) eq "a" or die "foo->GETC failed";
a7adf1f0 221}
222EXPECT
223This is a reversed sentence.
58f51617 224-- Out of inspiration --
2ae324a7 225foo->can(READ)(string 10 1)
226Don't GETC, Get Perl
a7adf1f0 227and destroyed as well
a6006777 228########
229my @a; $a[2] = 1; for (@a) { $_ = 2 } print "@a\n"
230EXPECT
2312 2 2
232########
233@a = ($a, $b, $c, $d) = (5, 6);
234print "ok\n"
235 if ($a[0] == 5 and $a[1] == 6 and !defined $a[2] and !defined $a[3]);
236EXPECT
237ok
238########
239print "ok\n" if (1E2<<1 == 200 and 3E4<<3 == 240000);
240EXPECT
241ok
242########
8ebc5c01 243print "ok\n" if ("\0" lt "\xFF");
a6006777 244EXPECT
245ok
246########
247open(H,'op/misc.t'); # must be in the 't' directory
248stat(H);
249print "ok\n" if (-e _ and -f _ and -r _);
250EXPECT
251ok
252########
253sub thing { 0 || return qw(now is the time) }
254print thing(), "\n";
255EXPECT
256nowisthetime
257########
258$ren = 'joy';
259$stimpy = 'happy';
260{ local $main::{ren} = *stimpy; print $ren, ' ' }
261print $ren, "\n";
262EXPECT
263happy joy
264########
265$stimpy = 'happy';
266{ local $main::{ren} = *stimpy; print ${'ren'}, ' ' }
267print +(defined(${'ren'}) ? 'oops' : 'joy'), "\n";
268EXPECT
269happy joy
270########
271package p;
272sub func { print 'really ' unless wantarray; 'p' }
273sub groovy { 'groovy' }
274package main;
275print p::func()->groovy(), "\n"
276EXPECT
277really groovy
278########
d53f8f1c 279@list = ([ 'one', 1 ], [ 'two', 2 ]);
280sub func { $num = shift; (grep $_->[1] == $num, @list)[0] }
281print scalar(map &func($_), 1 .. 3), " ",
282 scalar(map scalar &func($_), 1 .. 3), "\n";
283EXPECT
2842 3
285########
44a8e56a 286($k, $s) = qw(x 0);
287@{$h{$k}} = qw(1 2 4);
288for (@{$h{$k}}) { $s += $_; delete $h{$k} if ($_ == 2) }
289print "bogus\n" unless $s == 7;
290########
291my $a = 'outer';
292eval q[ my $a = 'inner'; eval q[ print "$a " ] ];
293eval { my $x = 'peace'; eval q[ print "$x\n" ] }
294EXPECT
295inner peace
774d564b 296########
297-w
298$| = 1;
299sub foo {
300 print "In foo1\n";
301 eval 'sub foo { print "In foo2\n" }';
302 print "Exiting foo1\n";
303}
304foo;
305foo;
306EXPECT
307In foo1
308Subroutine foo redefined at (eval 1) line 1.
309Exiting foo1
310In foo2
311########
312$s = 0;
313map {#this newline here tickles the bug
314$s += $_} (1,2,4);
315print "eat flaming death\n" unless ($s == 7);
1ca7b98a 316########
317sub foo { local $_ = shift; split; @_ }
318@x = foo(' x y z ');
319print "you die joe!\n" unless "@x" eq 'x y z';
f86702cc 320########
321sub foo { local(@_) = ('p', 'q', 'r'); }
322sub bar { unshift @_, 'D'; @_ }
323sub baz { push @_, 'E'; return @_ }
324for (1..3) { print foo('a', 'b', 'c'), bar('d'), baz('e'), "\n" }
325EXPECT
326pqrDdeE
327pqrDdeE
328pqrDdeE