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

Some of my (primarily CGI) scripts have subroutines that rarely get called. Things like error handling which (hopefully) never get run but which need to be there in case something unexpected happens.

Sometimes these infrequently used routines need to load a module or two. I'd just as soon not have to load these modules into memory every time the script runs unless I need to. So, I've begun using require and import rather than always using use.

My question is whether there are any real drawbacks to this, other than having to explictly import the features I want from the required module? Am I really saving any system resources, or am I just wasting time by typing that extra line of code?

Replies are listed 'Best First'.
(tye)Re: use vs. require
by tye (Sage) on Oct 20, 2000 at 00:25 UTC

    Yes, by switching from use to require and import, you are reducing start-up time and probably reducing memory utilization (unless those modules are getting required elsewhere) until that chunk of code actually gets run.

    The only thing you are losing is that the import doesn't happen until run time so any imported subroutines are not predeclared before the code that uses them is compiled. The practical consequences of this are that

    • you'll need to use parens to invoke the functions and
    • for any functions with prototypes, the prototypes will not be in effect for your code.

    For most modules, those two consequence are pretty minor.

            - tye (but my friends call me "Tye")
Re: use vs. require
by merlyn (Sage) on Oct 20, 2000 at 10:42 UTC
    require and import mean you do not get the function definitions at compile time, so prototypes aren't in place, and you'll get bareword errors if Perl can't figure out that it's a subroutine call.

    To fix all that, consider the pragma autouse. It prototypes the use'd functions, and sets up triggers to automatically use the module when the functions are called, transparently. Nice.

    -- Randal L. Schwartz, Perl hacker

Re: use vs. require
by Fastolfe (Vicar) on Oct 19, 2000 at 23:47 UTC
    I wonder if an eval("use Module"); would be more "correct" in cases where you may not wish to use a module until some optional point during your script's run...

    I think your method of requireing/importing infrequently used modules is perfectly sane, but we are of course making the assumption that this behavior adequately performs the same task as use at compile-time. So long as that's not violated in future versions of Perl, I see no problem...

Re: use vs. require
by Anonymous Monk on Oct 23, 2000 at 02:28 UTC

    One addendum to the existing answers. If you are running under mod_perl you will wants to 'use' all your modules during server startup rather than loading them as needed.

    The reason is that modules loaded before Apache forks off children will usually share a lot of the memory they use between the various child processes. If you require the module at run time, you will load a _full_ copy into every child that uses it.

    Unless those modules are used _extremely_ rarely, its generally going to be a memory loss under mod_perl.

    Of course, there is a nice clarity to being able to look at the top of a script/module and see all the modules it uses. Also, if a module isn't there, its nicer to get a compile time error rather a runtime error halfway through execution.

    Your approach is probably fine but I wouldn't want to do that myself unless I saw a really big impact.

    -dave