ben-at-geeklair has asked for the wisdom of the Perl Monks concerning the following question:

I'm writing a custom error-handling module for a steaming pile of CGIs that should rollback any active database handles, disconnect 'em, and choke out a nice, friendly message before dying. I'd like to be able to take advantage of HTML::Template with a default error template if possible, but to just spit out an HTML page if HTML::Template hasn't been loaded by the script that calls my module. Does anyone know of a reliable way to determine (from within the module) if the calling script has loaded another module? Is this the kind of thing for which I should just use eval()?
I'm also currently passing any active database handles manually into the module's functions. Is there a smart way to get a list of all active handles, so I don't have to trust myself to pass them to the error function?

Thanks much,

Ben

Replies are listed 'Best First'.
Re: checking for preloaded modules
by davorg (Chancellor) on Apr 18, 2001 at 12:08 UTC

    You could probably use the %INC hash. This contains a list of all of the modules that have been loaded. The keys are the names of the modules and the values are the paths that the modules were loaded from.

    if ($INC{'HTML/Template.pm'}) { # do HTML::Template stuff } else { # do vanilla HTML stuff }

    Update: Corrected hash key. Thanks to jmcnamara for pointing it out (twice!)

    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

Re: checking for preloaded modules
by nardo (Friar) on Apr 18, 2001 at 01:13 UTC
    if(defined(%HTML::Template::)) { #HTML::Template has been loaded } else { #HTML::Template has not been loaded }
Re: checking for preloaded modules
by sachmet (Scribe) on Apr 18, 2001 at 01:20 UTC
    To answer your first question, you could always check to see if the symbol table entries exist. For example:
    > perl print "Data::Dumper exists\n" if %Data::Dumper::; > perl -MData::Dumper print "Data::Dumper exists\n" if %Data::Dumper::; Data::Dumper exists
    So you could do:
    if (%HTML::Template::) { # HTML::template stuff } else { # Regular HTML stuff }
    Keep in mind, however, there is a caveat to that: if you load HTML::Template::othermodule without loading HTML::Template, that may fail. Instead, you can do:
    if (exists ${%HTML::Template::}{'import'}) { ... }
    which checks for the existance of the import subroutine. import is in every package by default, and that can be used to see if it exists (and hence if a module has been loaded by that name).
Re: checking for preloaded modules
by arturo (Vicar) on Apr 18, 2001 at 01:16 UTC

    Smudged (not too dirty, not too clean) hack is the one you suggest: wrap an eval block around the HTML::Template version, if the module's not loaded, $@ will be set, and you can just generate the 'plain ole' HTML page by hand.

    But since any CPAN modules worth its salt (IMO) sets $VERSION, you should be able to test defined ($HTML::Template::VERSION) and achieve similar result. But in a way, that's dirtier than the eval {} solution because a module can be perfectly functional without setting $VERSION, so I can see an otherwise excellent coder leaving that out.

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Re: checking for preloaded modules
by princepawn (Parson) on Apr 18, 2001 at 02:26 UTC
    This wasn't your main question, but CGI::Application makes use of HTML::Template internally and it must export a re-useable error-handling routine.

    If you aren't sure, msg AgentM or join the support mailing list.