Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

module loaded test

by chinaxing (Acolyte)
on Jun 27, 2013 at 03:20 UTC ( #1040900=perlquestion: print w/replies, xml ) Need Help??
chinaxing has asked for the wisdom of the Perl Monks concerning the following question:

Hi, everyone, I want to ensure a best method to test whether a module has loaded. if not I will load it with require;

I see some people use such method :

my $module = "MyModule::ABC"; eval " require $module " unless $module->can('isa');
which use can('isa') to test . but this may not working in new version of Perl ? which will true even the $module haven't loaded;

in some situation when Module is a OO module ,which has a new method generally. we can use :

to test .

is there a universal method for any module , to test loaded or not ?

very thanks your wisdom :)

Replies are listed 'Best First'.
Re: module loaded test
by tobyink (Abbot) on Jun 27, 2013 at 07:24 UTC

    Do you want to check if a module is loaded, or a package is loaded?

    These are subtly different, in that modules are related to pm files on the filesystem, while packages are related to namespaces (stashes) in Perl's memory. There's a convention of keeping the code for package "Foo::Bar" in module "Foo/", so packages and modules often coincide with each other. But sometimes testing for one will not give you the result you need, when you really should be testing for the other.

    The official way to check if a module is loaded is %INC. If exists $INC{"Foo/"} then the module Foo::Bar (i.e. Foo/ is considered to be loaded. This is how the require function avoids unnecessarily reloading modules; it checks %INC.

    For packages, there's no official technique, but one way that's become popular (because it's what Class::Load does, and Class::Load is popular) is to assume that if $Foo::Bar::VERSION is defined, or if @Foo::Bar::ISA is non-empty, or if there are any subs defined in the Foo::Bar namespace, then we can consider the package Foo::Bar to be loaded. Actually checking all that involves somewhat convoluted stash manipulation, so I'd recommend using Class::Load's is_class_loaded function.

    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

      There's a convention of keeping the code for package "Foo::Bar" in module "Foo/", so packages and modules often coincide with each other.

      s/convention/rule that makes a module a module - a definition/

      a module is a namespace associated with a file ; a module is a package you can use or require

Re: module loaded test
by patcat88 (Deacon) on Jun 27, 2013 at 04:09 UTC
    1st way, this almost always works, check %INC. 2nd way, works alot but not as much as 1st way, check scalar $Some::Module::VERSION for being true.
Re: module loaded test ( Module::Loaded)
by Anonymous Monk on Jun 27, 2013 at 04:05 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1040900]
Approved by LanX
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2017-06-28 15:10 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (640 votes). Check out past polls.