in reply to Reducing Perl's memory footprint depending on run mode

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

Replies are listed 'Best First'.
Re^2: Reducing Perl's memory footprint depending on run mode
by dave_the_m (Monsignor) on Apr 21, 2005 at 15:12 UTC
    Er no; all those uses need to be requires, plus possible calls to import. Or eval 'use ...'

    Dave.

Re^2: Reducing Perl's memory footprint depending on run mode
by perrin (Chancellor) on Apr 21, 2005 at 15:13 UTC
    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

Re^2: Reducing Perl's memory footprint depending on run mode
by DrWhy (Chaplain) on Apr 21, 2005 at 15:16 UTC
    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..."