I have a package FOO, where inside works a Safe compartment (from Safe.pm), and FOO need to be cleaned to be reused. All the stufs to make this need to be pure Perl and need to work from Perl 5.6.0+!
To do that I used Devel::Symdump to get the symbols table and created this function:
There is a better way or module to do that?!use Devel::Symdump ; my %clean_types = ( 'packages' => '::' , 'scalars' => '$' , 'arrays' => '@' , 'hashes' => '%' , 'functions' => '&' , 'ios' => '*' , ) ; my @clean_order = qw(scalars arrays hashes functions ios packages) ; sub clean_pack { my ( $pack ) = @_ ; my $dump = Devel::Symdump->rnew($pack); my %glob ; foreach my $i ( @clean_order ) { my $type = $clean_types{$i} ; my @symbols = $dump->$i ; foreach my $name ( @symbols ) { #print "$type$name\n" ; if ($type eq '\$') { undef $$name ;} elsif ($type eq '\@') { undef @$name ;} elsif ($type eq '\%') { undef %$name ;} elsif ($type eq '\&') { undef &$name ;} elsif ($type eq '\*') { undef *$name ;} elsif ($type eq '::') { $glob{"$name\::"} = 1 ;} $glob{$name} = 1 ; } } foreach my $Key ( reverse sort keys %glob ) { undef *$Key ;} }
I really need to reuse the package to run the Safe compartment again! 1- because I need to clean the memory, 2- because I can't use a different name for the package.
My method works fine, but I think that Perl don't clean 100% of the memory! It cleans all the variables, functions, *ios and packages, but some bytes are still in use!
Graciliano M. P.
"The creativity is the expression of the liberty".
In reply to better way to cleanup a package? by gmpassos
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |