This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix untimely destruction introduced by lvalue ops [RT#67838] by returning a TEMP...
[perl5.git] / t / op / stash.t
index 578f937..2c17022 100644 (file)
@@ -7,12 +7,12 @@ BEGIN {
 
 BEGIN { require "./test.pl"; }
 
-plan( tests => 31 );
+plan( tests => 38 );
 
 # Used to segfault (bug #15479)
-fresh_perl_is(
+fresh_perl_like(
     '%:: = ""',
-    'Odd number of elements in hash assignment at - line 1.',
+    qr/Odd number of elements in hash assignment at - line 1\./,
     { switches => [ '-w' ] },
     'delete $::{STDERR} and print a warning',
 );
@@ -25,14 +25,17 @@ fresh_perl_is(
     q(Insert a non-GV in a stash, under warnings 'once'),
 );
 
-ok( !defined %oedipa::maas::, q(stashes aren't defined if not used) );
-ok( !defined %{"oedipa::maas::"}, q(- work with hard refs too) );
+{
+    no warnings 'deprecated';
+    ok( defined %oedipa::maas::, q(stashes happen to be defined if not used) );
+    ok( defined %{"oedipa::maas::"}, q(- work with hard refs too) );
 
-ok( defined %tyrone::slothrop::, q(stashes are defined if seen at compile time) );
-ok( defined %{"tyrone::slothrop::"}, q(- work with hard refs too) );
+    ok( defined %tyrone::slothrop::, q(stashes are defined if seen at compile time) );
+    ok( defined %{"tyrone::slothrop::"}, q(- work with hard refs too) );
 
-ok( defined %bongo::shaftsbury::, q(stashes are defined if a var is seen at compile time) );
-ok( defined %{"bongo::shaftsbury::"}, q(- work with hard refs too) );
+    ok( defined %bongo::shaftsbury::, q(stashes are defined if a var is seen at compile time) );
+    ok( defined %{"bongo::shaftsbury::"}, q(- work with hard refs too) );
+}
 
 package tyrone::slothrop;
 $bongo::shaftsbury::scalar = 1;
@@ -53,19 +56,20 @@ package main;
 
 # now tests in eval
 
-ok( !eval  { defined %achtfaden:: },   'works in eval{}' );
-ok( !eval q{ defined %schoenmaker:: }, 'works in eval("")' );
+ok( eval  { no warnings 'deprecated'; defined %achtfaden:: },   'works in eval{}' );
+ok( eval q{ no warnings 'deprecated'; defined %schoenmaker:: }, 'works in eval("")' );
 
 # now tests with strictures
 
 {
     use strict;
-    ok( !defined %pig::, q(referencing a non-existent stash doesn't produce stricture errors) );
+    no warnings 'deprecated';
+    ok( defined %pig::, q(referencing a non-existent stash doesn't produce stricture errors) );
     ok( !exists $pig::{bodine}, q(referencing a non-existent stash element doesn't produce stricture errors) );
 }
 
 SKIP: {
-    eval { require B; 1 } or skip "no B", 12;
+    eval { require B; 1 } or skip "no B", 18;
 
     *b = \&B::svref_2object;
     my $CVf_ANON = B::CVf_ANON();
@@ -106,55 +110,34 @@ SKIP: {
     is( eval { $gv->NAME }, "__ANON__", "...and an __ANON__ name");
     is( eval { $gv->STASH->NAME }, "__ANON__", "...and an __ANON__ stash");
 
-    TODO: {
-        local $TODO = "anon CVs not accounted for yet";
-
-        my @results = split "\n", runperl
-            switches    => [ "-MB", "-l" ],
-            stderr      => 1,
-            prog        => q{
-                my $sub = do {
-                    package four;
-                    sub { 1 };
-                };
-                %four:: = ();
-
-                my $gv = B::svref_2object($sub)->GV;
-                print $gv->isa(q/B::GV/) ? q/ok/ : q/not ok/;
-
-                my $st = eval { $gv->STASH->NAME };
-                print $st eq q/__ANON__/ ? q/ok/ : q/not ok/;
-
-                my $sub = do {
-                    package five;
-                    sub { 1 };
-                };
-                undef %five::;
-
-                $gv = B::svref_2object($sub)->GV;
-                print $gv->isa(q/B::GV/) ? q/ok/ : q/not ok/;
-
-                $st = eval { $gv->STASH->NAME };
-                print $st eq q/__ANON__/ ? q/ok/ : q/not ok/;
-
-                print q/done/;
-            };
-
-        ok( @results == 5 && $results[4] eq "done",
-            "anon CVs in undefed stash don't segfault" )
-            or todo_skip $TODO, 4;
-
-        ok( $results[0] eq "ok", 
-            "cleared stash leaves anon CV with valid GV");
-        ok( $results[1] eq "ok",
-            "...and an __ANON__ stash");
-            
-        ok( $results[2] eq "ok", 
-            "undefed stash leaves anon CV with valid GV");
-        ok( $results[3] eq "ok",
-            "...and an __ANON__ stash");
+    my $sub = do {
+       package four;
+       sub { 1 };
+    };
+    %four:: = ();
+
+    my $gv = B::svref_2object($sub)->GV;
+    ok($gv->isa(q/B::GV/), "cleared stash leaves anon CV with valid GV");
+
+    my $st = eval { $gv->STASH->NAME };
+    { local $TODO = 'STASHES not anonymized';
+       is($st, q/__ANON__/, "...and an __ANON__ stash");
+    }
+
+    my $sub = do {
+       package five;
+       sub { 1 };
+    };
+    undef %five::;
+
+    $gv = B::svref_2object($sub)->GV;
+    ok($gv->isa(q/B::GV/), "undefed stash leaves anon CV with valid GV");
+
+    $st = eval { $gv->STASH->NAME };
+    { local $TODO = 'STASHES not anonymized';
+       is($st, q/__ANON__/, "...and an __ANON__ stash");
     }
-    
+
     # [perl #58530]
     fresh_perl_is(
         'sub foo { 1 }; use overload q/""/ => \&foo;' .
@@ -163,4 +146,66 @@ SKIP: {
         {},
         "no segfault with overload/deleted stash entry [#58530]",
     );
+
+    # CvSTASH should be null on a named sub if the stash has been deleted
+    {
+       package FOO;
+       sub foo {}
+       my $rfoo = \&foo;
+       package main;
+       delete $::{'FOO::'};
+       my $cv = B::svref_2object($rfoo);
+       # (is there a better way of testing for NULL ?)
+       my $stash = $cv->STASH;
+       like($stash, qr/B::SPECIAL/, "NULL CvSTASH on named sub");
+    }
+
+    # on glob reassignment, orphaned CV should have anon CvGV
+
+    {
+       my $r;
+       eval q[
+           package FOO2;
+           sub f{};
+           $r = \&f;
+           *f = sub {};
+       ];
+       delete $FOO2::{f};
+       my $cv = B::svref_2object($r);
+       my $gv = $cv->GV;
+       ok($gv->isa(q/B::GV/), "orphaned CV has valid GV");
+       is($gv->NAME, '__ANON__', "orphaned CV has anon GV");
+    }
+
+    # deleting __ANON__ glob shouldn't break things
+
+    {
+       package FOO3;
+       sub named {};
+       my $anon = sub {};
+       my $named = eval q[\&named];
+       package main;
+       delete $FOO3::{named}; # make named anonymous
+
+       delete $FOO3::{__ANON__}; # whoops!
+       my ($cv,$gv);
+       $cv = B::svref_2object($named);
+       $gv = $cv->GV;
+       ok($gv->isa(q/B::GV/), "ex-named CV has valid GV");
+       is($gv->NAME, '__ANON__', "ex-named CV has anon GV");
+
+       $cv = B::svref_2object($anon);
+       $gv = $cv->GV;
+       ok($gv->isa(q/B::GV/), "anon CV has valid GV");
+       is($gv->NAME, '__ANON__', "anon CV has anon GV");
+    }
+}
+
+# make sure having a sub called __ANON__ doesn't confuse perl.
+
+{
+    my $c;
+    sub __ANON__ { $c = (caller(0))[3]; }
+    __ANON__();
+    is ($c, 'main::__ANON__', '__ANON__ sub called ok');
 }