I'd swear I've asked this question, but since I can't find the post, here's hoping I didn't :)

Basically, deleting subroutines from a symbol table seems a bit buggy, but I don't know if this behavior is documented or not.

#!/usr/bin/perl -l sub this { 'this' } print this; undef &main::this; # undefine the code slot print main->can('this') ? 'main->can("this")' : '! main->can("this")'; eval {print this}; print $@; undef *main::this; # undefine the entire glob print main->can('this') ? 'main->can("this")' : '! main->can("this")'; __END__ # result this main->can("this") Undefined subroutine &main::this called at sub.pl line 7. ! main->can("this")

The first time I try to remove the subroutine via undef it still leaves a CODE slot in the symbol table's *this glob. I can't just use delete because it's not an array or a hash (it fails if you try). This is bad in procedural code but it's awful in OO code because $object->can('this') will succeed even if the method has been deleted.

This leaves me with having to undefine the entire glob but that's a horrible solution because the other slots in the glob, if any, are blown away. Is this a known/documented bug/feature in Perl? Is there a workaround?

Cheers,
Ovid

New address of my CGI Course.


In reply to Removing subroutines from symbol tables by Ovid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.