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