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);
}