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        


In reply to Re: Question about UNIVERSAL::can (require) by tye
in thread Question about UNIVERSAL::can by schetchik

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.