1

In my Web application, I am loading the module dynamically at runtime by using the below code but looks like it is taking good amount of time that makes the application bit slower.

eval {
    eval "require $package_name";
}
if ($@) {
    die"Error while loading module: $@\n";
}

I made one small program to check that eval "require $package_name"; this piece of code taking how much time on an average, then I found that it is taking almost 10 seconds that is huge for web application.

use strict;
use Time::HiRes qw( gettimeofday tv_interval );

my $startTime = [ gettimeofday() ];
print "Time:@{$startTime}->[0]\n";

eval "require Foo::Bar"; # giving example

my $timeSpent = tv_interval( $startTime, [ gettimeofday() ] );
print "Time Spent:$timeSpent\n";

exit 1;

Output:

Time:1378897304
Time Spent:10.627147

So my question is that why this is taking so much time and Is there any alternative to resolve this?

4
  • This is most probably not the problem of string-eval. Just require Foo::Bar without the eval will probably take the same amount of time spent. Commented Sep 11, 2013 at 11:30
  • Right but still I am wondering why this is taking so much time. Commented Sep 11, 2013 at 12:44
  • Maybe Foo::Bar requires itself half of the CPAN? Or some expensive computations are done in Foo::Bar outside of functions? Commented Sep 11, 2013 at 14:30
  • BTW, the double eval in the first paragraph is useless. It's enough to have only the string-eval here. Commented Sep 11, 2013 at 14:31

1 Answer 1

1

Yes, it is.

Use apache and mod_perl and load modules on startup.

http://www.conceptsolutionsbc.com/perl-articles-mainmenu-41/29-perl-and-apache/55-modperl-part-2-pre-loading-perl-modules

I think the best way is to use a very simple script which just require all of your used modules:

PerlRequire "/usr/local/apache2/conf/startup.pl" 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer. I am already using apache & mod_perl but I cannot load modules at startup becuase module is coming dynamically at runtime. Consider the case where this piece of code is used by many applications, and for each application module name is different.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.