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
177 # LVALUE ref, 'cos I can't work out how to create one :)
180 [ 'undef', SCALAR => \undef ],
181 [ 'constant IV', SCALAR => \1 ],
182 [ 'constant NV', SCALAR => \1.0 ],
183 [ 'constant PV', SCALAR => \'f' ],
184 [ 'scalar', SCALAR => \$x ],
185 [ 'PVIV', SCALAR => \$pviv ],
186 [ 'PVNV', SCALAR => \$pvnv ],
187 [ 'PVMG', SCALAR => \$0 ],
188 [ 'PVBM', SCALAR => \PVBM ],
189 [ 'vstring', VSTRING => \v1 ],
190 [ 'ref', REF => \\1 ],
191 [ 'lvalue', LVALUE => \substr($x, 0, 0) ],
192 [ 'named array', ARRAY => \@ary ],
193 [ 'anon array', ARRAY => [ 1 ] ],
194 [ 'named hash', HASH => \%whatever ],
195 [ 'anon hash', HASH => { a => 1 } ],
196 [ 'named sub', CODE => \&mysub, ],
197 [ 'anon sub', CODE => sub { 1; } ],
198 [ 'glob', GLOB => \*foo ],
199 [ 'format', FORMAT => *STDERR{FORMAT} ],
201 my ($desc, $type, $ref) = @$_;
202 is (ref $ref, $type, "ref() for ref to $desc");
203 like ("$ref", qr/^$type\(0x[0-9a-f]+\)$/, "stringify for ref to $desc");
206 is (ref *STDOUT{IO}, 'IO::File', 'IO refs are blessed into IO::File');
207 like (*STDOUT{IO}, qr/^IO::File=IO\(0x[0-9a-f]+\)$/,
208 'stringify for IO refs');
210 # Test anonymous hash syntax.
213 is (ref $anonhash, 'HASH');
214 $anonhash2 = {FOO => 'BAR', ABC => 'XYZ',};
215 is (join('', sort values %$anonhash2), 'BARXYZ');
217 # Test bless operator.
221 $object = bless $main'anonhash2;
222 main::is (ref $object, 'MYHASH');
223 main::is ($object->{ABC}, 'XYZ');
226 main::is (ref $object2, 'MYHASH');
228 # Test ordinary call on object method.
230 &mymethod($object,"argument");
233 local($THIS, @ARGS) = @_;
234 die 'Got a "' . ref($THIS). '" instead of a MYHASH'
235 unless ref $THIS eq 'MYHASH';
236 main::is ($ARGS[0], "argument");
237 main::is ($THIS->{FOO}, 'BAR');
240 # Test automatic destructor call.
245 $main'anonhash2 = "foo";
249 return unless $string;
250 main::is ($string, 'good');
252 # Test that the object has not already been "cursed".
253 main::isnt (ref shift, 'HASH');
256 # Now test inheritance of methods.
262 $main'object = bless {FOO => 'foo', BAR => 'bar'};
266 # Test arrow-style method invocation.
268 is ($object->doit("BAR"), 'bar');
270 # Test indirect-object-style method invocation.
272 $foo = doit $object "FOO";
273 main::is ($foo, 'foo');
277 die "Not an OBJ" unless ref $ref eq 'OBJ';
285 sub foo { main::is ($_[1], 'works') }
288 foo WHATEVER "works";
291 # test the \(@foo) construct
296 @baz = \(1,@foo,@bar);
297 is (scalar (@bar), 3);
298 is (scalar grep(ref($_), @bar), 3);
299 is (scalar (@baz), 3);
301 my(@fuu) = \(1..2,3);
303 my(@bzz) = \(1,@fuu,@baa);
304 is (scalar (@baa), 3);
305 is (scalar grep(ref($_), @baa), 3);
306 is (scalar (@bzz), 3);
308 # also, it can't be an lvalue
309 eval '\\($x, $y) = (1, 2);';
310 like ($@, qr/Can\'t modify.*ref.*in.*assignment/);
312 # test for proper destruction of lexical objects
314 sub larry::DESTROY { print "# larry\nok $test\n"; }
315 sub curly::DESTROY { print "# curly\nok ", $test + 1, "\n"; }
316 sub moe::DESTROY { print "# moe\nok ", $test + 2, "\n"; }
319 my ($joe, @curly, %larry);
320 my $moe = bless \$joe, 'moe';
321 my $curly = bless \@curly, 'curly';
322 my $larry = bless \%larry, 'larry';
323 print "# leaving block\n";
326 print "# left block\n";
327 curr_test($test + 3);
333 { local(*bar) = "foo" }
343 # test if reblessing during destruction results in more destruction
347 sub new { bless {}, shift }
348 DESTROY { print "# destroying 'A'\nok ", $test + 1, "\n" }
350 sub new { bless {}, shift }
351 DESTROY { print "# destroying '_B'\nok $test\n"; bless shift, 'A' }
355 curr_test($test + 2);
357 # test if $_[0] is properly protected in DESTROY()
360 my $test = curr_test();
362 local $SIG{'__DIE__'} = sub {
365 print "# infinite recursion, bailing\nnot ok $test\n";
368 like ($m, qr/^Modification of a read-only/);
371 sub new { bless {}, shift }
372 DESTROY { $_[0] = 'foo' }
374 print "# should generate an error...\n";
377 print "# good, didn't recurse\n";
380 # test that DESTROY is called on all objects during global destruction,
381 # even those without hard references [perl #36347]
383 $TODO = 'bug #36347';
386 stderr => 1, prog => 'sub DESTROY { print qq-aaa\n- } bless \$a[0]'
388 "aaa\n", 'DESTROY called on array elem'
393 prog => '{ bless \my@x; *a=sub{@x}}sub DESTROY { print qq-aaa\n- }'
396 'DESTROY called on closure variable'
400 # test if refgen behaves with autoviv magic
412 # This test is the reason for postponed destruction in sv_unref
417 # This test used to coredump. The BEGIN block is important as it causes the
418 # op that created the constant reference to be freed. Hence the only
419 # reference to the constant string "pass" is in $a. The hack that made
420 # sure $a = $a->[1] would work didn't work with references to constants.
423 foreach my $lexical ('', 'my $a; ') {
424 my $expect = "pass\n";
425 my $result = runperl (switches => ['-wl'], stderr => 1,
426 prog => $lexical . 'BEGIN {$a = \q{pass}}; $a = $$a; print $a');
429 is ($result, $expect);
433 sub x::DESTROY {print "ok ", $test + shift->[0], "\n"}
434 { my $a1 = bless [3],"x";
435 my $a2 = bless [2],"x";
436 { my $a3 = bless [1],"x";
437 my $a4 = bless [0],"x";
443 is (runperl (switches=>['-l'],
444 prog=> 'print 1; print qq-*$\*-;print 1;'),
449 runperl(prog => 'sub UNIVERSAL::AUTOLOAD { qr// } a->p' );
450 is ($?, 0, 'UNIVERSAL::AUTOLOAD called when freeing qr//');
452 runperl(prog => 'sub UNIVERSAL::DESTROY { warn } bless \$a, A', stderr => 1);
453 is ($?, 0, 'warn called inside UNIVERSAL::DESTROY');
458 runperl(prog => 'sub f { my $x = shift; *z = $x; } f({}); f();');
459 is ($?, 0, 'coredump on typeglob = (SvRV && !SvROK)');
461 # bug #27268: freeing self-referential typeglobs could trigger
462 # "Attempt to free unreferenced scalar" warnings
465 prog => 'use Symbol;my $x=bless \gensym,q{t}; print;*$$x=$x',
467 ), '', 'freeing self-referential typeglob');
469 # using a regex in the destructor for STDOUT segfaulted because the
470 # REGEX pad had already been freed (ithreads build only). The
471 # object is required to trigger the early freeing of GV refs to to STDOUT
474 local $TODO = "works but output through pipe is mangled" if $^O eq 'VMS';
476 prog => '$x=bless[]; sub IO::Handle::DESTROY{$_=q{bad};s/bad/ok/;print}',
478 ), qr/^(ok)+$/, 'STDOUT destructor');
484 $name_utf8 = $name8 . chr 256;
487 is ($$name8, undef, 'Nothing before we start');
488 is ($$name_utf8, undef, 'Nothing before we start');
490 is ($$name8, "Pound", 'Accessing via 8 bit symref works');
491 local $TODO = "UTF8 mangled in symrefs";
492 is ($$name_utf8, "Pound", 'Accessing via UTF8 symref works');
497 $name_utf8 = $name = chr 9787;
498 utf8::encode $name_utf8;
500 is (length $name, 1, "Name is 1 char");
501 is (length $name_utf8, 3, "UTF8 representation is 3 chars");
503 is ($$name, undef, 'Nothing before we start');
504 is ($$name_utf8, undef, 'Nothing before we start');
506 is ($$name, "Face", 'Accessing via Unicode symref works');
507 local $TODO = "UTF8 mangled in symrefs";
508 is ($$name_utf8, undef,
509 'Accessing via the UTF8 byte sequence gives nothing');
517 isnt ($name1, $name2, "They differ");
519 is ($$name1, undef, 'Nothing before we start (scalars)');
520 is ($$name2, undef, 'Nothing before we start');
522 is ($$name1, "Yummy", 'Accessing via the correct name works');
524 'Accessing via a different NUL-containing name gives nothing');
525 # defined uses a different code path
526 ok (defined $$name1, 'defined via the correct name works');
527 ok (!defined $$name2,
528 'defined via a different NUL-containing name gives nothing');
530 is ($name1->[0], undef, 'Nothing before we start (arrays)');
531 is ($name2->[0], undef, 'Nothing before we start');
532 $name1->[0] = "Yummy";
533 is ($name1->[0], "Yummy", 'Accessing via the correct name works');
534 is ($name2->[0], undef,
535 'Accessing via a different NUL-containing name gives nothing');
536 ok (defined $name1->[0], 'defined via the correct name works');
537 ok (!defined$name2->[0],
538 'defined via a different NUL-containing name gives nothing');
540 my (undef, $one) = @{$name1}[2,3];
541 my (undef, $two) = @{$name2}[2,3];
542 is ($one, undef, 'Nothing before we start (array slices)');
543 is ($two, undef, 'Nothing before we start');
544 @{$name1}[2,3] = ("Very", "Yummy");
545 (undef, $one) = @{$name1}[2,3];
546 (undef, $two) = @{$name2}[2,3];
547 is ($one, "Yummy", 'Accessing via the correct name works');
549 'Accessing via a different NUL-containing name gives nothing');
550 ok (defined $one, 'defined via the correct name works');
552 'defined via a different NUL-containing name gives nothing');
554 is ($name1->{PWOF}, undef, 'Nothing before we start (hashes)');
555 is ($name2->{PWOF}, undef, 'Nothing before we start');
556 $name1->{PWOF} = "Yummy";
557 is ($name1->{PWOF}, "Yummy", 'Accessing via the correct name works');
558 is ($name2->{PWOF}, undef,
559 'Accessing via a different NUL-containing name gives nothing');
560 ok (defined $name1->{PWOF}, 'defined via the correct name works');
561 ok (!defined $name2->{PWOF},
562 'defined via a different NUL-containing name gives nothing');
564 my (undef, $one) = @{$name1}{'SNIF', 'BEEYOOP'};
565 my (undef, $two) = @{$name2}{'SNIF', 'BEEYOOP'};
566 is ($one, undef, 'Nothing before we start (hash slices)');
567 is ($two, undef, 'Nothing before we start');
568 @{$name1}{'SNIF', 'BEEYOOP'} = ("Very", "Yummy");
569 (undef, $one) = @{$name1}{'SNIF', 'BEEYOOP'};
570 (undef, $two) = @{$name2}{'SNIF', 'BEEYOOP'};
571 is ($one, "Yummy", 'Accessing via the correct name works');
573 'Accessing via a different NUL-containing name gives nothing');
574 ok (defined $one, 'defined via the correct name works');
576 'defined via a different NUL-containing name gives nothing');
578 $name1 = "Left"; $name2 = "Left\0Right";
579 my $glob2 = *{$name2};
581 is ($glob1, undef, "We get different typeglobs. In fact, undef");
583 *{$name1} = sub {"One"};
584 *{$name2} = sub {"Two"};
586 is (&{$name1}, "One");
587 is (&{$name2}, "Two");
590 # test derefs after list slice
592 is ( ({foo => "bar"})[0]{foo}, "bar", 'hash deref from list slice w/o ->' );
593 is ( ({foo => "bar"})[0]->{foo}, "bar", 'hash deref from list slice w/ ->' );
594 is ( ([qw/foo bar/])[0][1], "bar", 'array deref from list slice w/o ->' );
595 is ( ([qw/foo bar/])[0]->[1], "bar", 'array deref from list slice w/ ->' );
596 is ( (sub {"bar"})[0](), "bar", 'code deref from list slice w/o ->' );
597 is ( (sub {"bar"})[0]->(), "bar", 'code deref from list slice w/ ->' );
599 # deref on empty list shouldn't autovivify
603 like ( "$@", "Can't use an undefined value as a HASH reference",
604 "deref of undef from list slice fails" );
607 # test dereferencing errors
612 foreach $ref (*STDOUT{IO}, *STDERR{FORMAT}) {
614 like($@, qr/Not a SCALAR reference/, "Scalar dereference");
616 like($@, qr/Not an ARRAY reference/, "Array dereference");
618 like($@, qr/Not a HASH reference/, "Hash dereference");
620 like($@, qr/Not a CODE reference/, "Code dereference");
623 $ref = *STDERR{FORMAT};
625 like($@, qr/Not a GLOB reference/, "Glob dereference");
629 is($@, '', "Glob dereference of PVIO is acceptable");
631 is($ref, *{$ref}{IO}, "IO slot of the temporary glob is set correctly");
634 # these will segfault if they fail
639 ok (!eval { *$rpvbm }, 'PVBM ref is not a GLOB ref');
640 ok (!eval { *$pvbm }, 'PVBM is not a GLOB ref');
641 ok (!eval { $$pvbm }, 'PVBM is not a SCALAR ref');
642 ok (!eval { @$pvbm }, 'PVBM is not an ARRAY ref');
643 ok (!eval { %$pvbm }, 'PVBM is not a HASH ref');
644 ok (!eval { $pvbm->() }, 'PVBM is not a CODE ref');
645 ok (!eval { $rpvbm->foo }, 'PVBM is not an object');
648 is( runperl(stderr => 1, prog => 'map eval qq(exit),1 for 1'), "");
649 is( runperl(stderr => 1, prog => 'eval { for (1) { map { die } 2 } };'), "");
650 is( runperl(stderr => 1, prog => 'for (125) { map { exit } (213)}'), "");
651 my $hushed = $^O eq 'VMS' ? 'use vmsish qw(hushed);' : '';
652 is( runperl(stderr => 1, prog => $hushed . 'map die,4 for 3'), "Died at -e line 1.\n");
653 is( runperl(stderr => 1, prog => $hushed . 'grep die,4 for 3'), "Died at -e line 1.\n");
654 is( runperl(stderr => 1, prog => $hushed . 'for $a (3) {@b=sort {die} 4,5}'), "Died at -e line 1.\n");
657 is( runperl(stderr => 1, prog => 'my $i;for $i (1) { for $i (2) { } }'), "");
659 # The mechanism for freeing objects in globs used to leave dangling
660 # pointers to freed SVs. To test this, we construct this nested structure:
661 # GV => blessed(AV) => RV => GV => blessed(SV)
662 # all with a refcnt of 1, and hope that the second GV gets processed first
663 # by do_clean_named_objs. Then when the first GV is processed, it mustn't
664 # find anything nasty left by the previous GV processing.
665 # The eval is stop things in the main body of the code holding a reference
666 # to a GV, and the print at the end seems to bee necessary to ensure
667 # the correct freeing order of *x and *y (no, I don't know why - DAPM).
670 prog => 'eval q[bless \@y; bless \$x; $y[0] = \*x; $z = \*y; ]; '
671 . 'delete $::{x}; delete $::{y}; print qq{ok\n};',
673 "ok\n", 'freeing freed glob in global destruction');
676 # Test undefined hash references as arguments to %{} in boolean context
680 eval { my $foo; %$foo; }; ok !$@, '%$undef';
681 eval { my $foo; scalar %$foo; }; ok !$@, 'scalar %$undef';
682 eval { my $foo; !%$foo; }; ok !$@, '!%$undef';
683 eval { my $foo; if ( %$foo) {} }; ok !$@, 'if ( %$undef) {}';
684 eval { my $foo; if (!%$foo) {} }; ok !$@, 'if (!%$undef) {}';
685 eval { my $foo; unless ( %$foo) {} }; ok !$@, 'unless ( %$undef) {}';
686 eval { my $foo; unless (!%$foo) {} }; ok !$@, 'unless (!%$undef) {}';
687 eval { my $foo; 1 if %$foo; }; ok !$@, '1 if %$undef';
688 eval { my $foo; 1 if !%$foo; }; ok !$@, '1 if !%$undef';
689 eval { my $foo; 1 unless %$foo; }; ok !$@, '1 unless %$undef;';
690 eval { my $foo; 1 unless ! %$foo; }; ok !$@, '1 unless ! %$undef';
691 eval { my $foo; %$foo ? 1 : 0; }; ok !$@, ' %$undef ? 1 : 0';
692 eval { my $foo; !%$foo ? 1 : 0; }; ok !$@, '!%$undef ? 1 : 0';
696 # Bit of a hack to make test.pl happy. There are 3 more tests after it leaves.
698 curr_test($test + 3);
699 # test global destruction
701 my $test1 = $test + 1;
702 my $test2 = $test + 2;
707 $ref3 = bless ["ok $test2\n"]; # package destruction
708 my $ref2 = bless ["ok $test1\n"]; # lexical destruction
709 local $ref1 = bless ["ok $test\n"]; # dynamic destruction
710 1; # flush any temp values on stack