[Buffalo-pm] Test if module is loaded

Adriano Ferreira a.r.ferreira at gmail.com
Tue Dec 8 10:01:48 PST 2009


On Tue, Dec 8, 2009 at 3:53 PM, Joshua Johnson <joshpauljohnson at gmail.com>wrote:

> Hello all!
>
> Is there a way to test if a perl module is loaded? I'm working on a
> small project where I load some modules dynamically as part of a
> configuration and then "use" them at that time. I don't get any errors
> but I just feel dirty loading a module that may have already been
> loaded. So, if at the top of the file I do:
>
> use Whatever::Project::MyStuff;
>
> Can I find out later if Whatever::Project::MyStuff is already loaded?
> I suppose it may not matter or have any kind of performance impact but
> now I'm just curious to know if it can be done.
>
>
I think "require" will do just fine, with minimum performance impact in this
case.

But it can be done by looking at the corresponding entry for the module in
%INC array. So after


      use Whatever::Project::MyStuff;

or simply

       require Whatever::Project::MyStuff;

you must find a true value at

        $INC{'Whatever/Project/MyStuff.pm'}

So you have to transform the module name into a file name (for example, like
UNIVERSAL::require does).

You can read the docs of %INC with a new version of perldoc with

      $ perldoc -v '%INC'

or just look for the corresponding section of "perlvar.pod" if your perldoc
is not so new.


Cheers,
Adriano Ferreira


Thanks,
> Josh J
> _______________________________________________
> Buffalo Perl Mongers Homepage
> http://buffalo.pm.org
>
> Buffalo-pm mailing list
> Buffalo-pm at pm.org
> http://mail.pm.org/mailman/listinfo/buffalo-pm
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/buffalo-pm/attachments/20091208/e1442523/attachment.html>


More information about the Buffalo-pm mailing list