1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
#Extra stuff
our $DEFAULT_UNLOAD_METHOD ||= "unload_package_pp";
sub unload_package {
goto &$DEFAULT_UNLOAD_METHOD;
}
sub unload_package_pp {
my $package = shift;
no strict 'refs';
my $tab = \%{ $package . '::' };
# below we assign to a symbol first before undef'ing it, to avoid
# nuking aliases. If we undef directly we may undef not only the
# alias but the original function as well
for (keys %$tab) {
#Skip sub stashes
next if /::$/;
my $fullname = join '::', $package, $_;
# code/hash/array/scalar might be imported make sure the gv
# does not point elsewhere before undefing each
if (%$fullname) {
*{$fullname} = {};
undef %$fullname;
}
if (@$fullname) {
*{$fullname} = [];
undef @$fullname;
}
if ($$fullname) {
my $tmp; # argh, no such thing as an anonymous scalar
*{$fullname} = \$tmp;
undef $$fullname;
}
if (defined &$fullname) {
no warnings;
local $^W = 0;
if (defined(my $p = prototype $fullname)) {
*{$fullname} = eval "sub ($p) {}";
}
else {
*{$fullname} = sub {};
}
undef &$fullname;
}
if (*{$fullname}{IO}) {
local $@;
eval {
if (fileno $fullname) {
close $fullname;
}
};
}
}
#Wipe from %INC
$package =~ s[::][/]g;
$package .= '.pm';
delete $INC{$package};
}
|