in reply to Question about UNIVERSAL::can

Unfortunately, "module not loaded yet" is not a well-defined concept in Perl 5. Previous attempts to try to define that have met with quite disappointing results (see base which had to be replaced with parent due to exactly this problem).

My advice to you is, if you expect to be able to check whether Foo::Bar has yet been loaded, then you should make sure that loading Foo::Bar always sets $INC{'Foo/Bar.pm'}. For the usual case of Foo::Bar declared inside of the Foo/Bar.pm file, that just happens automatically.

For the unusual case of Foo::Bar declared in some other file, then you want to ensure the declaration looks something like:

package Foo::Bar; $INC{'Foo/Bar.pm'} = __FILE__;

(perhaps with a BEGIN thrown in somewhere there.)

If you want to be able to load Foo::Bar if it is found to not have been loaded already, then you, of course, want to make sure that loading Foo/Bar.pm causes Foo::Bar to be loaded. Which means that you just need to write require Foo::Bar; in order to load it.

But, if Foo/Bar.pm has already been loaded, then require Foo::Bar; just does a quick hash look-up and nothing more. So, rather than check whether it has been loaded or not and then doing the require, you might as well just do the require.

- tye