ERASE_PACKAGE('Package::Name');
# Will clear out package FOO but would leave FOO::BAR intact.
ERASE_PACKAGE_RECURSIVE('Package::Name');
# Will clear out package FOO and FOO::BAR and FOO::BAR::BASE, etc.
Brought on by this node. Why would you want to do this? You would almost never want to. If you think you need this, you probably should think harder, but in the spirit of giving enough rope...
sub ERASE_PACKAGE { my $packname = shift; $packname .= '::' unless $packname =~ /::$/; no strict "refs"; my $package = *{$packname}{HASH}; return unless defined $package; undef *{$packname . $_} foreach (keys %$package); } sub ERASE_PACKAGE_RECURSIVE { # Call with "Package::Name" my $packname = shift; $packname .= '::' unless $packname =~ /::$/; no strict "refs"; my $package = *{$packname}{HASH}; return unless defined $package; $_=~/::$/ ? ERASE_PACKAGE($packname . $_) : undef *{$packname . $_} +foreach (keys %$package); }

Replies are listed 'Best First'.
Re: Erasing Symbol Tables
by broquaint (Abbot) on Dec 04, 2002 at 12:01 UTC
    And for those who have a tiny C&P buffer
    sub empty_table { delete @{"$_[0]::"}{grep !/\b::\z/, keys %{"$_[0]::"}}; }

    _________
    broquaint

      scary ;)

      -Lee

      "To be civilized is to deny one's nature."
Re: Erasing Symbol Tables
by adrianh (Chancellor) on Dec 04, 2002 at 11:52 UTC

    There's also 'How do I clear a package?' in perlfaq7, and the handy Symbol::delete_package.

    I have actually come across a vaguely sane reason for doing this. When I was writing a test script for a package with persistant class data it was easier to clear the package out and reload the class, rather than run multiple test scripts.

      Agreed. It's one of the few reasons that makes sense.

      -Lee

      "To be civilized is to deny one's nature."