I'm looking for the best way to cleanup a package.

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:

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 ;} }
There is a better way or module to do that?!

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.