9 use strict qw(refs subs);
13 # Test glob operations.
42 # Test fake references.
50 # Test real references.
57 # test that ref(vstring) makes sense
59 is (ref($vstref), "VSTRING", "ref(vstr) eq VSTRING");
60 like ( $vstref, qr/VSTRING\(0x[0-9a-f]+\)/, '\vstr is also VSTRING');
62 # Test references to real arrays.
64 my $test = curr_test();
65 @ary = ($test,$test+1,$test+2,$test+3);
71 push(@{$ref[$i]}, "ok $ary[$i]\n");
82 # Test references to references.
86 is ($$$refref, 'Good');
88 # Test nested anonymous lists.
90 $ref = [[],2,[3,4,5,]];
93 is (${$$ref[2]}[2], 5);
94 is (scalar @{$$ref[0]}, 0);
97 is ($ref->[2]->[0], 3);
99 # Test references to hashes of references.
101 $refref = \%whatever;
102 $refref->{"key"} = $ref;
103 is ($refref->{"key"}->[2]->[0], 3);
105 # Test to see if anonymous subarrays spring into existence.
107 $spring[5]->[0] = 123;
108 $spring[5]->[1] = 456;
109 push(@{$spring[5]}, 789);
110 is (join(':',@{$spring[5]}), "123:456:789");
112 # Test to see if anonymous subhashes spring into existence.
114 @{$spring2{"foo"}} = (1,2,3);
115 $spring2{"foo"}->[3] = 4;
116 is (join(':',@{$spring2{"foo"}}), "1:2:3:4");
118 # Test references to subroutines.
122 sub mysub { $called++; }
128 $subrefref = \\&mysub2;
129 is ($$subrefref->("GOOD"), "good");
130 sub mysub2 { lc shift }
132 # Test the ref operator.
134 is (ref $subref, 'CODE');
135 is (ref $ref, 'ARRAY');
136 is (ref $refref, 'HASH');
138 # Test anonymous hash syntax.
141 is (ref $anonhash, 'HASH');
142 $anonhash2 = {FOO => 'BAR', ABC => 'XYZ',};
143 is (join('', sort values %$anonhash2), 'BARXYZ');
145 # Test bless operator.
149 $object = bless $main'anonhash2;
150 main::is (ref $object, 'MYHASH');
151 main::is ($object->{ABC}, 'XYZ');
154 main::is (ref $object2, 'MYHASH');
156 # Test ordinary call on object method.
158 &mymethod($object,"argument");
161 local($THIS, @ARGS) = @_;
162 die 'Got a "' . ref($THIS). '" instead of a MYHASH'
163 unless ref $THIS eq 'MYHASH';
164 main::is ($ARGS[0], "argument");
165 main::is ($THIS->{FOO}, 'BAR');
168 # Test automatic destructor call.
173 $main'anonhash2 = "foo";
177 return unless $string;
178 main::is ($string, 'good');
180 # Test that the object has not already been "cursed".
181 main::isnt (ref shift, 'HASH');
184 # Now test inheritance of methods.
190 $main'object = bless {FOO => 'foo', BAR => 'bar'};
194 # Test arrow-style method invocation.
196 is ($object->doit("BAR"), 'bar');
198 # Test indirect-object-style method invocation.
200 $foo = doit $object "FOO";
201 main::is ($foo, 'foo');
205 die "Not an OBJ" unless ref $ref eq 'OBJ';
213 sub foo { main::is ($_[1], 'works') }
216 foo WHATEVER "works";
219 # test the \(@foo) construct
224 @baz = \(1,@foo,@bar);
225 is (scalar (@bar), 3);
226 is (scalar grep(ref($_), @bar), 3);
227 is (scalar (@baz), 3);
229 my(@fuu) = \(1..2,3);
231 my(@bzz) = \(1,@fuu,@baa);
232 is (scalar (@baa), 3);
233 is (scalar grep(ref($_), @baa), 3);
234 is (scalar (@bzz), 3);
236 # also, it can't be an lvalue
237 eval '\\($x, $y) = (1, 2);';
238 like ($@, qr/Can\'t modify.*ref.*in.*assignment/);
240 # test for proper destruction of lexical objects
242 sub larry::DESTROY { print "# larry\nok $test\n"; }
243 sub curly::DESTROY { print "# curly\nok ", $test + 1, "\n"; }
244 sub moe::DESTROY { print "# moe\nok ", $test + 2, "\n"; }
247 my ($joe, @curly, %larry);
248 my $moe = bless \$joe, 'moe';
249 my $curly = bless \@curly, 'curly';
250 my $larry = bless \%larry, 'larry';
251 print "# leaving block\n";
254 print "# left block\n";
255 curr_test($test + 3);
261 { local(*bar) = "foo" }
271 # test if reblessing during destruction results in more destruction
275 sub new { bless {}, shift }
276 DESTROY { print "# destroying 'A'\nok ", $test + 1, "\n" }
278 sub new { bless {}, shift }
279 DESTROY { print "# destroying '_B'\nok $test\n"; bless shift, 'A' }
283 curr_test($test + 2);
285 # test if $_[0] is properly protected in DESTROY()
288 my $test = curr_test();
290 local $SIG{'__DIE__'} = sub {
293 print "# infinite recursion, bailing\nnot ok $test\n";
296 like ($m, qr/^Modification of a read-only/);
299 sub new { bless {}, shift }
300 DESTROY { $_[0] = 'foo' }
302 print "# should generate an error...\n";
305 print "# good, didn't recurse\n";
308 # test if refgen behaves with autoviv magic
320 # This test is the reason for postponed destruction in sv_unref
325 # This test used to coredump. The BEGIN block is important as it causes the
326 # op that created the constant reference to be freed. Hence the only
327 # reference to the constant string "pass" is in $a. The hack that made
328 # sure $a = $a->[1] would work didn't work with references to constants.
331 foreach my $lexical ('', 'my $a; ') {
332 my $expect = "pass\n";
333 my $result = runperl (switches => ['-wl'], stderr => 1,
334 prog => $lexical . 'BEGIN {$a = \q{pass}}; $a = $$a; print $a');
337 is ($result, $expect);
341 sub x::DESTROY {print "ok ", $test + shift->[0], "\n"}
342 { my $a1 = bless [3],"x";
343 my $a2 = bless [2],"x";
344 { my $a3 = bless [1],"x";
345 my $a4 = bless [0],"x";
351 is (runperl (switches=>['-l'],
352 prog=> 'print 1; print qq-*$\*-;print 1;'),
357 runperl(prog => 'sub UNIVERSAL::AUTOLOAD { qr// } a->p' );
358 is ($?, 0, 'UNIVERSAL::AUTOLOAD called when freeing qr//');
360 runperl(prog => 'sub UNIVERSAL::DESTROY { warn } bless \$a, A', stderr => 1);
361 is ($?, 0, 'warn called inside UNIVERSAL::DESTROY');
366 runperl(prog => 'sub f { my $x = shift; *z = $x; } f({}); f();');
367 is ($?, 0, 'coredump on typeglob = (SvRV && !SvROK)');
369 # bug #27268: freeing self-referential typeglobs could trigger
370 # "Attempt to free unreferenced scalar" warnings
373 prog => 'use Symbol;my $x=bless \gensym,"t"; print;*$$x=$x',
375 ), '', 'freeing self-referential typeglob');
377 # using a regex in the destructor for STDOUT segfaulted because the
378 # REGEX pad had already been freed (ithreads build only). The
379 # object is required to trigger the early freeing of GV refs to to STDOUT
382 prog => '$x=bless[]; sub IO::Handle::DESTROY{$_="bad";s/bad/ok/;print}',
384 ), qr/^(ok)+$/, 'STDOUT destructor');
389 $name_utf8 = $name8 . chr 256;
392 is ($$name8, undef, 'Nothing before we start');
393 is ($$name_utf8, undef, 'Nothing before we start');
395 is ($$name8, "Pound", 'Accessing via 8 bit symref works');
396 local $TODO = "UTF8 mangled in symrefs";
397 is ($$name_utf8, "Pound", 'Accessing via UTF8 symref works');
402 $name_utf8 = $name = chr 9787;
403 utf8::encode $name_utf8;
405 is (length $name, 1, "Name is 1 char");
406 is (length $name_utf8, 3, "UTF8 representation is 3 chars");
408 is ($$name, undef, 'Nothing before we start');
409 is ($$name_utf8, undef, 'Nothing before we start');
411 is ($$name, "Face", 'Accessing via Unicode symref works');
412 local $TODO = "UTF8 mangled in symrefs";
413 is ($$name_utf8, undef,
414 'Accessing via the UTF8 byte sequence gives nothing');
422 isnt ($name1, $name2, "They differ");
424 is ($$name1, undef, 'Nothing before we start (scalars)');
425 is ($$name2, undef, 'Nothing before we start');
427 is ($$name1, "Yummy", 'Accessing via the correct name works');
429 'Accessing via a different NUL-containing name gives nothing');
430 # defined uses a different code path
431 ok (defined $$name1, 'defined via the correct name works');
432 ok (!defined $$name2,
433 'defined via a different NUL-containing name gives nothing');
435 is ($name1->[0], undef, 'Nothing before we start (arrays)');
436 is ($name2->[0], undef, 'Nothing before we start');
437 $name1->[0] = "Yummy";
438 is ($name1->[0], "Yummy", 'Accessing via the correct name works');
439 is ($name2->[0], undef,
440 'Accessing via a different NUL-containing name gives nothing');
441 ok (defined $name1->[0], 'defined via the correct name works');
442 ok (!defined$name2->[0],
443 'defined via a different NUL-containing name gives nothing');
445 my (undef, $one) = @{$name1}[2,3];
446 my (undef, $two) = @{$name2}[2,3];
447 is ($one, undef, 'Nothing before we start (array slices)');
448 is ($two, undef, 'Nothing before we start');
449 @{$name1}[2,3] = ("Very", "Yummy");
450 (undef, $one) = @{$name1}[2,3];
451 (undef, $two) = @{$name2}[2,3];
452 is ($one, "Yummy", 'Accessing via the correct name works');
454 'Accessing via a different NUL-containing name gives nothing');
455 ok (defined $one, 'defined via the correct name works');
457 'defined via a different NUL-containing name gives nothing');
459 is ($name1->{PWOF}, undef, 'Nothing before we start (hashes)');
460 is ($name2->{PWOF}, undef, 'Nothing before we start');
461 $name1->{PWOF} = "Yummy";
462 is ($name1->{PWOF}, "Yummy", 'Accessing via the correct name works');
463 is ($name2->{PWOF}, undef,
464 'Accessing via a different NUL-containing name gives nothing');
465 ok (defined $name1->{PWOF}, 'defined via the correct name works');
466 ok (!defined $name2->{PWOF},
467 'defined via a different NUL-containing name gives nothing');
469 my (undef, $one) = @{$name1}{'SNIF', 'BEEYOOP'};
470 my (undef, $two) = @{$name2}{'SNIF', 'BEEYOOP'};
471 is ($one, undef, 'Nothing before we start (hash slices)');
472 is ($two, undef, 'Nothing before we start');
473 @{$name1}{'SNIF', 'BEEYOOP'} = ("Very", "Yummy");
474 (undef, $one) = @{$name1}{'SNIF', 'BEEYOOP'};
475 (undef, $two) = @{$name2}{'SNIF', 'BEEYOOP'};
476 is ($one, "Yummy", 'Accessing via the correct name works');
478 'Accessing via a different NUL-containing name gives nothing');
479 ok (defined $one, 'defined via the correct name works');
481 'defined via a different NUL-containing name gives nothing');
483 $name1 = "Left"; $name2 = "Left\0Right";
484 my $glob2 = *{$name2};
486 is ($glob1, undef, "We get different typeglobs. In fact, undef");
488 *{$name1} = sub {"One"};
489 *{$name2} = sub {"Two"};
491 is (&{$name1}, "One");
492 is (&{$name2}, "Two");
495 # test derefs after list slice
497 is ( ({foo => "bar"})[0]{foo}, "bar", 'hash deref from list slice w/o ->' );
498 is ( ({foo => "bar"})[0]->{foo}, "bar", 'hash deref from list slice w/ ->' );
499 is ( ([qw/foo bar/])[0][1], "bar", 'array deref from list slice w/o ->' );
500 is ( ([qw/foo bar/])[0]->[1], "bar", 'array deref from list slice w/ ->' );
501 is ( (sub {"bar"})[0](), "bar", 'code deref from list slice w/o ->' );
502 is ( (sub {"bar"})[0]->(), "bar", 'code deref from list slice w/ ->' );
504 # deref on empty list shouldn't autovivify
508 like ( "$@", "Can't use an undefined value as a HASH reference",
509 "deref of undef from list slice fails" );
512 # test dereferencing errors
517 foreach $ref (*STDOUT{IO}, *STDERR{FORMAT}) {
519 like($@, qr/Not a SCALAR reference/, "Scalar dereference");
521 like($@, qr/Not an ARRAY reference/, "Array dereference");
523 like($@, qr/Not a HASH reference/, "Hash dereference");
525 like($@, qr/Not a CODE reference/, "Code dereference");
528 $ref = *STDERR{FORMAT};
530 like($@, qr/Not a GLOB reference/, "Glob dereference");
534 is($@, '', "Glob dereference of PVIO is acceptable");
536 is($ref, *{$ref}{IO}, "IO slot of the temporary glob is set correctly");
539 # Bit of a hack to make test.pl happy. There are 3 more tests after it leaves.
541 curr_test($test + 3);
542 # test global destruction
544 my $test1 = $test + 1;
545 my $test2 = $test + 2;
550 $ref3 = bless ["ok $test2\n"]; # package destruction
551 my $ref2 = bless ["ok $test1\n"]; # lexical destruction
552 local $ref1 = bless ["ok $test\n"]; # dynamic destruction
553 1; # flush any temp values on stack