I need to load perl modules on the fly from mod_perl. Here is my working solution.
In an apache conf file I have:
The code above tells apache to execute CEMS::AutoLoader::handler() whenever it sees a uri that looks like *.pm<FilesMatch "\.pm"> PerlSetVar RootNamespace CEMS::EXT PerlFixupHandler CEMS::AutoLoader </FilesMatch>
Module CEMS::AutoLoader looks like this
package CEMS::AutoLoader; use Apache::Constants qw(:common); use strict; sub handler { my $r = shift; return DECLINED unless $r->is_main; #get package name my $module = join ('::',$r->dir_config('RootNamespace'), split (/\//, substr($r->uri(),1,-3) ) ); #see if package really exists and set content handler if (eval "require ".$module) { $r->push_handlers(PerlHandler => $module); $r->handler('perl-script'); return OK; } #require threw an exception so package does not exist return DECLINED if ($@); } 1;
The above code is working, but there has to be a better way to do this. Calling require on every request just to see if a package exists probably isn't efficient. Most likely the package will already be loaded.
Does someone know of a better way to see if a package is already loaded?
Edit kudra, 2001-08-30 Added p breaks; code->single spaced
In reply to loading modules on the fly with mod_perl by pmc2
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |