1#!./perl 2 3BEGIN { 4 chdir 't' if -d 't'; 5 @INC = qw(. ../lib); 6 require './test.pl'; 7} 8 9use strict qw(refs subs); 10 11plan(235); 12 13# Test this first before we extend the stack with other operations. 14# This caused an asan failure due to a bad write past the end of the stack. 15eval { die 1..127, $_=\() }; 16 17# Test glob operations. 18 19$bar = "one"; 20$foo = "two"; 21{ 22 local(*foo) = *bar; 23 is($foo, 'one'); 24} 25is ($foo, 'two'); 26 27$baz = "three"; 28$foo = "four"; 29{ 30 local(*foo) = 'baz'; 31 is ($foo, 'three'); 32} 33is ($foo, 'four'); 34 35$foo = "global"; 36{ 37 local(*foo); 38 is ($foo, undef); 39 $foo = "local"; 40 is ($foo, 'local'); 41} 42is ($foo, 'global'); 43 44{ 45 no strict 'refs'; 46# Test fake references. 47 48 $baz = "valid"; 49 $bar = 'baz'; 50 $foo = 'bar'; 51 is ($$$foo, 'valid'); 52} 53 54# Test real references. 55 56$FOO = \$BAR; 57$BAR = \$BAZ; 58$BAZ = "hit"; 59is ($$$FOO, 'hit'); 60 61# Test references to real arrays. 62 63my $test = curr_test(); 64@ary = ($test,$test+1,$test+2,$test+3); 65$ref[0] = \@a; 66$ref[1] = \@b; 67$ref[2] = \@c; 68$ref[3] = \@d; 69for $i (3,1,2,0) { 70 push(@{$ref[$i]}, "ok $ary[$i]\n"); 71} 72print @a; 73print ${$ref[1]}[0]; 74print @{$ref[2]}[0]; 75{ 76 no strict 'refs'; 77 print @{'d'}; 78} 79curr_test($test+4); 80 81# Test references to references. 82 83$refref = \\$x; 84$x = "Good"; 85is ($$$refref, 'Good'); 86 87# Test nested anonymous arrays. 88 89$ref = [[],2,[3,4,5,]]; 90is (scalar @$ref, 3); 91is ($$ref[1], 2); 92is (${$$ref[2]}[2], 5); 93is (scalar @{$$ref[0]}, 0); 94 95is ($ref->[1], 2); 96is ($ref->[2]->[0], 3); 97 98# Test references to hashes of references. 99 100$refref = \%whatever; 101$refref->{"key"} = $ref; 102is ($refref->{"key"}->[2]->[0], 3); 103 104# Test to see if anonymous subarrays spring into existence. 105 106$spring[5]->[0] = 123; 107$spring[5]->[1] = 456; 108push(@{$spring[5]}, 789); 109is (join(':',@{$spring[5]}), "123:456:789"); 110 111# Test to see if anonymous subhashes spring into existence. 112 113@{$spring2{"foo"}} = (1,2,3); 114$spring2{"foo"}->[3] = 4; 115is (join(':',@{$spring2{"foo"}}), "1:2:3:4"); 116 117# Test references to subroutines. 118 119{ 120 my $called; 121 sub mysub { $called++; } 122 $subref = \&mysub; 123 &$subref; 124 is ($called, 1); 125} 126is ref eval {\&{""}}, "CODE", 'reference to &{""} [perl #94476]'; 127 128# Test references to return values of operators (TARGs/PADTMPs) 129{ 130 my @refs; 131 for("a", "b") { 132 push @refs, \"$_" 133 } 134 is join(" ", map $$_, @refs), "a b", 'refgen+PADTMP'; 135} 136 137$subrefref = \\&mysub2; 138is ($$subrefref->("GOOD"), "good"); 139sub mysub2 { lc shift } 140 141# Test REGEXP assignment 142 143SKIP: { 144 skip_if_miniperl("no dynamic loading on miniperl, so can't load re", 5); 145 require re; 146 my $x = qr/x/; 147 my $str = "$x"; # regex stringification may change 148 149 my $y = $$x; 150 is ($y, $str, "bare REGEXP stringifies correctly"); 151 ok (eval { "x" =~ $y }, "bare REGEXP matches correctly"); 152 153 my $z = \$y; 154 ok (re::is_regexp($z), "new ref to REXEXP passes is_regexp"); 155 is ($z, $str, "new ref to REGEXP stringifies correctly"); 156 ok (eval { "x" =~ $z }, "new ref to REGEXP matches correctly"); 157} 158{ 159 my ($x, $str); 160 { 161 my $y = qr/x/; 162 $str = "$y"; 163 $x = $$y; 164 } 165 is ($x, $str, "REGEXP keeps a ref to its mother_re"); 166 ok (eval { "x" =~ $x }, "REGEXP with mother_re still matches"); 167} 168 169# Test the ref operator. 170 171sub PVBM () { 'foo' } 172{ my $dummy = index 'foo', PVBM } 173 174my $pviv = 1; "$pviv"; 175my $pvnv = 1.0; "$pvnv"; 176my $x; 177 178# we don't test 179# tied lvalue => SCALAR, as we haven't tested tie yet 180# BIND, 'cos we can't create them yet 181# REGEXP, 'cos that requires overload or Scalar::Util 182 183for ( 184 [ 'undef', SCALAR => \undef ], 185 [ 'constant IV', SCALAR => \1 ], 186 [ 'constant NV', SCALAR => \1.0 ], 187 [ 'constant PV', SCALAR => \'f' ], 188 [ 'scalar', SCALAR => \$x ], 189 [ 'PVIV', SCALAR => \$pviv ], 190 [ 'PVNV', SCALAR => \$pvnv ], 191 [ 'PVMG', SCALAR => \$0 ], 192 [ 'PVBM', SCALAR => \PVBM ], 193 [ 'scalar @array', SCALAR => \scalar @array ], 194 [ 'scalar %hash', SCALAR => \scalar %hash ], 195 [ 'vstring', VSTRING => \v1 ], 196 [ 'ref', REF => \\1 ], 197 [ 'substr lvalue', LVALUE => \substr($x, 0, 0) ], 198 [ 'pos lvalue', LVALUE => \pos ], 199 [ 'vec lvalue', LVALUE => \vec($x,0,1) ], 200 [ 'named array', ARRAY => \@ary ], 201 [ 'anon array', ARRAY => [ 1 ] ], 202 [ 'named hash', HASH => \%whatever ], 203 [ 'anon hash', HASH => { a => 1 } ], 204 [ 'named sub', CODE => \&mysub, ], 205 [ 'anon sub', CODE => sub { 1; } ], 206 [ 'glob', GLOB => \*foo ], 207 [ 'format', FORMAT => *STDERR{FORMAT} ], 208) { 209 my ($desc, $type, $ref) = @$_; 210 is (ref $ref, $type, "ref() for ref to $desc"); 211 like ("$ref", qr/^$type\(0x[0-9a-f]+\)$/, "stringify for ref to $desc"); 212} 213 214is (ref *STDOUT{IO}, 'IO::File', 'IO refs are blessed into IO::File'); 215like (*STDOUT{IO}, qr/^IO::File=IO\(0x[0-9a-f]+\)$/, 216 'stringify for IO refs'); 217 218{ # Test re-use of ref's TARG [perl #101738] 219 my $obj = bless [], '____'; 220 my $uniobj = bless [], chr 256; 221 my $get_ref = sub { ref shift }; 222 my $dummy = &$get_ref($uniobj); 223 $dummy = &$get_ref($obj); 224 ok exists { ____ => undef }->{$dummy}, 'ref sets UTF8 flag correctly'; 225} 226 227# Test anonymous hash syntax. 228 229$anonhash = {}; 230is (ref $anonhash, 'HASH'); 231$anonhash2 = {FOO => 'BAR', ABC => 'XYZ',}; 232is (join('', sort values %$anonhash2), 'BARXYZ'); 233 234# Test bless operator. 235 236package MYHASH; 237 238$object = bless $main'anonhash2; 239main::is (ref $object, 'MYHASH'); 240main::is ($object->{ABC}, 'XYZ'); 241 242$object2 = bless {}; 243main::is (ref $object2, 'MYHASH'); 244 245# Test ordinary call on object method. 246 247&mymethod($object,"argument"); 248 249sub mymethod { 250 local($THIS, @ARGS) = @_; 251 die 'Got a "' . ref($THIS). '" instead of a MYHASH' 252 unless ref $THIS eq 'MYHASH'; 253 main::is ($ARGS[0], "argument"); 254 main::is ($THIS->{FOO}, 'BAR'); 255} 256 257# Test automatic destructor call. 258 259$string = "bad"; 260$object = "foo"; 261$string = "good"; 262$main'anonhash2 = "foo"; 263$string = ""; 264 265DESTROY { 266 return unless $string; 267 main::is ($string, 'good'); 268 269 # Test that the object has not already been "cursed". 270 main::isnt (ref shift, 'HASH'); 271} 272 273# Now test inheritance of methods. 274 275package OBJ; 276 277@ISA = ('BASEOBJ'); 278 279$main'object = bless {FOO => 'foo', BAR => 'bar'}; 280 281package main; 282 283# Test arrow-style method invocation. 284 285is ($object->doit("BAR"), 'bar'); 286 287# Test indirect-object-style method invocation. 288 289$foo = doit $object "FOO"; 290main::is ($foo, 'foo'); 291 292sub BASEOBJ'doit { 293 local $ref = shift; 294 die "Not an OBJ" unless ref $ref eq 'OBJ'; 295 $ref->{shift()}; 296} 297 298package UNIVERSAL; 299@ISA = 'LASTCHANCE'; 300 301package LASTCHANCE; 302sub foo { main::is ($_[1], 'works') } 303 304package WHATEVER; 305foo WHATEVER "works"; 306 307# 308# test the \(@foo) construct 309# 310package main; 311@foo = \(1..3); 312@bar = \(@foo); 313@baz = \(1,@foo,@bar); 314is (scalar (@bar), 3); 315is (scalar grep(ref($_), @bar), 3); 316is (scalar (@baz), 3); 317 318my(@fuu) = \(1..2,3); 319my(@baa) = \(@fuu); 320my(@bzz) = \(1,@fuu,@baa); 321is (scalar (@baa), 3); 322is (scalar grep(ref($_), @baa), 3); 323is (scalar (@bzz), 3); 324 325# also, it can't be an lvalue 326# (That’s what *you* think! --sprout) 327eval '\\($x, $y) = (1, 2);'; 328like ($@, qr/Can\'t modify.*ref.*in.*assignment(?x: 329 )|Experimental aliasing via reference not enabled/); 330 331# test for proper destruction of lexical objects 332$test = curr_test(); 333sub larry::DESTROY { print "# larry\nok $test\n"; } 334sub curly::DESTROY { print "# curly\nok ", $test + 1, "\n"; } 335sub moe::DESTROY { print "# moe\nok ", $test + 2, "\n"; } 336 337{ 338 my ($joe, @curly, %larry); 339 my $moe = bless \$joe, 'moe'; 340 my $curly = bless \@curly, 'curly'; 341 my $larry = bless \%larry, 'larry'; 342 print "# leaving block\n"; 343} 344 345print "# left block\n"; 346curr_test($test + 3); 347 348# another glob test 349 350 351$foo = "garbage"; 352{ local(*bar) = "foo" } 353$bar = "glob 3"; 354local(*bar) = *bar; 355is ($bar, "glob 3"); 356 357$var = "glob 4"; 358$_ = \$var; 359is ($$_, 'glob 4'); 360 361 362# test if reblessing during destruction results in more destruction 363$test = curr_test(); 364{ 365 package A; 366 sub new { bless {}, shift } 367 DESTROY { print "# destroying 'A'\nok ", $test + 1, "\n" } 368 package _B; 369 sub new { bless {}, shift } 370 DESTROY { print "# destroying '_B'\nok $test\n"; bless shift, 'A' } 371 package main; 372 my $b = _B->new; 373} 374curr_test($test + 2); 375 376# test if $_[0] is properly protected in DESTROY() 377 378{ 379 my $test = curr_test(); 380 my $i = 0; 381 local $SIG{'__DIE__'} = sub { 382 my $m = shift; 383 if ($i++ > 4) { 384 print "# infinite recursion, bailing\nnot ok $test\n"; 385 exit 1; 386 } 387 like ($m, qr/^Modification of a read-only/); 388 }; 389 package C; 390 sub new { bless {}, shift } 391 DESTROY { $_[0] = 'foo' } 392 { 393 print "# should generate an error...\n"; 394 my $c = C->new; 395 } 396 print "# good, didn't recurse\n"; 397} 398 399# test that DESTROY is called on all objects during global destruction, 400# even those without hard references [perl #36347] 401 402is( 403 runperl( 404 stderr => 1, prog => 'sub DESTROY { print qq-aaa\n- } bless \$a[0]' 405 ), 406 "aaa\n", 'DESTROY called on array elem' 407); 408is( 409 runperl( 410 stderr => 1, 411 prog => '{ bless \my@x; *a=sub{@x}}sub DESTROY { print qq-aaa\n- }' 412 ), 413 "aaa\n", 414 'DESTROY called on closure variable' 415); 416 417# But cursing objects must not result in double frees 418# This caused "Attempt to free unreferenced scalar" in 5.16. 419fresh_perl_is( 420 'bless \%foo::, bar::; bless \%bar::, foo::; print "ok\n"', "ok\n", 421 { stderr => 1 }, 422 'no double free when stashes are blessed into each other'); 423 424 425# test if refgen behaves with autoviv magic 426{ 427 my @a; 428 $a[1] = "good"; 429 my $got; 430 for (@a) { 431 $got .= ${\$_}; 432 $got .= ';'; 433 } 434 is ($got, ";good;"); 435} 436 437# This test is the reason for postponed destruction in sv_unref 438$a = [1,2,3]; 439$a = $a->[1]; 440is ($a, 2); 441 442# This test used to coredump. The BEGIN block is important as it causes the 443# op that created the constant reference to be freed. Hence the only 444# reference to the constant string "pass" is in $a. The hack that made 445# sure $a = $a->[1] would work didn't work with references to constants. 446 447 448foreach my $lexical ('', 'my $a; ') { 449 my $expect = "pass\n"; 450 my $result = runperl (switches => ['-wl'], stderr => 1, 451 prog => $lexical . 'BEGIN {$a = \q{pass}}; $a = $$a; print $a'); 452 453 is ($?, 0); 454 is ($result, $expect); 455} 456 457$test = curr_test(); 458sub x::DESTROY {print "ok ", $test + shift->[0], "\n"} 459{ my $a1 = bless [3],"x"; 460 my $a2 = bless [2],"x"; 461 { my $a3 = bless [1],"x"; 462 my $a4 = bless [0],"x"; 463 567; 464 } 465} 466curr_test($test+4); 467 468is (runperl (switches=>['-l'], 469 prog=> 'print 1; print qq-*$\*-;print 1;'), 470 "1\n*\n*\n1\n"); 471 472# bug #21347 473 474runperl(prog => 'sub UNIVERSAL::AUTOLOAD { qr// } a->p' ); 475is ($?, 0, 'UNIVERSAL::AUTOLOAD called when freeing qr//'); 476 477runperl(prog => 'sub UNIVERSAL::DESTROY { warn } bless \$a, A', stderr => 1); 478is ($?, 0, 'warn called inside UNIVERSAL::DESTROY'); 479 480 481# bug #22719 482 483runperl(prog => 'sub f { my $x = shift; *z = $x; } f({}); f();'); 484is ($?, 0, 'coredump on typeglob = (SvRV && !SvROK)'); 485 486# bug #27268: freeing self-referential typeglobs could trigger 487# "Attempt to free unreferenced scalar" warnings 488 489is (runperl( 490 prog => 'use Symbol;my $x=bless \gensym,q{t}; print;*$$x=$x', 491 stderr => 1 492), '', 'freeing self-referential typeglob'); 493 494# using a regex in the destructor for STDOUT segfaulted because the 495# REGEX pad had already been freed (ithreads build only). The 496# object is required to trigger the early freeing of GV refs to to STDOUT 497 498TODO: { 499 local $TODO = "works but output through pipe is mangled" if $^O eq 'VMS'; 500 like (runperl( 501 prog => '$x=bless[]; sub IO::Handle::DESTROY{$_=q{bad};s/bad/ok/;print}', 502 stderr => 1 503 ), qr/^(ok)+$/, 'STDOUT destructor'); 504} 505 506{ 507 no strict 'refs'; 508 $name8 = chr 163; 509 $name_utf8 = $name8 . chr 256; 510 chop $name_utf8; 511 512 is ($$name8, undef, 'Nothing before we start'); 513 is ($$name_utf8, undef, 'Nothing before we start'); 514 $$name8 = "Pound"; 515 is ($$name8, "Pound", 'Accessing via 8 bit symref works'); 516 is ($$name_utf8, "Pound", 'Accessing via UTF8 symref works'); 517} 518 519{ 520 no strict 'refs'; 521 $name_utf8 = $name = chr 9787; 522 utf8::encode $name_utf8; 523 524 is (length $name, 1, "Name is 1 char"); 525 is (length $name_utf8, 3, "UTF8 representation is 3 chars"); 526 527 is ($$name, undef, 'Nothing before we start'); 528 is ($$name_utf8, undef, 'Nothing before we start'); 529 $$name = "Face"; 530 is ($$name, "Face", 'Accessing via Unicode symref works'); 531 is ($$name_utf8, undef, 532 'Accessing via the UTF8 byte sequence gives nothing'); 533} 534 535{ 536 no strict 'refs'; 537 $name1 = "\0Chalk"; 538 $name2 = "\0Cheese"; 539 540 isnt ($name1, $name2, "They differ"); 541 542 is ($$name1, undef, 'Nothing before we start (scalars)'); 543 is ($$name2, undef, 'Nothing before we start'); 544 $$name1 = "Yummy"; 545 is ($$name1, "Yummy", 'Accessing via the correct name works'); 546 is ($$name2, undef, 547 'Accessing via a different NUL-containing name gives nothing'); 548 # defined uses a different code path 549 ok (defined $$name1, 'defined via the correct name works'); 550 ok (!defined $$name2, 551 'defined via a different NUL-containing name gives nothing'); 552 553 is ($name1->[0], undef, 'Nothing before we start (arrays)'); 554 is ($name2->[0], undef, 'Nothing before we start'); 555 $name1->[0] = "Yummy"; 556 is ($name1->[0], "Yummy", 'Accessing via the correct name works'); 557 is ($name2->[0], undef, 558 'Accessing via a different NUL-containing name gives nothing'); 559 ok (defined $name1->[0], 'defined via the correct name works'); 560 ok (!defined$name2->[0], 561 'defined via a different NUL-containing name gives nothing'); 562 563 my (undef, $one) = @{$name1}[2,3]; 564 my (undef, $two) = @{$name2}[2,3]; 565 is ($one, undef, 'Nothing before we start (array slices)'); 566 is ($two, undef, 'Nothing before we start'); 567 @{$name1}[2,3] = ("Very", "Yummy"); 568 (undef, $one) = @{$name1}[2,3]; 569 (undef, $two) = @{$name2}[2,3]; 570 is ($one, "Yummy", 'Accessing via the correct name works'); 571 is ($two, undef, 572 'Accessing via a different NUL-containing name gives nothing'); 573 ok (defined $one, 'defined via the correct name works'); 574 ok (!defined $two, 575 'defined via a different NUL-containing name gives nothing'); 576 577 is ($name1->{PWOF}, undef, 'Nothing before we start (hashes)'); 578 is ($name2->{PWOF}, undef, 'Nothing before we start'); 579 $name1->{PWOF} = "Yummy"; 580 is ($name1->{PWOF}, "Yummy", 'Accessing via the correct name works'); 581 is ($name2->{PWOF}, undef, 582 'Accessing via a different NUL-containing name gives nothing'); 583 ok (defined $name1->{PWOF}, 'defined via the correct name works'); 584 ok (!defined $name2->{PWOF}, 585 'defined via a different NUL-containing name gives nothing'); 586 587 my (undef, $one) = @{$name1}{'SNIF', 'BEEYOOP'}; 588 my (undef, $two) = @{$name2}{'SNIF', 'BEEYOOP'}; 589 is ($one, undef, 'Nothing before we start (hash slices)'); 590 is ($two, undef, 'Nothing before we start'); 591 @{$name1}{'SNIF', 'BEEYOOP'} = ("Very", "Yummy"); 592 (undef, $one) = @{$name1}{'SNIF', 'BEEYOOP'}; 593 (undef, $two) = @{$name2}{'SNIF', 'BEEYOOP'}; 594 is ($one, "Yummy", 'Accessing via the correct name works'); 595 is ($two, undef, 596 'Accessing via a different NUL-containing name gives nothing'); 597 ok (defined $one, 'defined via the correct name works'); 598 ok (!defined $two, 599 'defined via a different NUL-containing name gives nothing'); 600 601 $name1 = "Left"; $name2 = "Left\0Right"; 602 my $glob2 = *{$name2}; 603 604 is ($glob1, undef, "We get different typeglobs. In fact, undef"); 605 606 *{$name1} = sub {"One"}; 607 *{$name2} = sub {"Two"}; 608 609 is (&{$name1}, "One"); 610 is (&{$name2}, "Two"); 611} 612 613# test derefs after list slice 614 615is ( ({foo => "bar"})[0]{foo}, "bar", 'hash deref from list slice w/o ->' ); 616is ( ({foo => "bar"})[0]->{foo}, "bar", 'hash deref from list slice w/ ->' ); 617is ( ([qw/foo bar/])[0][1], "bar", 'array deref from list slice w/o ->' ); 618is ( ([qw/foo bar/])[0]->[1], "bar", 'array deref from list slice w/ ->' ); 619is ( (sub {"bar"})[0](), "bar", 'code deref from list slice w/o ->' ); 620is ( (sub {"bar"})[0]->(), "bar", 'code deref from list slice w/ ->' ); 621 622# deref on empty list shouldn't autovivify 623{ 624 local $@; 625 eval { ()[0]{foo} }; 626 like ( "$@", qr/Can't use an undefined value as a HASH reference/, 627 "deref of undef from list slice fails" ); 628} 629 630# test dereferencing errors 631{ 632 format STDERR = 633. 634 my $ref; 635 foreach $ref (*STDOUT{IO}, *STDERR{FORMAT}) { 636 eval q/ $$ref /; 637 like($@, qr/Not a SCALAR reference/, "Scalar dereference"); 638 eval q/ @$ref /; 639 like($@, qr/Not an ARRAY reference/, "Array dereference"); 640 eval q/ %$ref /; 641 like($@, qr/Not a HASH reference/, "Hash dereference"); 642 eval q/ &$ref /; 643 like($@, qr/Not a CODE reference/, "Code dereference"); 644 } 645 646 $ref = *STDERR{FORMAT}; 647 eval q/ *$ref /; 648 like($@, qr/Not a GLOB reference/, "Glob dereference"); 649 650 $ref = *STDOUT{IO}; 651 eval q/ *$ref /; 652 is($@, '', "Glob dereference of PVIO is acceptable"); 653 654 is($ref, *{$ref}{IO}, "IO slot of the temporary glob is set correctly"); 655} 656 657# these will segfault if they fail 658 659my $pvbm = PVBM; 660my $rpvbm = \$pvbm; 661 662ok (!eval { *$rpvbm }, 'PVBM ref is not a GLOB ref'); 663ok (!eval { *$pvbm }, 'PVBM is not a GLOB ref'); 664ok (!eval { $$pvbm }, 'PVBM is not a SCALAR ref'); 665ok (!eval { @$pvbm }, 'PVBM is not an ARRAY ref'); 666ok (!eval { %$pvbm }, 'PVBM is not a HASH ref'); 667ok (!eval { $pvbm->() }, 'PVBM is not a CODE ref'); 668ok (!eval { $rpvbm->foo }, 'PVBM is not an object'); 669 670# bug 24254 671is( runperl(stderr => 1, prog => 'map eval qq(exit),1 for 1'), ""); 672is( runperl(stderr => 1, prog => 'eval { for (1) { map { die } 2 } };'), ""); 673is( runperl(stderr => 1, prog => 'for (125) { map { exit } (213)}'), ""); 674my $hushed = $^O eq 'VMS' ? 'use vmsish qw(hushed);' : ''; 675is( runperl(stderr => 1, prog => $hushed . 'map die,4 for 3'), "Died at -e line 1.\n"); 676is( runperl(stderr => 1, prog => $hushed . 'grep die,4 for 3'), "Died at -e line 1.\n"); 677is( runperl(stderr => 1, prog => $hushed . 'for $a (3) {@b=sort {die} 4,5}'), "Died at -e line 1.\n"); 678 679# bug 57564 680is( runperl(stderr => 1, prog => 'my $i;for $i (1) { for $i (2) { } }'), ""); 681 682# The mechanism for freeing objects in globs used to leave dangling 683# pointers to freed SVs. To test this, we construct this nested structure: 684# GV => blessed(AV) => RV => GV => blessed(SV) 685# all with a refcnt of 1, and hope that the second GV gets processed first 686# by do_clean_named_objs. Then when the first GV is processed, it mustn't 687# find anything nasty left by the previous GV processing. 688# The eval is stop things in the main body of the code holding a reference 689# to a GV, and the print at the end seems to bee necessary to ensure 690# the correct freeing order of *x and *y (no, I don't know why - DAPM). 691 692is (runperl( 693 prog => 'eval q[bless \@y; bless \$x; $y[0] = \*x; $z = \*y; ]; ' 694 . 'delete $::{x}; delete $::{y}; print qq{ok\n};', 695 stderr => 1), 696 "ok\n", 'freeing freed glob in global destruction'); 697 698 699# Test undefined hash references as arguments to %{} in boolean context 700# [perl #81750] 701{ 702 no strict 'refs'; 703 eval { my $foo; %$foo; }; ok !$@, '%$undef'; 704 eval { my $foo; scalar %$foo; }; ok !$@, 'scalar %$undef'; 705 eval { my $foo; !%$foo; }; ok !$@, '!%$undef'; 706 eval { my $foo; if ( %$foo) {} }; ok !$@, 'if ( %$undef) {}'; 707 eval { my $foo; if (!%$foo) {} }; ok !$@, 'if (!%$undef) {}'; 708 eval { my $foo; unless ( %$foo) {} }; ok !$@, 'unless ( %$undef) {}'; 709 eval { my $foo; unless (!%$foo) {} }; ok !$@, 'unless (!%$undef) {}'; 710 eval { my $foo; 1 if %$foo; }; ok !$@, '1 if %$undef'; 711 eval { my $foo; 1 if !%$foo; }; ok !$@, '1 if !%$undef'; 712 eval { my $foo; 1 unless %$foo; }; ok !$@, '1 unless %$undef;'; 713 eval { my $foo; 1 unless ! %$foo; }; ok !$@, '1 unless ! %$undef'; 714 eval { my $foo; %$foo ? 1 : 0; }; ok !$@, ' %$undef ? 1 : 0'; 715 eval { my $foo; !%$foo ? 1 : 0; }; ok !$@, '!%$undef ? 1 : 0'; 716} 717 718# RT #88330 719# Make sure that a leaked thinggy with multiple weak references to 720# it doesn't trigger a panic with multiple rounds of global cleanup 721# (Perl_sv_clean_all). 722 723SKIP: { 724 skip_if_miniperl('no Scalar::Util under miniperl', 4); 725 726 local $ENV{PERL_DESTRUCT_LEVEL} = 2; 727 728 # we do all permutations of array/hash, 1ref/2ref, to account 729 # for the different way backref magic is stored 730 731 fresh_perl_is(<<'EOF', 'ok', { stderr => 1 }, 'array with 1 weak ref'); 732use Scalar::Util qw(weaken); 733my $r = []; 734Internals::SvREFCNT(@$r, 9); 735my $r1 = $r; 736weaken($r1); 737print "ok"; 738EOF 739 740 fresh_perl_is(<<'EOF', 'ok', { stderr => 1 }, 'array with 2 weak refs'); 741use Scalar::Util qw(weaken); 742my $r = []; 743Internals::SvREFCNT(@$r, 9); 744my $r1 = $r; 745weaken($r1); 746my $r2 = $r; 747weaken($r2); 748print "ok"; 749EOF 750 751 fresh_perl_is(<<'EOF', 'ok', { stderr => 1 }, 'hash with 1 weak ref'); 752use Scalar::Util qw(weaken); 753my $r = {}; 754Internals::SvREFCNT(%$r, 9); 755my $r1 = $r; 756weaken($r1); 757print "ok"; 758EOF 759 760 fresh_perl_is(<<'EOF', 'ok', { stderr => 1 }, 'hash with 2 weak refs'); 761use Scalar::Util qw(weaken); 762my $r = {}; 763Internals::SvREFCNT(%$r, 9); 764my $r1 = $r; 765weaken($r1); 766my $r2 = $r; 767weaken($r2); 768print "ok"; 769EOF 770 771} 772 773SKIP:{ 774 skip_if_miniperl "no Scalar::Util on miniperl", 1; 775 my $error; 776 *hassgropper::DESTROY = sub { 777 require Scalar::Util; 778 eval { Scalar::Util::weaken($_[0]) }; 779 $error = $@; 780 # This line caused a crash before weaken refused to weaken a 781 # read-only reference: 782 $do::not::overwrite::this = $_[0]; 783 }; 784 my $xs = bless [], "hassgropper"; 785 undef $xs; 786 like $error, qr/^Modification of a read-only/, 787 'weaken refuses to weaken a read-only ref'; 788 # Now that the test has passed, avoid sabotaging global destruction: 789 undef *hassgropper::DESTROY; 790 undef $do::not::overwrite::this; 791} 792 793 794is ref( bless {}, "nul\0clean" ), "nul\0clean", "ref() is nul-clean"; 795 796# Test constants and references thereto. 797for (3) { 798 eval { $_ = 4 }; 799 like $@, qr/^Modification of a read-only/, 800 'assignment to value aliased to literal number'; 801 eval { ${\$_} = 4 }; 802 like $@, qr/^Modification of a read-only/, 803 'refgen does not allow assignment to value aliased to literal number'; 804} 805for ("4eounthouonth") { 806 eval { $_ = 4 }; 807 like $@, qr/^Modification of a read-only/, 808 'assignment to value aliased to literal string'; 809 eval { ${\$_} = 4 }; 810 like $@, qr/^Modification of a read-only/, 811 'refgen does not allow assignment to value aliased to literal string'; 812} 813{ 814 my $aref = \123; 815 is \$$aref, $aref, 816 '[perl #109746] referential identity of \literal under threads+mad' 817} 818 819# Bit of a hack to make test.pl happy. There are 3 more tests after it leaves. 820$test = curr_test(); 821curr_test($test + 3); 822# test global destruction 823 824my $test1 = $test + 1; 825my $test2 = $test + 2; 826 827package FINALE; 828 829{ 830 $ref3 = bless ["ok $test2\n"]; # package destruction 831 my $ref2 = bless ["ok $test1\n"]; # lexical destruction 832 local $ref1 = bless ["ok $test\n"]; # dynamic destruction 833 1; # flush any temp values on stack 834} 835 836DESTROY { 837 print $_[0][0]; 838} 839 840