12 # Test glob operations.
39 # Test fake references.
46 # Test real references.
53 # Test references to real arrays.
61 push(@{$ref[$i]}, "ok $ary[$i]\n");
68 # Test references to references.
74 # Test nested anonymous lists.
76 $ref = [[],2,[3,4,5,]];
77 print scalar @$ref == 3 ? "ok 14\n" : "not ok 14\n";
78 print $$ref[1] == 2 ? "ok 15\n" : "not ok 15\n";
79 print ${$$ref[2]}[2] == 5 ? "ok 16\n" : "not ok 16\n";
80 print scalar @{$$ref[0]} == 0 ? "ok 17\n" : "not ok 17\n";
82 print $ref->[1] == 2 ? "ok 18\n" : "not ok 18\n";
83 print $ref->[2]->[0] == 3 ? "ok 19\n" : "not ok 19\n";
85 # Test references to hashes of references.
88 $refref->{"key"} = $ref;
89 print $refref->{"key"}->[2]->[0] == 3 ? "ok 20\n" : "not ok 20\n";
91 # Test to see if anonymous subarrays spring into existence.
93 $spring[5]->[0] = 123;
94 $spring[5]->[1] = 456;
95 push(@{$spring[5]}, 789);
96 print join(':',@{$spring[5]}) eq "123:456:789" ? "ok 21\n" : "not ok 21\n";
98 # Test to see if anonymous subhashes spring into existence.
100 @{$spring2{"foo"}} = (1,2,3);
101 $spring2{"foo"}->[3] = 4;
102 print join(':',@{$spring2{"foo"}}) eq "1:2:3:4" ? "ok 22\n" : "not ok 22\n";
104 # Test references to subroutines.
106 sub mysub { print "ok 23\n" }
110 $subrefref = \\&mysub2;
111 $$subrefref->("ok 24\n");
112 sub mysub2 { print shift }
114 # Test the ref operator.
116 print ref $subref eq CODE ? "ok 25\n" : "not ok 25\n";
117 print ref $ref eq ARRAY ? "ok 26\n" : "not ok 26\n";
118 print ref $refref eq HASH ? "ok 27\n" : "not ok 27\n";
120 # Test anonymous hash syntax.
123 print ref $anonhash eq HASH ? "ok 28\n" : "not ok 28\n";
124 $anonhash2 = {FOO => BAR, ABC => XYZ,};
125 print join('', sort values %$anonhash2) eq BARXYZ ? "ok 29\n" : "not ok 29\n";
127 # Test bless operator.
131 $object = bless $main'anonhash2;
132 print ref $object eq MYHASH ? "ok 30\n" : "not ok 30\n";
133 print $object->{ABC} eq XYZ ? "ok 31\n" : "not ok 31\n";
136 print ref $object2 eq MYHASH ? "ok 32\n" : "not ok 32\n";
138 # Test ordinary call on object method.
140 &mymethod($object,33);
143 local($THIS, @ARGS) = @_;
144 die 'Got a "' . ref($THIS). '" instead of a MYHASH'
145 unless ref $THIS eq MYHASH;
146 print $THIS->{FOO} eq BAR ? "ok $ARGS[0]\n" : "not ok $ARGS[0]\n";
149 # Test automatic destructor call.
151 $string = "not ok 34\n";
154 $main'anonhash2 = "foo";
158 return unless $string;
161 # Test that the object has not already been "cursed".
162 print ref shift ne HASH ? "ok 35\n" : "not ok 35\n";
165 # Now test inheritance of methods.
171 $main'object = bless {FOO => foo, BAR => bar};
175 # Test arrow-style method invocation.
177 print $object->doit("BAR") eq bar ? "ok 36\n" : "not ok 36\n";
179 # Test indirect-object-style method invocation.
181 $foo = doit $object "FOO";
182 print $foo eq foo ? "ok 37\n" : "not ok 37\n";
186 die "Not an OBJ" unless ref $ref eq OBJ;
194 sub foo { print $_[1] }
197 foo WHATEVER "ok 38\n";
200 # test the \(@foo) construct
205 @baz = \(1,@foo,@bar);
206 print @bar == 3 ? "ok 39\n" : "not ok 39\n";
207 print grep(ref($_), @bar) == 3 ? "ok 40\n" : "not ok 40\n";
208 print @baz == 3 ? "ok 41\n" : "not ok 41\n";
210 my(@fuu) = \(1..2,3);
212 my(@bzz) = \(1,@fuu,@baa);
213 print @baa == 3 ? "ok 42\n" : "not ok 42\n";
214 print grep(ref($_), @baa) == 3 ? "ok 43\n" : "not ok 43\n";
215 print @bzz == 3 ? "ok 44\n" : "not ok 44\n";
217 # also, it can't be an lvalue
218 eval '\\($x, $y) = (1, 2);';
219 print $@ =~ /Can\'t modify.*ref.*in.*assignment/ ? "ok 45\n" : "not ok 45\n";
221 # test for proper destruction of lexical objects
223 sub larry::DESTROY { print "# larry\nok 46\n"; }
224 sub curly::DESTROY { print "# curly\nok 47\n"; }
225 sub moe::DESTROY { print "# moe\nok 48\n"; }
228 my ($joe, @curly, %larry);
229 my $moe = bless \$joe, 'moe';
230 my $curly = bless \@curly, 'curly';
231 my $larry = bless \%larry, 'larry';
232 print "# leaving block\n";
235 print "# left block\n";
240 { local(*bar) = "foo" }
249 # test if reblessing during destruction results in more destruction
253 sub new { bless {}, shift }
254 DESTROY { print "# destroying 'A'\nok 52\n" }
256 sub new { bless {}, shift }
257 DESTROY { print "# destroying '_B'\nok 51\n"; bless shift, 'A' }
262 # test if $_[0] is properly protected in DESTROY()
266 local $SIG{'__DIE__'} = sub {
269 print "# infinite recursion, bailing\nnot ok 53\n";
273 if ($m =~ /^Modification of a read-only/) { print "ok 53\n" }
276 sub new { bless {}, shift }
277 DESTROY { $_[0] = 'foo' }
279 print "# should generate an error...\n";
282 print "# good, didn't recurse\n";
285 # test if refgen behaves with autoviv magic
293 # This test is the reason for postponed destruction in sv_unref
296 print "not " unless $a == 2;
299 # This test used to coredump. The BEGIN block is important as it causes the
300 # op that created the constant reference to be freed. Hence the only
301 # reference to the constant string "pass" is in $a. The hack that made
302 # sure $a = $a->[1] would work didn't work with references to constants.
306 foreach my $lexical ('', 'my $a; ') {
307 my $expect = "pass\n";
308 my $result = runperl (switches => ['-wl'], stderr => 1,
309 prog => $lexical . 'BEGIN {$a = \q{pass}}; $a = $$a; print $a');
311 if ($? == 0 and $result eq $expect) {
314 print "not ok $test # \$? = $?\n";
315 print "# expected ", _qq ($expect), ", got ", _qq ($result), "\n";
320 sub x::DESTROY {print "ok ", $test + shift->[0], "\n"}
321 { my $a1 = bless [3],"x";
322 my $a2 = bless [2],"x";
323 { my $a3 = bless [1],"x";
324 my $a4 = bless [0],"x";
330 my $result = runperl (switches=>['-l'],
331 prog=> 'print 1; print qq-*$\*-;print 1;');
332 my $expect = "1\n*\n*\n1\n";
333 if ($result eq $expect) {
336 print "not ok $test\n";
337 foreach ($expect, $result) {
340 print "# expected \"$expect\", got \"$result\"\n";
345 runperl(prog => 'sub UNIVERSAL::AUTOLOAD { qr// } a->p' );
346 if ($? != 0) { print "not " };
347 print "ok ",++$test," - UNIVERSAL::AUTOLOAD called when freeing qr//\n";
349 runperl(prog => 'sub UNIVERSAL::DESTROY { warn } bless \$a, A', stderr => 1);
350 if ($? != 0) { print "not " };
351 print "ok ",++$test," - warn called inside UNIVERSAL::DESTROY\n";
356 runperl(prog => 'sub f { my $x = shift; *z = $x; } f({}); f();');
357 if ($? != 0) { print "not " };
358 print "ok ",++$test," - coredump on typeglob = (SvRV && !SvROK)\n";
360 # bug #27268: freeing self-referential typeglobs could trigger
361 # "Attempt to free unreferenced scalar" warnings
364 prog => 'use Symbol;my $x=bless \gensym,"t"; print;*$$x=$x',
367 print "not " if length $result;
368 print "ok ",++$test," - freeing self-referential typeglob\n";
369 print "# got: $result\n" if length $result;
371 # using a regex in the destructor for STDOUT segfaulted because the
372 # REGEX pad had already been freed (ithreads build only). The
373 # object is required to trigger the early freeing of GV refs to to STDOUT
376 prog => '$x=bless[]; sub IO::Handle::DESTROY{$_="bad";s/bad/ok/;print}',
379 print "not " unless $result =~ /^(ok)+$/;
380 print "ok ",++$test," - STDOUT destructor\n";
381 print "# got: $result\n" unless $result =~ /^(ok)+$/;
383 # test global destruction
386 my $test1 = $test + 1;
387 my $test2 = $test + 2;
392 $ref3 = bless ["ok $test2\n"]; # package destruction
393 my $ref2 = bless ["ok $test1\n"]; # lexical destruction
394 local $ref1 = bless ["ok $test\n"]; # dynamic destruction
395 1; # flush any temp values on stack