Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
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:

Hi great people,

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.

-------- #! /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() Not a CODE reference at - line 17 --------

The way that this comes into play in the case that Module1 attempts to undefine subroutines that have been defined, but when Module2 tries to check whether they are there it is told that they are, with disasterous results. what is should I do ?

"Keep pouring your ideas"

2006-10-07 Unapproved by planetscape once evidence of habitual plagiarism uncovered.

Comment on undefing *foo{CODE} does not fully work
Download Code
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
      Hi

      $x = *foo; undef &$x;
      Is the above statement is wrong ? If so, give me your valuable inputs, so that I can be on safer side from my other developers.
      Because I was the one who has done this in the initial stage.

      "Keep pouring your ideas"

      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;
Re: undefing *foo{CODE} does not fully work
by ikegami (Pope) 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 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$..$/

Log In?
Username:
Password:

What's my password?
Create A New User
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 musing on the Monastery: (5)
As of 2014-12-27 14:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls