Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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

by anazawa (Beadle)
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' };
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 :)


Comment on Re^2: undef/defined &{$name} while "strict refs"
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2014-04-21 06:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (492 votes), past polls