Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re^2: undef/defined &{$name} while "strict refs"

by anazawa (Scribe)
on Aug 21, 2012 at 19:49 UTC ( #988837=note: print w/replies, xml ) Need Help??

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' };
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 {};
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 :)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://988837]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2016-10-21 09:58 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (288 votes). Check out past polls.