Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Erasing Symbol Tables

by shotgunefx (Parson)
on Dec 04, 2002 at 11:12 UTC ( [id://217455]=CUFP: print w/replies, xml ) Need Help??

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."

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (1)
As of 2024-04-25 01:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found