I *think* I know why this is going on, but I would like someone smarter than me to explain it. I have spent some time reading documentation and did not find anything that explained this exact scenario.

Consider... Executing this code:
# Main script require 'TEST2.pm'; require 'TEST.pm'; test_sub();
produces the error "Undefined subroutine &main::test_sub called..."

The subroutine test_sub lives in the TEST package within TEST.pm, and is exported:
# TEST.pm *test_sub = *TEST::test_sub; package TEST; sub test_sub { print "Hello, world!\n"; }
and the contents of TEST2.pm:
# TEST2.pm package TEST2; require 'TEST.pm'; return 1;

So the main script requires TEST2.pm, which requires TEST.pm from within the TEST2 package. Then the main script requires TEST.pm, which is responsible for exporting the test_sub() subroutine. But it does not work. If I reverse the two require's in the main script, the call to test_sub() will work, but calling it from TEST2 will not work. I am under the impression that the first time require is called for TEST.pm (from the TEST2 package in TEST2.pm), test_sub() is exported to TEST2. When the main script then attempts to require TEST.pm, it is skipped because it has already been require'd. This means the main script cannot find test_sub() (unless the call to it is fully-qualified.) Is this the right reason for this behavior, or is something else going on? And is there any way to get the test_sub() subroutine to be recognizable from both the main script and the TEST2 package, without using a fully-qualified call?

In reply to Requiring libraries that export subroutines, and are required by other libraries... Exported subs cannot be found? by RP31

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.