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:
c.f.my $bool = defined &{ 'foo::bar' };
In fact, the following causes autovivification:my $namespace = do { no strict 'refs'; \%{"foo::"} }; my $bool = exists $namespace->{bar} && defined *{ $namespace->{bar} }{ +CODE};
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' }.no strict 'refs'; my $before = exists $foo::{bar}; # false my $bool = defined *{ 'foo::bar' }{CODE}; my $after = exists $foo::{bar}; # true
c.f.undef &{ 'foo::bar' }; no strict 'refs'; *{ 'foo::bar' } = sub {};
By the way, the following should work:no strict 'refs'; no warnings 'redefine'; *{ 'foo::bar' } = sub {};
Thanks for your comment :)my $coderef = \&{ 'subroutine_name' }; #undef $$coderef; # typo ? undef &{ $coderef };
|
---|
In Section
Seekers of Perl Wisdom