Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

require() on the fly

by BooK (Curate)
on Jan 16, 2001 at 04:49 UTC ( #52121=perlquestion: print w/ replies, xml ) Need Help??

In fact I have in fact two questions in one...

1) I am in the process of writing a CGI script that would be run quite often, and would need only a few of the many possibilities provided by (mostly param and header)... And I heard that using can quite slow down a script... Would use CGI /:cgi/ help, or should I rewrite the few bits I need?

2) This script will have some very different behaviors depending on the parameter the user feeds to it... Since it's CGI (no mod_perl yet), would something like

if($do eq 'this') { require This } else { require That }
help? It should be quicker than to parse and reparse all the (mostly useless) subs every time, shouldn't it?

Thanks for your comments.

Comment on require() on the fly
Download Code
Re: require() on the fly
by AgentM (Curate) on Jan 16, 2001 at 04:55 UTC
    You might look into other CGI-type modules like CGI::Lite or some other lib that you find under this search. For your second question, don't forget to import modules after requiring them. Since you're doing this runtime linking stuff, be sure to use CGI::Carp in combination with fatalsToBrowser.

      In fact since (and will only hold subroutine definitions, it won't be a package (no package This; at the beginning)... So I don't think I'll even need to use import or @EXPORT...

      It's really meant to be a convenient (and efficient) way to parse only the necessary subroutine code in order to speed up the script a bit. My question was more like: "Is it really worth it?"

        require reads and does preliminary parsing on the file. Saving one on a larger file will certainly save you time though probably not much if you're just loading a few functions. If you want to work on encapsualtion and efficiency, a package setup would work well. "Is it really worth it?" Probably not. But definitely give CGI::Lite a whorl.
(Ovid) Re: require() on the fly
by Ovid (Cardinal) on Jan 16, 2001 at 05:16 UTC
    One of the comments made in CGI Programming with Perl, second edition (O'Reilly), is that has to go through so much work to export its functions that the OO interface is as fast or faster as the function oriented interface. Of course, since I always use the OO interface, I've never put that to the test.

    Of course, you mentioned that you don't use mod_perl yet. If you're planning on switching to mod_perl in the future, sticking with the OO interface is probably a Good Thing.



Re: require() on the fly
by chipmunk (Parson) on Jan 16, 2001 at 07:03 UTC
    It is worthwhile to note that most of CGI's subroutines are stored as strings and compiled only on demand. If you never use redirect(), for example, then the redirect() method never has to be compiled. (This applies even for subs that you import.) While there is still some cost to creating the %SUBS hash that contains the code snippets, it's much more efficient than if CGI compiled all its code up front. So, using the CGI module for just a few functions isn't as bad as you might think.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2014-12-26 10:10 GMT
Find Nodes?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?

    Results (171 votes), past polls