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

Which below is generally more efficient considering that the directory with the modules has perhaps 15-20 custom modules? And also, are the used modules loaded dynamically or is everything in the 'lib' directory compiled at runtime?
use lib '/path/to/modules'; use CustomMod1; use CustomMod2; ...etc. -or- require '/path/to/modules/CustomMod1.pm'; require '/path/to/modules/CustomMod2.pm';

And as a side question, are modules more efficient than libraries (if so why?) Thanks from a novice!

Replies are listed 'Best First'.
Re: Efficiently loading modules
by arturo (Vicar) on May 10, 2001 at 19:25 UTC

    use and require do different things, so there really is no comparison here. use is a compile-time directive, while require is a run-time directive (happens after the script starts executing).

    In addition, use Foo imports all of the symbols (variables, subroutines, etc.) that the module exports, while require doesn't.

    I doubt there's that much of a speed difference when you add in the code that would make the second method do what the first does. The first is also *way* easier to maintain.

    Your best optimizations in this area come with using only the modules you *need* and importing only the symbols you *need*.

    HTH!

    perl -e 'print "How sweet does a rose smell? "; chomp $n = <STDIN>; $r +ose = "smells sweet to degree $n"; *other_name = *rose; print "$other +_name\n"'
Re: Efficiently loading modules
by merlyn (Sage) on May 10, 2001 at 19:25 UTC
    The differences in speed will be down in the noise level, and the use permits prototyped subroutines to have an influence on the following compilation. So all other things being equal, use the use, luke!

    -- Randal L. Schwartz, Perl hacker

Re: Efficiently loading modules
by petdance (Parson) on May 10, 2001 at 19:53 UTC
    #include <standard_efficiency_screed.h>

    Does it matter which is "more efficient"? Are you having a speed problem? Have you run anything thru Benchmark?

    Dijkstra says "Premature optimization is the root of all evil". My corrollary is that "'Unnecessary' is, by definition, premature."

    xoxo,
    Andy

    %_=split/;/,".;;n;u;e;ot;t;her;c; ".   #   Andy Lester
    'Perl ;@; a;a;j;m;er;y;t;p;n;d;s;o;'.  #   http://petdance.com
    "hack";print map delete$_{$_},split//,q<   andy@petdance.com   >