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

I'm working with a large Perl application that has a couple of different run modes. One of these modes is a full-bore mode and it needs to call in lots of modules. Another mode is much simpler (think of it as read-only) and doesn't require nearly as many modules.

I've looked in the Perl man pages, looked in the Camel, googled a little and used Super Search here on PM but haven't yet come across anything that will help me reduce the memory footprint (by not pulling in modules that aren't used).

The only solution that I can think of is to only use the modules if we're running in the 'full-bore' mode. Does that make sense? Any other suggestions, comments?

Alex / talexb / Toronto

"Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Replies are listed 'Best First'.
Re: Reducing Perl's memory footprint depending on run mode
by BrowserUk (Patriarch) on Apr 21, 2005 at 15:27 UTC

    Take a look at If


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco.
    Rule 1 has a caveat! -- Who broke the cabal?
Re: Reducing Perl's memory footprint depending on run mode
by cees (Curate) on Apr 21, 2005 at 15:56 UTC

    Take a look at autouse or Class::Autouse which will define placeholder methods for all the modules you autouse, and then load the real module only if those methods are called.

    - Cees

Re: Reducing Perl's memory footprint depending on run mode
by dragonchild (Archbishop) on Apr 21, 2005 at 15:06 UTC
    Alternately, have a different statup.pl for each mode, containing all the appropriate use statements. Then, you require the appropriate one at startup in a BEGIN block, based on some configuration setting.
Re: Reducing Perl's memory footprint depending on run mode
by ides (Deacon) on Apr 21, 2005 at 14:55 UTC
    What you can do is determine at runtime which mode you're in and load the modules based on an if statement in a BEGIN {} block. Such as:
    BEGIN: { if( $ENV{fullbore} ) { use Big::Module1; use Big::Module2; use Big::Module3; } else { use Small::Module1; } }

    Frank Wiles <frank@wiles.org>
    http://www.wiles.org

      Er no; all those uses need to be requires, plus possible calls to import. Or eval 'use ...'

      Dave.

      I think you'd still have to make those require() instead of use(). Otherwise, they will all get run before evaluating the if statement, even in a BEGIN block.
        Yup you guys are right, they needed to have been requires not uses. That's what I get for replying without testing my code. :)

        Frank Wiles <frank@wiles.org>
        http://www.wiles.org

      This is not going to work as written. use statements get executed in the compilation phase. if statements are executed at runtime. Even inside a BEGIN block the code gets compiled first before being executed.

      The following might work:

      BEGIN { if (completely_boring()) { eval <<USE; use Module::A; use Module::B; use Module::Etc; USE } else { eval "use Module::C"; } die $@ if $@; }

      Putting the use statements inside an eval postpones their evaluation till runtime. This only works if the code is in a string rathern than a block. I've also used this trick to set up END blocks conditionally.

      I vaguely recall that there might be a CPAN module that would let you do this more cleanly, but don't quote me on that.

      --DrWhy

      "If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."