Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

loading a module only if you need it

by skazat (Hermit)
on Oct 09, 2000 at 20:34 UTC ( #35940=perlquestion: print w/ replies, xml ) Need Help??
skazat has asked for the wisdom of the Perl Monks concerning the following question:

here's a q:

I have a hefty cgi script, its about 125k, with about 5 modules attached to it, so load time is becoming somewhat an issue. the thing is, i don't always need some modules, would this work?

sub some_function { use THIS_MODULE; # yadda yadda } sub another_function { use THIS_MODULE; use ANOTHER_MODULE; #yadda yadda }

in other words, would it work to only call modules using `use` when i need them in a specific function? or would that not work. and would it really help load time, or would all the modules get loaded anyhoo at load time?

-justin simoni
!skazat!

Comment on loading a module only if you need it
Download Code
Re: loading a module only if you need it
by merlyn (Sage) on Oct 09, 2000 at 20:36 UTC
    Keep in mind that use is a compile-time operation. If you try to do it at runtime, you won't get the prototypes set at the proper time. Having said that, just change your use to a require and you're done!

    -- Randal L. Schwartz, Perl hacker


    update: yes, as tilly points out, you'll need to change
    use FOO;
    to
    require FOO; FOO->import();
    and
    use FOO qw(BAR);
    to
    require FOO; FOO->import(qw(BAR));
    Thanks for that! But remember, you still don't get prototypes.
      Not necessarily done. You may need to import foo(); if the modules are exporting things to your current namespace.
Re: loading a module only if you need it
by skazat (Hermit) on Oct 09, 2000 at 21:42 UTC

    hmm, not having protoypes is going to bite me in the end and i'll have a bug i won't know how to get rid of :)

    i think i'll focus on relinquishing common functions still in the main script of using global variables, and then move those functions to another module, and then split up whats left to smaller, bite sized scripts... possibly OO'ing some of the modules to get a standardized interface.

    which leads to another question. has anybody started a project with a small script, but after a while the script becomes unruly and large, so much as to question your whole structure and the only way to releive the problem is to dramatically change the sctructure? if so, how did ou go about this. above is my plan. anyone want to comment on this?

    -justin simoni
    !skazat!

RE: loading a module only if you need it
by extremely (Priest) on Oct 10, 2000 at 01:26 UTC

    I've seen something else on this site about eval'ing the use Xxxx, is that a real solution? ala:

    if ($needFancyCGI) { eval 'use CGI qw(:all)'; } else eval 'use CGI qw(:cgi)'; }

    I've had the privilege of always having the memory and CPU at levels where conditional includes were a non-issue (thankyouthankyouthankyou mod_perl) so I've never run into this need.

    --
    $you = new YOU;
    honk() if $you->love(perl)

      The danger of this is that it implies things that aren't true - namely that the subroutines are defined in the lines of code after this, and they aren't.

      For example, these two chunks of code do entirely opposite things:
      use CGI qw(:all); print hr, "Hello world", br, "How's life?", hr;
      eval 'use CGI qw(:all)'; print hr, "Hello world", br, "How's life?", hr;
      In fact, the latter won't compile under use strict, because we're using the hr and br barewords!

      Now, if the if statement were inside a BEGIN, that'd be better, but then most likely the variable would need to be set earlier, ever regressing into nothingness. It's turtles all the way down!

      -- Randal L. Schwartz, Perl hacker

        I knew there was a reason the hairs on the back of my neck stood on end when I wrote that code =) Of course, I tend not to import and call everything with $object->method just because I like the clarity of it. Sheesh, I'd have been bitten by that, but not until the pattern was well set! Still, it will work with this (frickin huge) caveat.

        Probably not recommended then, but useful.

        BTW, you mangled your </tt> tag there...

        --
        $you = new YOU;
        honk() if $you->love(perl)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://35940]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (11)
As of 2014-07-29 12:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (217 votes), past polls