7 # Test glob operations.
34 # Test fake references.
41 # Test real references.
48 # Test references to real arrays.
56 push(@{$ref[$i]}, "ok $ary[$i]\n");
63 # Test references to references.
69 # Test nested anonymous lists.
71 $ref = [[],2,[3,4,5,]];
72 print scalar @$ref == 3 ? "ok 14\n" : "not ok 14\n";
73 print $$ref[1] == 2 ? "ok 15\n" : "not ok 15\n";
74 print ${$$ref[2]}[2] == 5 ? "ok 16\n" : "not ok 16\n";
75 print scalar @{$$ref[0]} == 0 ? "ok 17\n" : "not ok 17\n";
77 print $ref->[1] == 2 ? "ok 18\n" : "not ok 18\n";
78 print $ref->[2]->[0] == 3 ? "ok 19\n" : "not ok 19\n";
80 # Test references to hashes of references.
83 $refref->{"key"} = $ref;
84 print $refref->{"key"}->[2]->[0] == 3 ? "ok 20\n" : "not ok 20\n";
86 # Test to see if anonymous subarrays spring into existence.
88 $spring[5]->[0] = 123;
89 $spring[5]->[1] = 456;
90 push(@{$spring[5]}, 789);
91 print join(':',@{$spring[5]}) eq "123:456:789" ? "ok 21\n" : "not ok 21\n";
93 # Test to see if anonymous subhashes spring into existence.
95 @{$spring2{"foo"}} = (1,2,3);
96 $spring2{"foo"}->[3] = 4;
97 print join(':',@{$spring2{"foo"}}) eq "1:2:3:4" ? "ok 22\n" : "not ok 22\n";
99 # Test references to subroutines.
101 sub mysub { print "ok 23\n" }
105 $subrefref = \\&mysub2;
106 $$subrefref->("ok 24\n");
107 sub mysub2 { print shift }
109 # Test the ref operator.
111 print ref $subref eq CODE ? "ok 25\n" : "not ok 25\n";
112 print ref $ref eq ARRAY ? "ok 26\n" : "not ok 26\n";
113 print ref $refref eq HASH ? "ok 27\n" : "not ok 27\n";
115 # Test anonymous hash syntax.
118 print ref $anonhash eq HASH ? "ok 28\n" : "not ok 28\n";
119 $anonhash2 = {FOO => BAR, ABC => XYZ,};
120 print join('', sort values %$anonhash2) eq BARXYZ ? "ok 29\n" : "not ok 29\n";
122 # Test bless operator.
126 $object = bless $main'anonhash2;
127 print ref $object eq MYHASH ? "ok 30\n" : "not ok 30\n";
128 print $object->{ABC} eq XYZ ? "ok 31\n" : "not ok 31\n";
131 print ref $object2 eq MYHASH ? "ok 32\n" : "not ok 32\n";
133 # Test ordinary call on object method.
135 &mymethod($object,33);
138 local($THIS, @ARGS) = @_;
139 die 'Got a "' . ref($THIS). '" instead of a MYHASH'
140 unless ref $THIS eq MYHASH;
141 print $THIS->{FOO} eq BAR ? "ok $ARGS[0]\n" : "not ok $ARGS[0]\n";
144 # Test automatic destructor call.
146 $string = "not ok 34\n";
149 $main'anonhash2 = "foo";
153 return unless $string;
156 # Test that the object has not already been "cursed".
157 print ref shift ne HASH ? "ok 35\n" : "not ok 35\n";
160 # Now test inheritance of methods.
166 $main'object = bless {FOO => foo, BAR => bar};
170 # Test arrow-style method invocation.
172 print $object->doit("BAR") eq bar ? "ok 36\n" : "not ok 36\n";
174 # Test indirect-object-style method invocation.
176 $foo = doit $object "FOO";
177 print $foo eq foo ? "ok 37\n" : "not ok 37\n";
181 die "Not an OBJ" unless ref $ref eq OBJ;
189 sub foo { print $_[1] }
192 foo WHATEVER "ok 38\n";
195 # test the \(@foo) construct
200 @baz = \(1,@foo,@bar);
201 print @bar == 3 ? "ok 39\n" : "not ok 39\n";
202 print grep(ref($_), @bar) == 3 ? "ok 40\n" : "not ok 40\n";
203 print @baz == 3 ? "ok 41\n" : "not ok 41\n";
207 my(@bzz) = \(1,@fuu,@baa);
208 print @baa == 3 ? "ok 42\n" : "not ok 42\n";
209 print grep(ref($_), @baa) == 3 ? "ok 43\n" : "not ok 43\n";
210 print @bzz == 3 ? "ok 44\n" : "not ok 44\n";
212 # test for proper destruction of lexical objects
214 sub larry::DESTROY { print "# larry\nok 45\n"; }
215 sub curly::DESTROY { print "# curly\nok 46\n"; }
216 sub moe::DESTROY { print "# moe\nok 47\n"; }
219 my ($joe, @curly, %larry);
220 my $moe = bless \$joe, 'moe';
221 my $curly = bless \@curly, 'curly';
222 my $larry = bless \%larry, 'larry';
223 print "# leaving block\n";
226 print "# left block\n";
231 { local(*bar) = "foo" }
240 # test if reblessing during destruction results in more destruction
244 sub new { bless {}, shift }
245 DESTROY { print "# destroying 'A'\nok 51\n" }
247 sub new { bless {}, shift }
248 DESTROY { print "# destroying '_B'\nok 50\n"; bless shift, 'A' }
253 # test if $_[0] is properly protected in DESTROY()
257 local $SIG{'__DIE__'} = sub {
260 print "# infinite recursion, bailing\nnot ok 52\n";
264 if ($m =~ /^Modification of a read-only/) { print "ok 52\n" }
267 sub new { bless {}, shift }
268 DESTROY { $_[0] = 'foo' }
270 print "# should generate an error...\n";
273 print "# good, didn't recurse\n";
276 # test if refgen behaves with autoviv magic
284 # This test is the reason for postponed destruction in sv_unref
287 print "not " unless $a == 2;
290 sub x::DESTROY {print "ok ", 54 + shift->[0], "\n"}
291 { my $a1 = bless [4],"x";
292 my $a2 = bless [3],"x";
293 { my $a3 = bless [2],"x";
294 my $a4 = bless [1],"x";
300 my $result = runperl (switches=>['-l'],
301 prog=> 'print 1; print qq-*$\*-;print 1;');
302 my $expect = "1\n*\n*\n1\n";
303 if ($result eq $expect) {
307 foreach ($expect, $result) {
310 print "# expected \"$expect\", got \"$result\"\n";
313 # test global destruction
316 my $test1 = $test + 1;
317 my $test2 = $test + 2;
322 $ref3 = bless ["ok $test2\n"]; # package destruction
323 my $ref2 = bless ["ok $test1\n"]; # lexical destruction
324 local $ref1 = bless ["ok $test\n"]; # dynamic destruction
325 1; # flush any temp values on stack