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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (11)
As of 2017-07-26 14:45 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (396 votes). Check out past polls.