Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Is this a safe way to 'un'-require a module?

by stevieb (Canon)
on Sep 10, 2015 at 18:13 UTC ( #1141584=perlquestion: print w/replies, xml ) Need Help??

stevieb has asked for the wisdom of the Perl Monks concerning the following question:

Good day esteemed friends,

I'm writing a new module that will be long running. This module will have a function that requires code from another one of my modules (which loads 115+ other modules and dependencies), but this function will only be called on extremely rare situations so I don't want to use it.

I've written some mock-up (working) test code, and I'm just looking to see if this is a sane thing to do, and/or feedback on alternative approaches.

use strict; use warnings; use Symbol qw(delete_package); my @pre_require = keys %INC; print "Modules pre-require: " . @pre_require . "\n"; require Devel::Examine::Subs; import Devel::Examine::Subs; my @post_require = keys %INC; # do something with imported module immediately after # getting the post-require %INC count ... print "Modules post-require: " . @post_require . "\n"; my $count = @post_require - @pre_require; print "Additional modules loaded: $count\n"; for my $file (@post_require){ if (! grep /$file/, @pre_require){ (my $module = $file ) =~ s/\//::/g; $module =~ s/\.pm$//; delete_package $module; delete $INC{$file}; } } __END__ Modules pre-require: 4 Modules post-require: 138 Additional modules loaded: 134 Module count post cleanup: 4



Replies are listed 'Best First'.
Re: Is this a safe way to 'un'-require a module? (don't)
by tye (Sage) on Sep 10, 2015 at 18:36 UTC

    The loaded modules aren't likely consuming much of import. Rather than prematurely optimize, either figure out how to measure the impact or find something actually useful to spend your time on.

    I can see value in delaying the loading of such a large number of modules. But I find it hard to imagine how trying to unload them again would do anything other than consume more resources. The pages allocated to your process that your process doesn't touch are one of the least important resources in most situations are so are almost never worth trying to exert effort to optimize them.

    For going from 138 modules loaded down to 4, it may even be more efficient to use "exec( $^X, $0, @ARGV );" to do that instead. And that is also less likely to cause breakage when you try to re-load the modules. Though, I wouldn't even bother with that unless I could find evidence of some real problem.

    - tye        

Re: Is this a safe way to 'un'-require a module?
by jeffa (Bishop) on Sep 10, 2015 at 18:31 UTC

    I would wait until you know that this is causing an issue, premature optimization is the root of all evil. ;) Having said that, here's my take on the solution (given i did understand the problem correctly)

    use strict; use warnings; if (shift) { eval "use Data::Dumper"; } print Data::Dumper->Dump( [ 1 .. 10 ] );
    The obvious problem with this solution is you lose features from Autoload --- this approach works best with object oriented packages that require instantiation but you can still work around non OO packages. Most of the time ... ... these kinds of concerns tend to open cans of worms. :/


    (the triplet paradiddle with high-hat)
Re: Is this a safe way to 'un'-require a module?
by BrowserUk (Patriarch) on Sep 10, 2015 at 19:44 UTC
      I just had a birthday... I must be losing my mind in my old age :)

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1141584]
Approved by Old_Gray_Bear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2022-05-27 07:08 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (94 votes). Check out past polls.