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 sub PVBM () { 'foo' }
130 { my $dummy = index 'foo', PVBM }
132 my $pviv = 1; "$pviv";
133 my $pvnv = 1.0; "$pvnv";
137 # tied lvalue => SCALAR, as we haven't tested tie yet
138 # BIND, 'cos we can't create them yet
139 # REGEXP, 'cos that requires overload or Scalar::Util
140 # LVALUE ref, 'cos I can't work out how to create one :)
143 [ 'undef', SCALAR => \undef ],
144 [ 'constant IV', SCALAR => \1 ],
145 [ 'constant NV', SCALAR => \1.0 ],
146 [ 'constant PV', SCALAR => \'f' ],
147 [ 'scalar', SCALAR => \$x ],
148 [ 'PVIV', SCALAR => \$pviv ],
149 [ 'PVNV', SCALAR => \$pvnv ],
150 [ 'PVMG', SCALAR => \$0 ],
151 [ 'PVBM', SCALAR => \PVBM ],
152 [ 'vstring', VSTRING => \v1 ],
153 [ 'ref', REF => \\1 ],
154 [ 'lvalue', LVALUE => \substr($x, 0, 0) ],
155 [ 'named array', ARRAY => \@ary ],
156 [ 'anon array', ARRAY => [ 1 ] ],
157 [ 'named hash', HASH => \%whatever ],
158 [ 'anon hash', HASH => { a => 1 } ],
159 [ 'named sub', CODE => \&mysub, ],
160 [ 'anon sub', CODE => sub { 1; } ],
161 [ 'glob', GLOB => \*foo ],
162 [ 'format', FORMAT => *STDERR{FORMAT} ],
164 my ($desc, $type, $ref) = @$_;
165 is (ref $ref, $type, "ref() for ref to $desc");
166 like ("$ref", qr/^$type\(0x[0-9a-f]+\)$/, "stringify for ref to $desc");
169 is (ref *STDOUT{IO}, 'IO::Handle', 'IO refs are blessed into IO::Handle');
170 like (*STDOUT{IO}, qr/^IO::Handle=IO\(0x[0-9a-f]+\)$/,
171 'stringify for IO refs');
173 # Test anonymous hash syntax.
176 is (ref $anonhash, 'HASH');
177 $anonhash2 = {FOO => 'BAR', ABC => 'XYZ',};
178 is (join('', sort values %$anonhash2), 'BARXYZ');
180 # Test bless operator.
184 $object = bless $main'anonhash2;
185 main::is (ref $object, 'MYHASH');
186 main::is ($object->{ABC}, 'XYZ');
189 main::is (ref $object2, 'MYHASH');
191 # Test ordinary call on object method.
193 &mymethod($object,"argument");
196 local($THIS, @ARGS) = @_;
197 die 'Got a "' . ref($THIS). '" instead of a MYHASH'
198 unless ref $THIS eq 'MYHASH';
199 main::is ($ARGS[0], "argument");
200 main::is ($THIS->{FOO}, 'BAR');
203 # Test automatic destructor call.
208 $main'anonhash2 = "foo";
212 return unless $string;
213 main::is ($string, 'good');
215 # Test that the object has not already been "cursed".
216 main::isnt (ref shift, 'HASH');
219 # Now test inheritance of methods.
225 $main'object = bless {FOO => 'foo', BAR => 'bar'};
229 # Test arrow-style method invocation.
231 is ($object->doit("BAR"), 'bar');
233 # Test indirect-object-style method invocation.
235 $foo = doit $object "FOO";
236 main::is ($foo, 'foo');
240 die "Not an OBJ" unless ref $ref eq 'OBJ';
248 sub foo { main::is ($_[1], 'works') }
251 foo WHATEVER "works";
254 # test the \(@foo) construct
259 @baz = \(1,@foo,@bar);
260 is (scalar (@bar), 3);
261 is (scalar grep(ref($_), @bar), 3);
262 is (scalar (@baz), 3);
264 my(@fuu) = \(1..2,3);
266 my(@bzz) = \(1,@fuu,@baa);
267 is (scalar (@baa), 3);
268 is (scalar grep(ref($_), @baa), 3);
269 is (scalar (@bzz), 3);
271 # also, it can't be an lvalue
272 eval '\\($x, $y) = (1, 2);';
273 like ($@, qr/Can\'t modify.*ref.*in.*assignment/);
275 # test for proper destruction of lexical objects
277 sub larry::DESTROY { print "# larry\nok $test\n"; }
278 sub curly::DESTROY { print "# curly\nok ", $test + 1, "\n"; }
279 sub moe::DESTROY { print "# moe\nok ", $test + 2, "\n"; }
282 my ($joe, @curly, %larry);
283 my $moe = bless \$joe, 'moe';
284 my $curly = bless \@curly, 'curly';
285 my $larry = bless \%larry, 'larry';
286 print "# leaving block\n";
289 print "# left block\n";
290 curr_test($test + 3);
296 { local(*bar) = "foo" }
306 # test if reblessing during destruction results in more destruction
310 sub new { bless {}, shift }
311 DESTROY { print "# destroying 'A'\nok ", $test + 1, "\n" }
313 sub new { bless {}, shift }
314 DESTROY { print "# destroying '_B'\nok $test\n"; bless shift, 'A' }
318 curr_test($test + 2);
320 # test if $_[0] is properly protected in DESTROY()
323 my $test = curr_test();
325 local $SIG{'__DIE__'} = sub {
328 print "# infinite recursion, bailing\nnot ok $test\n";
331 like ($m, qr/^Modification of a read-only/);
334 sub new { bless {}, shift }
335 DESTROY { $_[0] = 'foo' }
337 print "# should generate an error...\n";
340 print "# good, didn't recurse\n";
343 # test if refgen behaves with autoviv magic
355 # This test is the reason for postponed destruction in sv_unref
360 # This test used to coredump. The BEGIN block is important as it causes the
361 # op that created the constant reference to be freed. Hence the only
362 # reference to the constant string "pass" is in $a. The hack that made
363 # sure $a = $a->[1] would work didn't work with references to constants.
366 foreach my $lexical ('', 'my $a; ') {
367 my $expect = "pass\n";
368 my $result = runperl (switches => ['-wl'], stderr => 1,
369 prog => $lexical . 'BEGIN {$a = \q{pass}}; $a = $$a; print $a');
372 is ($result, $expect);
376 sub x::DESTROY {print "ok ", $test + shift->[0], "\n"}
377 { my $a1 = bless [3],"x";
378 my $a2 = bless [2],"x";
379 { my $a3 = bless [1],"x";
380 my $a4 = bless [0],"x";
386 is (runperl (switches=>['-l'],
387 prog=> 'print 1; print qq-*$\*-;print 1;'),
392 runperl(prog => 'sub UNIVERSAL::AUTOLOAD { qr// } a->p' );
393 is ($?, 0, 'UNIVERSAL::AUTOLOAD called when freeing qr//');
395 runperl(prog => 'sub UNIVERSAL::DESTROY { warn } bless \$a, A', stderr => 1);
396 is ($?, 0, 'warn called inside UNIVERSAL::DESTROY');
401 runperl(prog => 'sub f { my $x = shift; *z = $x; } f({}); f();');
402 is ($?, 0, 'coredump on typeglob = (SvRV && !SvROK)');
404 # bug #27268: freeing self-referential typeglobs could trigger
405 # "Attempt to free unreferenced scalar" warnings
408 prog => 'use Symbol;my $x=bless \gensym,"t"; print;*$$x=$x',
410 ), '', 'freeing self-referential typeglob');
412 # using a regex in the destructor for STDOUT segfaulted because the
413 # REGEX pad had already been freed (ithreads build only). The
414 # object is required to trigger the early freeing of GV refs to to STDOUT
417 prog => '$x=bless[]; sub IO::Handle::DESTROY{$_="bad";s/bad/ok/;print}',
419 ), qr/^(ok)+$/, 'STDOUT destructor');
424 $name_utf8 = $name8 . chr 256;
427 is ($$name8, undef, 'Nothing before we start');
428 is ($$name_utf8, undef, 'Nothing before we start');
430 is ($$name8, "Pound", 'Accessing via 8 bit symref works');
431 local $TODO = "UTF8 mangled in symrefs";
432 is ($$name_utf8, "Pound", 'Accessing via UTF8 symref works');
437 $name_utf8 = $name = chr 9787;
438 utf8::encode $name_utf8;
440 is (length $name, 1, "Name is 1 char");
441 is (length $name_utf8, 3, "UTF8 representation is 3 chars");
443 is ($$name, undef, 'Nothing before we start');
444 is ($$name_utf8, undef, 'Nothing before we start');
446 is ($$name, "Face", 'Accessing via Unicode symref works');
447 local $TODO = "UTF8 mangled in symrefs";
448 is ($$name_utf8, undef,
449 'Accessing via the UTF8 byte sequence gives nothing');
457 isnt ($name1, $name2, "They differ");
459 is ($$name1, undef, 'Nothing before we start (scalars)');
460 is ($$name2, undef, 'Nothing before we start');
462 is ($$name1, "Yummy", 'Accessing via the correct name works');
464 'Accessing via a different NUL-containing name gives nothing');
465 # defined uses a different code path
466 ok (defined $$name1, 'defined via the correct name works');
467 ok (!defined $$name2,
468 'defined via a different NUL-containing name gives nothing');
470 is ($name1->[0], undef, 'Nothing before we start (arrays)');
471 is ($name2->[0], undef, 'Nothing before we start');
472 $name1->[0] = "Yummy";
473 is ($name1->[0], "Yummy", 'Accessing via the correct name works');
474 is ($name2->[0], undef,
475 'Accessing via a different NUL-containing name gives nothing');
476 ok (defined $name1->[0], 'defined via the correct name works');
477 ok (!defined$name2->[0],
478 'defined via a different NUL-containing name gives nothing');
480 my (undef, $one) = @{$name1}[2,3];
481 my (undef, $two) = @{$name2}[2,3];
482 is ($one, undef, 'Nothing before we start (array slices)');
483 is ($two, undef, 'Nothing before we start');
484 @{$name1}[2,3] = ("Very", "Yummy");
485 (undef, $one) = @{$name1}[2,3];
486 (undef, $two) = @{$name2}[2,3];
487 is ($one, "Yummy", 'Accessing via the correct name works');
489 'Accessing via a different NUL-containing name gives nothing');
490 ok (defined $one, 'defined via the correct name works');
492 'defined via a different NUL-containing name gives nothing');
494 is ($name1->{PWOF}, undef, 'Nothing before we start (hashes)');
495 is ($name2->{PWOF}, undef, 'Nothing before we start');
496 $name1->{PWOF} = "Yummy";
497 is ($name1->{PWOF}, "Yummy", 'Accessing via the correct name works');
498 is ($name2->{PWOF}, undef,
499 'Accessing via a different NUL-containing name gives nothing');
500 ok (defined $name1->{PWOF}, 'defined via the correct name works');
501 ok (!defined $name2->{PWOF},
502 'defined via a different NUL-containing name gives nothing');
504 my (undef, $one) = @{$name1}{'SNIF', 'BEEYOOP'};
505 my (undef, $two) = @{$name2}{'SNIF', 'BEEYOOP'};
506 is ($one, undef, 'Nothing before we start (hash slices)');
507 is ($two, undef, 'Nothing before we start');
508 @{$name1}{'SNIF', 'BEEYOOP'} = ("Very", "Yummy");
509 (undef, $one) = @{$name1}{'SNIF', 'BEEYOOP'};
510 (undef, $two) = @{$name2}{'SNIF', 'BEEYOOP'};
511 is ($one, "Yummy", 'Accessing via the correct name works');
513 'Accessing via a different NUL-containing name gives nothing');
514 ok (defined $one, 'defined via the correct name works');
516 'defined via a different NUL-containing name gives nothing');
518 $name1 = "Left"; $name2 = "Left\0Right";
519 my $glob2 = *{$name2};
521 is ($glob1, undef, "We get different typeglobs. In fact, undef");
523 *{$name1} = sub {"One"};
524 *{$name2} = sub {"Two"};
526 is (&{$name1}, "One");
527 is (&{$name2}, "Two");
530 # test derefs after list slice
532 is ( ({foo => "bar"})[0]{foo}, "bar", 'hash deref from list slice w/o ->' );
533 is ( ({foo => "bar"})[0]->{foo}, "bar", 'hash deref from list slice w/ ->' );
534 is ( ([qw/foo bar/])[0][1], "bar", 'array deref from list slice w/o ->' );
535 is ( ([qw/foo bar/])[0]->[1], "bar", 'array deref from list slice w/ ->' );
536 is ( (sub {"bar"})[0](), "bar", 'code deref from list slice w/o ->' );
537 is ( (sub {"bar"})[0]->(), "bar", 'code deref from list slice w/ ->' );
539 # deref on empty list shouldn't autovivify
543 like ( "$@", "Can't use an undefined value as a HASH reference",
544 "deref of undef from list slice fails" );
547 # test dereferencing errors
552 foreach $ref (*STDOUT{IO}, *STDERR{FORMAT}) {
554 like($@, qr/Not a SCALAR reference/, "Scalar dereference");
556 like($@, qr/Not an ARRAY reference/, "Array dereference");
558 like($@, qr/Not a HASH reference/, "Hash dereference");
560 like($@, qr/Not a CODE reference/, "Code dereference");
563 $ref = *STDERR{FORMAT};
565 like($@, qr/Not a GLOB reference/, "Glob dereference");
569 is($@, '', "Glob dereference of PVIO is acceptable");
571 is($ref, *{$ref}{IO}, "IO slot of the temporary glob is set correctly");
574 # these will segfault if they fail
579 ok (!eval { *$rpvbm }, 'PVBM ref is not a GLOB ref');
580 ok (!eval { *$pvbm }, 'PVBM is not a GLOB ref');
581 ok (!eval { $$pvbm }, 'PVBM is not a SCALAR ref');
582 ok (!eval { @$pvbm }, 'PVBM is not an ARRAY ref');
583 ok (!eval { %$pvbm }, 'PVBM is not a HASH ref');
584 ok (!eval { $pvbm->() }, 'PVBM is not a CODE ref');
585 ok (!eval { $rpvbm->foo }, 'PVBM is not an object');
588 is( runperl(stderr => 1, prog => 'map eval qq(exit),1 for 1'), "");
589 is( runperl(stderr => 1, prog => 'eval { for (1) { map { die } 2 } };'), "");
590 is( runperl(stderr => 1, prog => 'for (125) { map { exit } (213)}'), "");
591 my $hushed = $^O eq 'VMS' ? 'use vmsish qw(hushed);' : '';
592 is( runperl(stderr => 1, prog => $hushed . 'map die,4 for 3'), "Died at -e line 1.\n");
593 is( runperl(stderr => 1, prog => $hushed . 'grep die,4 for 3'), "Died at -e line 1.\n");
594 is( runperl(stderr => 1, prog => $hushed . 'for $a (3) {@b=sort {die} 4,5}'), "Died at -e line 1.\n");
597 is( runperl(stderr => 1, prog => 'my $i;for $i (1) { for $i (2) { } }'), "");
600 # Bit of a hack to make test.pl happy. There are 3 more tests after it leaves.
602 curr_test($test + 3);
603 # test global destruction
605 my $test1 = $test + 1;
606 my $test2 = $test + 2;
611 $ref3 = bless ["ok $test2\n"]; # package destruction
612 my $ref2 = bless ["ok $test1\n"]; # lexical destruction
613 local $ref1 = bless ["ok $test\n"]; # dynamic destruction
614 1; # flush any temp values on stack