in reply to better way to cleanup a package?

If you want to clear out a package, you could do the following...
sub ERASE_PACKAGE { # Call with "Package::Name" my $packname = shift; $packname .= '::' unless $packname =~ /::$/; no strict "refs"; warn $packname; my $package = *{$packname}{HASH}; return unless defined $package; undef *{$packname . $_} foreach (keys %$package); }
Many caveats. It's not good for unusing a module. See the above comments about @INC. Also it won't remove exported symbols. So if you export function Pack::Foo::foo(), the symbol table that it was exported to will still have foo() available after it's been erased.

It can be useful for clearing out variables. I would only recommend it for situations such as that.

-Lee

"To be civilized is to deny one's nature."

Replies are listed 'Best First'.
Re: Re: better way to cleanup a package?
by gmpassos (Priest) on Dec 03, 2002 at 19:27 UTC
    The problem with this way is the sub packages, and you don't clean the memory very well!
    Before to do a
    undef *{"scalarvar"}
    you need to do
    undef ${"scalarvar"}
    or the memory are not cleaned very well!

    And a package are only cleaned with:
    undef *{"packname::"}
    not with:
    undef *{"packname"}
    But you need to clean the variables and functions inside before!

    I made some tests of the reuse of 100 times of a package, with a randome variable with 50Mb, and it don't use more than 60Mb in the memory. But if I only do a undef *{"foovar"}, without undef ${"foovar"} first, the process use 100Mb!

    Graciliano M. P.
    "The creativity is the expression of the liberty".

      My tests show no difference in memory usage between undef *{"scalarvar"} and first undef ${"scalarvar"}. Perhaps you had a memory leak? My understanding is that Perl won't return the memory to the OS, just make it available to itself when freed. (Until the process terminates of course.)

      As far as not clearing out sub packages, I think of that as a feature. It would be rare enough to want to clear out a package in this manner, so my feeling is that if you would want clear out sub-packages (which may or may not have a relationship to the package being deleted) you should be explicit.

      -Lee

      "To be civilized is to deny one's nature."