I need to load perl modules on the fly from mod_perl. Here is my working solution.

In an apache conf file I have:

<FilesMatch "\.pm"> PerlSetVar RootNamespace CEMS::EXT PerlFixupHandler CEMS::AutoLoader </FilesMatch>
The code above tells apache to execute CEMS::AutoLoader::handler() whenever it sees a uri that looks like *.pm

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.