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 # Test references to return values of operators (TARGs/PADTMPs)
129 is join(" ", map $$_, @refs), "a b", 'refgen+PADTMP';
132 $subrefref = \\&mysub2;
133 is ($$subrefref->("GOOD"), "good");
134 sub mysub2 { lc shift }
136 # Test REGEXP assignment
139 skip_if_miniperl("no dynamic loading on miniperl, so can't load re", 5);
142 my $str = "$x"; # regex stringification may change
145 is ($y, $str, "bare REGEXP stringifies correctly");
146 ok (eval { "x" =~ $y }, "bare REGEXP matches correctly");
149 ok (re::is_regexp($z), "new ref to REXEXP passes is_regexp");
150 is ($z, $str, "new ref to REGEXP stringifies correctly");
151 ok (eval { "x" =~ $z }, "new ref to REGEXP matches correctly");
160 is ($x, $str, "REGEXP keeps a ref to its mother_re");
161 ok (eval { "x" =~ $x }, "REGEXP with mother_re still matches");
164 # Test the ref operator.
166 sub PVBM () { 'foo' }
167 { my $dummy = index 'foo', PVBM }
169 my $pviv = 1; "$pviv";
170 my $pvnv = 1.0; "$pvnv";
174 # tied lvalue => SCALAR, as we haven't tested tie yet
175 # BIND, 'cos we can't create them yet
176 # REGEXP, 'cos that requires overload or Scalar::Util
179 [ 'undef', SCALAR => \undef ],
180 [ 'constant IV', SCALAR => \1 ],
181 [ 'constant NV', SCALAR => \1.0 ],
182 [ 'constant PV', SCALAR => \'f' ],
183 [ 'scalar', SCALAR => \$x ],
184 [ 'PVIV', SCALAR => \$pviv ],
185 [ 'PVNV', SCALAR => \$pvnv ],
186 [ 'PVMG', SCALAR => \$0 ],
187 [ 'PVBM', SCALAR => \PVBM ],
188 [ 'scalar @array', SCALAR => \scalar @array ],
189 [ 'scalar %hash', SCALAR => \scalar %hash ],
190 [ 'vstring', VSTRING => \v1 ],
191 [ 'ref', REF => \\1 ],
192 [ 'substr lvalue', LVALUE => \substr($x, 0, 0) ],
193 [ 'pos lvalue', LVALUE => \pos ],
194 [ 'vec lvalue', LVALUE => \vec($x,0,1) ],
195 [ 'named array', ARRAY => \@ary ],
196 [ 'anon array', ARRAY => [ 1 ] ],
197 [ 'named hash', HASH => \%whatever ],
198 [ 'anon hash', HASH => { a => 1 } ],
199 [ 'named sub', CODE => \&mysub, ],
200 [ 'anon sub', CODE => sub { 1; } ],
201 [ 'glob', GLOB => \*foo ],
202 [ 'format', FORMAT => *STDERR{FORMAT} ],
204 my ($desc, $type, $ref) = @$_;
205 is (ref $ref, $type, "ref() for ref to $desc");
206 like ("$ref", qr/^$type\(0x[0-9a-f]+\)$/, "stringify for ref to $desc");
209 is (ref *STDOUT{IO}, 'IO::File', 'IO refs are blessed into IO::File');
210 like (*STDOUT{IO}, qr/^IO::File=IO\(0x[0-9a-f]+\)$/,
211 'stringify for IO refs');
213 { # Test re-use of ref's TARG [perl #101738]
214 my $obj = bless [], '____';
215 my $uniobj = bless [], chr 256;
216 my $get_ref = sub { ref shift };
217 my $dummy = &$get_ref($uniobj);
218 $dummy = &$get_ref($obj);
219 ok exists { ____ => undef }->{$dummy}, 'ref sets UTF8 flag correctly';
222 # Test anonymous hash syntax.
225 is (ref $anonhash, 'HASH');
226 $anonhash2 = {FOO => 'BAR', ABC => 'XYZ',};
227 is (join('', sort values %$anonhash2), 'BARXYZ');
229 # Test bless operator.
233 $object = bless $main'anonhash2;
234 main::is (ref $object, 'MYHASH');
235 main::is ($object->{ABC}, 'XYZ');
238 main::is (ref $object2, 'MYHASH');
240 # Test ordinary call on object method.
242 &mymethod($object,"argument");
245 local($THIS, @ARGS) = @_;
246 die 'Got a "' . ref($THIS). '" instead of a MYHASH'
247 unless ref $THIS eq 'MYHASH';
248 main::is ($ARGS[0], "argument");
249 main::is ($THIS->{FOO}, 'BAR');
252 # Test automatic destructor call.
257 $main'anonhash2 = "foo";
261 return unless $string;
262 main::is ($string, 'good');
264 # Test that the object has not already been "cursed".
265 main::isnt (ref shift, 'HASH');
268 # Now test inheritance of methods.
274 $main'object = bless {FOO => 'foo', BAR => 'bar'};
278 # Test arrow-style method invocation.
280 is ($object->doit("BAR"), 'bar');
282 # Test indirect-object-style method invocation.
284 $foo = doit $object "FOO";
285 main::is ($foo, 'foo');
289 die "Not an OBJ" unless ref $ref eq 'OBJ';
297 sub foo { main::is ($_[1], 'works') }
300 foo WHATEVER "works";
303 # test the \(@foo) construct
308 @baz = \(1,@foo,@bar);
309 is (scalar (@bar), 3);
310 is (scalar grep(ref($_), @bar), 3);
311 is (scalar (@baz), 3);
313 my(@fuu) = \(1..2,3);
315 my(@bzz) = \(1,@fuu,@baa);
316 is (scalar (@baa), 3);
317 is (scalar grep(ref($_), @baa), 3);
318 is (scalar (@bzz), 3);
320 # also, it can't be an lvalue
321 eval '\\($x, $y) = (1, 2);';
322 like ($@, qr/Can\'t modify.*ref.*in.*assignment/);
324 # test for proper destruction of lexical objects
326 sub larry::DESTROY { print "# larry\nok $test\n"; }
327 sub curly::DESTROY { print "# curly\nok ", $test + 1, "\n"; }
328 sub moe::DESTROY { print "# moe\nok ", $test + 2, "\n"; }
331 my ($joe, @curly, %larry);
332 my $moe = bless \$joe, 'moe';
333 my $curly = bless \@curly, 'curly';
334 my $larry = bless \%larry, 'larry';
335 print "# leaving block\n";
338 print "# left block\n";
339 curr_test($test + 3);
345 { local(*bar) = "foo" }
355 # test if reblessing during destruction results in more destruction
359 sub new { bless {}, shift }
360 DESTROY { print "# destroying 'A'\nok ", $test + 1, "\n" }
362 sub new { bless {}, shift }
363 DESTROY { print "# destroying '_B'\nok $test\n"; bless shift, 'A' }
367 curr_test($test + 2);
369 # test if $_[0] is properly protected in DESTROY()
372 my $test = curr_test();
374 local $SIG{'__DIE__'} = sub {
377 print "# infinite recursion, bailing\nnot ok $test\n";
380 like ($m, qr/^Modification of a read-only/);
383 sub new { bless {}, shift }
384 DESTROY { $_[0] = 'foo' }
386 print "# should generate an error...\n";
389 print "# good, didn't recurse\n";
392 # test that DESTROY is called on all objects during global destruction,
393 # even those without hard references [perl #36347]
397 stderr => 1, prog => 'sub DESTROY { print qq-aaa\n- } bless \$a[0]'
399 "aaa\n", 'DESTROY called on array elem'
404 prog => '{ bless \my@x; *a=sub{@x}}sub DESTROY { print qq-aaa\n- }'
407 'DESTROY called on closure variable'
411 # test if refgen behaves with autoviv magic
423 # This test is the reason for postponed destruction in sv_unref
428 # This test used to coredump. The BEGIN block is important as it causes the
429 # op that created the constant reference to be freed. Hence the only
430 # reference to the constant string "pass" is in $a. The hack that made
431 # sure $a = $a->[1] would work didn't work with references to constants.
434 foreach my $lexical ('', 'my $a; ') {
435 my $expect = "pass\n";
436 my $result = runperl (switches => ['-wl'], stderr => 1,
437 prog => $lexical . 'BEGIN {$a = \q{pass}}; $a = $$a; print $a');
440 is ($result, $expect);
444 sub x::DESTROY {print "ok ", $test + shift->[0], "\n"}
445 { my $a1 = bless [3],"x";
446 my $a2 = bless [2],"x";
447 { my $a3 = bless [1],"x";
448 my $a4 = bless [0],"x";
454 is (runperl (switches=>['-l'],
455 prog=> 'print 1; print qq-*$\*-;print 1;'),
460 runperl(prog => 'sub UNIVERSAL::AUTOLOAD { qr// } a->p' );
461 is ($?, 0, 'UNIVERSAL::AUTOLOAD called when freeing qr//');
463 runperl(prog => 'sub UNIVERSAL::DESTROY { warn } bless \$a, A', stderr => 1);
464 is ($?, 0, 'warn called inside UNIVERSAL::DESTROY');
469 runperl(prog => 'sub f { my $x = shift; *z = $x; } f({}); f();');
470 is ($?, 0, 'coredump on typeglob = (SvRV && !SvROK)');
472 # bug #27268: freeing self-referential typeglobs could trigger
473 # "Attempt to free unreferenced scalar" warnings
476 prog => 'use Symbol;my $x=bless \gensym,q{t}; print;*$$x=$x',
478 ), '', 'freeing self-referential typeglob');
480 # using a regex in the destructor for STDOUT segfaulted because the
481 # REGEX pad had already been freed (ithreads build only). The
482 # object is required to trigger the early freeing of GV refs to to STDOUT
485 local $TODO = "works but output through pipe is mangled" if $^O eq 'VMS';
487 prog => '$x=bless[]; sub IO::Handle::DESTROY{$_=q{bad};s/bad/ok/;print}',
489 ), qr/^(ok)+$/, 'STDOUT destructor');
495 $name_utf8 = $name8 . chr 256;
498 is ($$name8, undef, 'Nothing before we start');
499 is ($$name_utf8, undef, 'Nothing before we start');
501 is ($$name8, "Pound", 'Accessing via 8 bit symref works');
502 is ($$name_utf8, "Pound", 'Accessing via UTF8 symref works');
507 $name_utf8 = $name = chr 9787;
508 utf8::encode $name_utf8;
510 is (length $name, 1, "Name is 1 char");
511 is (length $name_utf8, 3, "UTF8 representation is 3 chars");
513 is ($$name, undef, 'Nothing before we start');
514 is ($$name_utf8, undef, 'Nothing before we start');
516 is ($$name, "Face", 'Accessing via Unicode symref works');
517 is ($$name_utf8, undef,
518 'Accessing via the UTF8 byte sequence gives nothing');
526 isnt ($name1, $name2, "They differ");
528 is ($$name1, undef, 'Nothing before we start (scalars)');
529 is ($$name2, undef, 'Nothing before we start');
531 is ($$name1, "Yummy", 'Accessing via the correct name works');
533 'Accessing via a different NUL-containing name gives nothing');
534 # defined uses a different code path
535 ok (defined $$name1, 'defined via the correct name works');
536 ok (!defined $$name2,
537 'defined via a different NUL-containing name gives nothing');
539 is ($name1->[0], undef, 'Nothing before we start (arrays)');
540 is ($name2->[0], undef, 'Nothing before we start');
541 $name1->[0] = "Yummy";
542 is ($name1->[0], "Yummy", 'Accessing via the correct name works');
543 is ($name2->[0], undef,
544 'Accessing via a different NUL-containing name gives nothing');
545 ok (defined $name1->[0], 'defined via the correct name works');
546 ok (!defined$name2->[0],
547 'defined via a different NUL-containing name gives nothing');
549 my (undef, $one) = @{$name1}[2,3];
550 my (undef, $two) = @{$name2}[2,3];
551 is ($one, undef, 'Nothing before we start (array slices)');
552 is ($two, undef, 'Nothing before we start');
553 @{$name1}[2,3] = ("Very", "Yummy");
554 (undef, $one) = @{$name1}[2,3];
555 (undef, $two) = @{$name2}[2,3];
556 is ($one, "Yummy", 'Accessing via the correct name works');
558 'Accessing via a different NUL-containing name gives nothing');
559 ok (defined $one, 'defined via the correct name works');
561 'defined via a different NUL-containing name gives nothing');
563 is ($name1->{PWOF}, undef, 'Nothing before we start (hashes)');
564 is ($name2->{PWOF}, undef, 'Nothing before we start');
565 $name1->{PWOF} = "Yummy";
566 is ($name1->{PWOF}, "Yummy", 'Accessing via the correct name works');
567 is ($name2->{PWOF}, undef,
568 'Accessing via a different NUL-containing name gives nothing');
569 ok (defined $name1->{PWOF}, 'defined via the correct name works');
570 ok (!defined $name2->{PWOF},
571 'defined via a different NUL-containing name gives nothing');
573 my (undef, $one) = @{$name1}{'SNIF', 'BEEYOOP'};
574 my (undef, $two) = @{$name2}{'SNIF', 'BEEYOOP'};
575 is ($one, undef, 'Nothing before we start (hash slices)');
576 is ($two, undef, 'Nothing before we start');
577 @{$name1}{'SNIF', 'BEEYOOP'} = ("Very", "Yummy");
578 (undef, $one) = @{$name1}{'SNIF', 'BEEYOOP'};
579 (undef, $two) = @{$name2}{'SNIF', 'BEEYOOP'};
580 is ($one, "Yummy", 'Accessing via the correct name works');
582 'Accessing via a different NUL-containing name gives nothing');
583 ok (defined $one, 'defined via the correct name works');
585 'defined via a different NUL-containing name gives nothing');
587 $name1 = "Left"; $name2 = "Left\0Right";
588 my $glob2 = *{$name2};
590 is ($glob1, undef, "We get different typeglobs. In fact, undef");
592 *{$name1} = sub {"One"};
593 *{$name2} = sub {"Two"};
595 is (&{$name1}, "One");
596 is (&{$name2}, "Two");
599 # test derefs after list slice
601 is ( ({foo => "bar"})[0]{foo}, "bar", 'hash deref from list slice w/o ->' );
602 is ( ({foo => "bar"})[0]->{foo}, "bar", 'hash deref from list slice w/ ->' );
603 is ( ([qw/foo bar/])[0][1], "bar", 'array deref from list slice w/o ->' );
604 is ( ([qw/foo bar/])[0]->[1], "bar", 'array deref from list slice w/ ->' );
605 is ( (sub {"bar"})[0](), "bar", 'code deref from list slice w/o ->' );
606 is ( (sub {"bar"})[0]->(), "bar", 'code deref from list slice w/ ->' );
608 # deref on empty list shouldn't autovivify
612 like ( "$@", "Can't use an undefined value as a HASH reference",
613 "deref of undef from list slice fails" );
616 # test dereferencing errors
621 foreach $ref (*STDOUT{IO}, *STDERR{FORMAT}) {
623 like($@, qr/Not a SCALAR reference/, "Scalar dereference");
625 like($@, qr/Not an ARRAY reference/, "Array dereference");
627 like($@, qr/Not a HASH reference/, "Hash dereference");
629 like($@, qr/Not a CODE reference/, "Code dereference");
632 $ref = *STDERR{FORMAT};
634 like($@, qr/Not a GLOB reference/, "Glob dereference");
638 is($@, '', "Glob dereference of PVIO is acceptable");
640 is($ref, *{$ref}{IO}, "IO slot of the temporary glob is set correctly");
643 # these will segfault if they fail
648 ok (!eval { *$rpvbm }, 'PVBM ref is not a GLOB ref');
649 ok (!eval { *$pvbm }, 'PVBM is not a GLOB ref');
650 ok (!eval { $$pvbm }, 'PVBM is not a SCALAR ref');
651 ok (!eval { @$pvbm }, 'PVBM is not an ARRAY ref');
652 ok (!eval { %$pvbm }, 'PVBM is not a HASH ref');
653 ok (!eval { $pvbm->() }, 'PVBM is not a CODE ref');
654 ok (!eval { $rpvbm->foo }, 'PVBM is not an object');
657 is( runperl(stderr => 1, prog => 'map eval qq(exit),1 for 1'), "");
658 is( runperl(stderr => 1, prog => 'eval { for (1) { map { die } 2 } };'), "");
659 is( runperl(stderr => 1, prog => 'for (125) { map { exit } (213)}'), "");
660 my $hushed = $^O eq 'VMS' ? 'use vmsish qw(hushed);' : '';
661 is( runperl(stderr => 1, prog => $hushed . 'map die,4 for 3'), "Died at -e line 1.\n");
662 is( runperl(stderr => 1, prog => $hushed . 'grep die,4 for 3'), "Died at -e line 1.\n");
663 is( runperl(stderr => 1, prog => $hushed . 'for $a (3) {@b=sort {die} 4,5}'), "Died at -e line 1.\n");
666 is( runperl(stderr => 1, prog => 'my $i;for $i (1) { for $i (2) { } }'), "");
668 # The mechanism for freeing objects in globs used to leave dangling
669 # pointers to freed SVs. To test this, we construct this nested structure:
670 # GV => blessed(AV) => RV => GV => blessed(SV)
671 # all with a refcnt of 1, and hope that the second GV gets processed first
672 # by do_clean_named_objs. Then when the first GV is processed, it mustn't
673 # find anything nasty left by the previous GV processing.
674 # The eval is stop things in the main body of the code holding a reference
675 # to a GV, and the print at the end seems to bee necessary to ensure
676 # the correct freeing order of *x and *y (no, I don't know why - DAPM).
679 prog => 'eval q[bless \@y; bless \$x; $y[0] = \*x; $z = \*y; ]; '
680 . 'delete $::{x}; delete $::{y}; print qq{ok\n};',
682 "ok\n", 'freeing freed glob in global destruction');
685 # Test undefined hash references as arguments to %{} in boolean context
689 eval { my $foo; %$foo; }; ok !$@, '%$undef';
690 eval { my $foo; scalar %$foo; }; ok !$@, 'scalar %$undef';
691 eval { my $foo; !%$foo; }; ok !$@, '!%$undef';
692 eval { my $foo; if ( %$foo) {} }; ok !$@, 'if ( %$undef) {}';
693 eval { my $foo; if (!%$foo) {} }; ok !$@, 'if (!%$undef) {}';
694 eval { my $foo; unless ( %$foo) {} }; ok !$@, 'unless ( %$undef) {}';
695 eval { my $foo; unless (!%$foo) {} }; ok !$@, 'unless (!%$undef) {}';
696 eval { my $foo; 1 if %$foo; }; ok !$@, '1 if %$undef';
697 eval { my $foo; 1 if !%$foo; }; ok !$@, '1 if !%$undef';
698 eval { my $foo; 1 unless %$foo; }; ok !$@, '1 unless %$undef;';
699 eval { my $foo; 1 unless ! %$foo; }; ok !$@, '1 unless ! %$undef';
700 eval { my $foo; %$foo ? 1 : 0; }; ok !$@, ' %$undef ? 1 : 0';
701 eval { my $foo; !%$foo ? 1 : 0; }; ok !$@, '!%$undef ? 1 : 0';
705 # Make sure that a leaked thinggy with multiple weak references to
706 # it doesn't trigger a panic with multiple rounds of global cleanup
707 # (Perl_sv_clean_all).
710 skip_if_miniperl('no Scalar::Util under miniperl', 4);
712 local $ENV{PERL_DESTRUCT_LEVEL} = 2;
714 # we do all permutations of array/hash, 1ref/2ref, to account
715 # for the different way backref magic is stored
717 fresh_perl_is(<<'EOF', 'ok', { stderr => 1 }, 'array with 1 weak ref');
718 use Scalar::Util qw(weaken);
720 Internals::SvREFCNT(@$r, 9);
726 fresh_perl_is(<<'EOF', 'ok', { stderr => 1 }, 'array with 2 weak refs');
727 use Scalar::Util qw(weaken);
729 Internals::SvREFCNT(@$r, 9);
737 fresh_perl_is(<<'EOF', 'ok', { stderr => 1 }, 'hash with 1 weak ref');
738 use Scalar::Util qw(weaken);
740 Internals::SvREFCNT(%$r, 9);
746 fresh_perl_is(<<'EOF', 'ok', { stderr => 1 }, 'hash with 2 weak refs');
747 use Scalar::Util qw(weaken);
749 Internals::SvREFCNT(%$r, 9);
760 skip_if_miniperl "no Scalar::Util on miniperl", 1;
762 *hassgropper::DESTROY = sub {
763 require Scalar::Util;
764 eval { Scalar::Util::weaken($_[0]) };
766 # This line caused a crash before weaken refused to weaken a
767 # read-only reference:
768 $do::not::overwrite::this = $_[0];
770 my $xs = bless [], "hassgropper";
772 like $error, qr/^Modification of a read-only/,
773 'weaken refuses to weaken a read-only ref';
774 # Now that the test has passed, avoid sabotaging global destruction:
775 undef *hassgropper::DESTROY;
776 undef $do::not::overwrite::this;
780 is ref( bless {}, "nul\0clean" ), "nul\0clean", "ref() is nul-clean";
782 # Bit of a hack to make test.pl happy. There are 3 more tests after it leaves.
784 curr_test($test + 3);
785 # test global destruction
787 my $test1 = $test + 1;
788 my $test2 = $test + 2;
793 $ref3 = bless ["ok $test2\n"]; # package destruction
794 my $ref2 = bless ["ok $test1\n"]; # lexical destruction
795 local $ref1 = bless ["ok $test\n"]; # dynamic destruction
796 1; # flush any temp values on stack