9 use strict qw(refs subs);
13 # Test glob operations.
42 # Test fake references.
50 # Test real references.
57 # Test references to real arrays.
59 my $test = curr_test();
60 @ary = ($test,$test+1,$test+2,$test+3);
66 push(@{$ref[$i]}, "ok $ary[$i]\n");
77 # Test references to references.
81 is ($$$refref, 'Good');
83 # Test nested anonymous lists.
85 $ref = [[],2,[3,4,5,]];
88 is (${$$ref[2]}[2], 5);
89 is (scalar @{$$ref[0]}, 0);
92 is ($ref->[2]->[0], 3);
94 # Test references to hashes of references.
97 $refref->{"key"} = $ref;
98 is ($refref->{"key"}->[2]->[0], 3);
100 # Test to see if anonymous subarrays spring into existence.
102 $spring[5]->[0] = 123;
103 $spring[5]->[1] = 456;
104 push(@{$spring[5]}, 789);
105 is (join(':',@{$spring[5]}), "123:456:789");
107 # Test to see if anonymous subhashes spring into existence.
109 @{$spring2{"foo"}} = (1,2,3);
110 $spring2{"foo"}->[3] = 4;
111 is (join(':',@{$spring2{"foo"}}), "1:2:3:4");
113 # Test references to subroutines.
117 sub mysub { $called++; }
123 $subrefref = \\&mysub2;
124 is ($$subrefref->("GOOD"), "good");
125 sub mysub2 { lc shift }
127 # Test the ref operator.
129 is (ref $subref, 'CODE');
130 is (ref $ref, 'ARRAY');
131 is (ref $refref, 'HASH');
133 # Test anonymous hash syntax.
136 is (ref $anonhash, 'HASH');
137 $anonhash2 = {FOO => 'BAR', ABC => 'XYZ',};
138 is (join('', sort values %$anonhash2), 'BARXYZ');
140 # Test bless operator.
144 $object = bless $main'anonhash2;
145 main::is (ref $object, 'MYHASH');
146 main::is ($object->{ABC}, 'XYZ');
149 main::is (ref $object2, 'MYHASH');
151 # Test ordinary call on object method.
153 &mymethod($object,"argument");
156 local($THIS, @ARGS) = @_;
157 die 'Got a "' . ref($THIS). '" instead of a MYHASH'
158 unless ref $THIS eq 'MYHASH';
159 main::is ($ARGS[0], "argument");
160 main::is ($THIS->{FOO}, 'BAR');
163 # Test automatic destructor call.
168 $main'anonhash2 = "foo";
172 return unless $string;
173 main::is ($string, 'good');
175 # Test that the object has not already been "cursed".
176 main::isnt (ref shift, 'HASH');
179 # Now test inheritance of methods.
185 $main'object = bless {FOO => 'foo', BAR => 'bar'};
189 # Test arrow-style method invocation.
191 is ($object->doit("BAR"), 'bar');
193 # Test indirect-object-style method invocation.
195 $foo = doit $object "FOO";
196 main::is ($foo, 'foo');
200 die "Not an OBJ" unless ref $ref eq 'OBJ';
208 sub foo { main::is ($_[1], 'works') }
211 foo WHATEVER "works";
214 # test the \(@foo) construct
219 @baz = \(1,@foo,@bar);
220 is (scalar (@bar), 3);
221 is (scalar grep(ref($_), @bar), 3);
222 is (scalar (@baz), 3);
224 my(@fuu) = \(1..2,3);
226 my(@bzz) = \(1,@fuu,@baa);
227 is (scalar (@baa), 3);
228 is (scalar grep(ref($_), @baa), 3);
229 is (scalar (@bzz), 3);
231 # also, it can't be an lvalue
232 eval '\\($x, $y) = (1, 2);';
233 like ($@, qr/Can\'t modify.*ref.*in.*assignment/);
235 # test for proper destruction of lexical objects
237 sub larry::DESTROY { print "# larry\nok $test\n"; }
238 sub curly::DESTROY { print "# curly\nok ", $test + 1, "\n"; }
239 sub moe::DESTROY { print "# moe\nok ", $test + 2, "\n"; }
242 my ($joe, @curly, %larry);
243 my $moe = bless \$joe, 'moe';
244 my $curly = bless \@curly, 'curly';
245 my $larry = bless \%larry, 'larry';
246 print "# leaving block\n";
249 print "# left block\n";
250 curr_test($test + 3);
256 { local(*bar) = "foo" }
266 # test if reblessing during destruction results in more destruction
270 sub new { bless {}, shift }
271 DESTROY { print "# destroying 'A'\nok ", $test + 1, "\n" }
273 sub new { bless {}, shift }
274 DESTROY { print "# destroying '_B'\nok $test\n"; bless shift, 'A' }
278 curr_test($test + 2);
280 # test if $_[0] is properly protected in DESTROY()
283 my $test = curr_test();
285 local $SIG{'__DIE__'} = sub {
288 print "# infinite recursion, bailing\nnot ok $test\n";
291 like ($m, qr/^Modification of a read-only/);
294 sub new { bless {}, shift }
295 DESTROY { $_[0] = 'foo' }
297 print "# should generate an error...\n";
300 print "# good, didn't recurse\n";
303 # test if refgen behaves with autoviv magic
315 # This test is the reason for postponed destruction in sv_unref
320 # This test used to coredump. The BEGIN block is important as it causes the
321 # op that created the constant reference to be freed. Hence the only
322 # reference to the constant string "pass" is in $a. The hack that made
323 # sure $a = $a->[1] would work didn't work with references to constants.
326 foreach my $lexical ('', 'my $a; ') {
327 my $expect = "pass\n";
328 my $result = runperl (switches => ['-wl'], stderr => 1,
329 prog => $lexical . 'BEGIN {$a = \q{pass}}; $a = $$a; print $a');
332 is ($result, $expect);
336 sub x::DESTROY {print "ok ", $test + shift->[0], "\n"}
337 { my $a1 = bless [3],"x";
338 my $a2 = bless [2],"x";
339 { my $a3 = bless [1],"x";
340 my $a4 = bless [0],"x";
346 is (runperl (switches=>['-l'],
347 prog=> 'print 1; print qq-*$\*-;print 1;'),
352 runperl(prog => 'sub UNIVERSAL::AUTOLOAD { qr// } a->p' );
353 is ($?, 0, 'UNIVERSAL::AUTOLOAD called when freeing qr//');
355 runperl(prog => 'sub UNIVERSAL::DESTROY { warn } bless \$a, A', stderr => 1);
356 is ($?, 0, 'warn called inside UNIVERSAL::DESTROY');
361 runperl(prog => 'sub f { my $x = shift; *z = $x; } f({}); f();');
362 is ($?, 0, 'coredump on typeglob = (SvRV && !SvROK)');
364 # bug #27268: freeing self-referential typeglobs could trigger
365 # "Attempt to free unreferenced scalar" warnings
368 prog => 'use Symbol;my $x=bless \gensym,"t"; print;*$$x=$x',
370 ), '', 'freeing self-referential typeglob');
372 # using a regex in the destructor for STDOUT segfaulted because the
373 # REGEX pad had already been freed (ithreads build only). The
374 # object is required to trigger the early freeing of GV refs to to STDOUT
377 prog => '$x=bless[]; sub IO::Handle::DESTROY{$_="bad";s/bad/ok/;print}',
379 ), qr/^(ok)+$/, 'STDOUT destructor');
384 $name_utf8 = $name8 . chr 256;
387 is ($$name8, undef, 'Nothing before we start');
388 is ($$name_utf8, undef, 'Nothing before we start');
390 is ($$name8, "Pound", 'Accessing via 8 bit symref works');
391 local $TODO = "UTF8 mangled in symrefs";
392 is ($$name_utf8, "Pound", 'Accessing via UTF8 symref works');
397 $name_utf8 = $name = chr 9787;
398 utf8::encode $name_utf8;
400 is (length $name, 1, "Name is 1 char");
401 is (length $name_utf8, 3, "UTF8 representation is 3 chars");
403 is ($$name, undef, 'Nothing before we start');
404 is ($$name_utf8, undef, 'Nothing before we start');
406 is ($$name, "Face", 'Accessing via Unicode symref works');
407 local $TODO = "UTF8 mangled in symrefs";
408 is ($$name_utf8, undef,
409 'Accessing via the UTF8 byte sequence gives nothing');
417 isnt ($name1, $name2, "They differ");
419 is ($$name1, undef, 'Nothing before we start');
420 is ($$name2, undef, 'Nothing before we start');
422 is ($$name1, "Yummy", 'Accessing via the correct name works');
423 local $TODO = "NUL bytes truncate in symrefs";
425 'Accessing via a different NUL-containing name gives nothing');
428 # test derefs after list slice
430 is ( ({foo => "bar"})[0]{foo}, "bar", 'hash deref from list slice w/o ->' );
431 is ( ({foo => "bar"})[0]->{foo}, "bar", 'hash deref from list slice w/ ->' );
432 is ( ([qw/foo bar/])[0][1], "bar", 'array deref from list slice w/o ->' );
433 is ( ([qw/foo bar/])[0]->[1], "bar", 'array deref from list slice w/ ->' );
434 is ( (sub {"bar"})[0](), "bar", 'code deref from list slice w/o ->' );
435 is ( (sub {"bar"})[0]->(), "bar", 'code deref from list slice w/ ->' );
437 # deref on empty list shouldn't autovivify
441 like ( "$@", "Can't use an undefined value as a HASH reference",
442 "deref of undef from list slice fails" );
445 # Bit of a hack to make test.pl happy. There are 3 more tests after it leaves.
447 curr_test($test + 3);
448 # test global destruction
450 my $test1 = $test + 1;
451 my $test2 = $test + 2;
456 $ref3 = bless ["ok $test2\n"]; # package destruction
457 my $ref2 = bless ["ok $test1\n"]; # lexical destruction
458 local $ref1 = bless ["ok $test\n"]; # dynamic destruction
459 1; # flush any temp values on stack