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

Hello monks,
I am trying to include a package in my script and it takes over 3 seconds to do it. As this package is included in different cgi scripts this means a 3 seconds wait when loading a page, which is unacceptable. I have tried it with "use" and with "require" and it is the same. The package was written by a friend of mine and handles a database communication. I am using ActivePerl (5.8) on WindowsXP and the package occupies 56 Kb. Do you have any idea what the problem might be?
Thanks

Replies are listed 'Best First'.
Re: including a package - time issue
by misc (Friar) on Aug 27, 2007 at 10:23 UTC
    You should probably have a look at modperl.
    (http://perl.apache.org/)
    There's also a book available for free: http://modperlbook.org/

    Modperl will compile your perl scripts only once, and answer all requests with the already compiled scripts.

    Update:
    To go more into the details:
    It's hard to say what needs 3 seconds, without having more data/the code.
    I could think of:
    • Perl needs some time to compile the packages
    • Perl needs so long in order to find the package
    • The database connection is established while requiring the packages, this could also take 3 seconds

    However, all of the points above could be solved with modperl,
    the database connection could also easily be made persistent.(Apache::DBI)
Re: including a package - time issue
by bruceb3 (Pilgrim) on Aug 27, 2007 at 10:58 UTC
    While I think that "misc" has the right answer, if you don't want to use mod_perl, you could have your friend update the module so that the connection to the database isn't created until the connection needs to be used. Basically a lazy connection to the database.
Re: including a package - time issue
by andreas1234567 (Vicar) on Aug 27, 2007 at 10:11 UTC
    Do you have any idea what the problem might be?
    Not unless you include your code. Read How do I post a question effectively? and update your node with relevant information.
    --
    Andreas
      Thank you Andreas.
      I don't believe that a 56Kb file, which means about 2000 lines of code would help you. As for the script where I try to include the package, it is a simple
      use Libs::MyPackage;
      print "package included";

        The code you posted does not neccessarily indicate that Libs::MyPackage is the problem. Look for example at this code:

        use Libs::MyPackage; print "Package included\n"; BEGIN { sleep 10; };

        This will print "Package included" only after 10 seconds. Any <c>use line is equivalent to a BEGIN block, so any other module could cause the same problem, as could any not/slow responding network drive if you're (trying to) loading modules from there. Also, a virus scanner might run wild.

Re: including a package - time issue
by FunkyMonk (Bishop) on Aug 27, 2007 at 23:55 UTC
    Serious question: Why don't ask your friend?

    Since he wrote the package, he's going to to have a greater insight into why it might be taking longer than you think it should.