Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

undefing *foo{CODE} does not fully work

by jesuashok (Curate)
on Apr 13, 2006 at 03:49 UTC ( #542988=perlquestion: print w/replies, xml ) Need Help??

jesuashok has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: undefing *foo{CODE} does not fully work
by liverpole (Monsignor) on Oct 06, 2006 at 12:08 UTC
    Posted to this site on September 9, 2005:
    ----------------------------------------------------------------- [Please enter your report here] This problem has been seen on Perl 5.8.3 and 5.8.7. I assume that it happens under other versions as well. If you try to undef the CODE slot of a glob, it only halfway works. ref(*foo{CODE}) will still show that it is a code reference, and UNIVERSAL::can still shows that it is a code reference. This bug causes problems when Apache::Reload and Class::DBI are used together. The following code sample demonstrates what I am talking about -------- #! /usr/bin/perl -l print "Originally foo() is a " . ref(*foo{CODE}); print "Initializing foo()"; *foo = sub {print "hello"}; print "foo() is now a " . ref(*foo{CODE}); print "According to can, foo is " . main->can("foo"); print "Calling foo()"; foo(); print "Undefining foo()"; $x = *foo; undef &$x; print "foo() is now a " . ref(*foo{CODE}); print "According to can, foo is " . main->can("foo"); print "Calling foo()"; foo(); __END__ Originally foo() is a Initializing foo() foo() is now a CODE According to can, foo is CODE(0x8175b24) Calling foo() hello Undefining foo() foo() is now a CODE According to can, foo is CODE(0x8175b24) Calling foo()

    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: undefing *foo{CODE} does not fully work
by ikegami (Patriarch) on Apr 13, 2006 at 05:56 UTC

    I think you can't do it because of stubs

    sub foo; # stub print("\&foo is ", defined &foo ? 'defined' : 'not defined', "\n"); print(__PACKAGE__->can('foo') ? 'Can' : 'Can\'t', " foo\n"); foo(); __END__ &foo is not defined Can foo Undefined subroutine &main::foo called.

    Undefining the CODE slot removes the sub there, but not the stub. Apparently, defined checks for the sub, while can checks for the stub.

Re: undefing *foo{CODE} does not fully work
by brian_d_foy (Abbot) on Apr 13, 2006 at 04:18 UTC

    It seems to work if you undefine *foo instead of undefining $x. If you want to undefine something, you have to undefine it (and not undefine something else).

    print "Undefining foo()"; undef *foo; print "Foo is not defined" unless defined &foo;
    --
    brian d foy <brian@stonehenge.com>
    Subscribe to The Perl Review

      While that and works, it also undefines @foo, %foo, etc. It seems to me that the OP is trying to undefined &foo specifically.

      Other working solutions with the same caveat:

      my $x = \*foo; undef *$x;

      and

      my $x = 'foo'; { no strict 'refs'; undef *$x; }

      and

      my $x = do { no strict 'refs'; \*{'foo'} }; undef *$x;
    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2022-08-08 22:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?