http://www.perlmonks.org?node_id=988837


in reply to Re: undef/defined &{$name} while "strict refs"
in thread undef/defined &{$name} while "strict refs"

Your suggestion is insightful. We need to manipulate symbol table carefully. Though I was under the impression that the following causes autovivification, it doesn't:

my $bool = defined &{ 'foo::bar' };
c.f.
my $namespace = do { no strict 'refs'; \%{"foo::"} }; my $bool = exists $namespace->{bar} && defined *{ $namespace->{bar} }{ +CODE};
In fact, the following causes autovivification:
no strict 'refs'; my $before = exists $foo::{bar}; # false my $bool = defined *{ 'foo::bar' }{CODE}; my $after = exists $foo::{bar}; # true
I misunderstood defined &{ 'foo::bar' } cause autovivification. That's why you think defined &{ 'foo::bar' } is safe, right?

I agree the name of the subroutine has to be undefined. We are allowed to avoid "no warnings redefine" when we use undef &{ 'foo::bar' }.
undef &{ 'foo::bar' }; no strict 'refs'; *{ 'foo::bar' } = sub {};
c.f.
no strict 'refs'; no warnings 'redefine'; *{ 'foo::bar' } = sub {};
By the way, the following should work:
my $coderef = \&{ 'subroutine_name' }; #undef $$coderef; # typo ? undef &{ $coderef };
Thanks for your comment :)