in reply to module loaded test
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: module loaded test
by Anonymous Monk on Jun 27, 2013 at 07:51 UTC |