The use keyword is quite simple in its idea. It just looks up a file, and then issues an import call on that package. It will look in the directories listed the @INC array, which you can modify through the lib pragma. This means that use MyPackage::Package2; will look for MyPackage/Package2.pm. But it will also issue an import call on MyPackage::Package2, MyPackage::Package2->import(), but that will do nothing, because in Package2.pm you have package Package2; (I presume). So there is no MyPackage::Package2 package.

Changing package Package2; to package MyPackage::Package2; will just make the import call work right, but it won't make Package1 find Package2 through use Package2;, since the lookup is done through the file system. This also won't make it possible to move modules around to different "packages", (java-style packages).

So what you really want to do is to keep your packages named whatever you want them to be. The way to make them all use()able is by using the lib pragma. @INC is a global variable, and all packages will share it, so if one package can use Package1;, then all other can too. This means that the problem will solve itself once the first modules is loaded if loaded via use, since that will make sure that MyPackage is in @INC.

Hope I've helped,
ihb

In reply to Re: "Packaging" Perl modules by ihb
in thread "Packaging" Perl modules by Heidegger

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.