Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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 :

$module->can('new')
to test .

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

very thanks your wisdom :)

Comment on module loaded test
Select or Download Code
Re: module loaded test ( Module::Loaded)
by Anonymous Monk on Jun 27, 2013 at 04:05 UTC
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
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/Bar.pm", 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/Bar.pm"} then the module Foo::Bar (i.e. Foo/Bar.pm) 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/Bar.pm", 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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1040900]
Approved by LanX
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2014-12-26 03:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (164 votes), past polls