This is because of how perl requires & uses. Consider this code:
package Foo; use thing_to_use; ... package Bar; use thing_to_use; ...
When this code executes, in package Foo, thing_to_use is included, and the stuff in thing_to_use is put into the namespace Foo.
Then in package Bar, thing_to_use is specified to be used again, but since perl has already used thing_to_use, it skips over that use line. (otherwise circular references would keep parsing forever - among other issues).
So when you try to use something from thing_to_use in package Bar, it doesnt work right because the functions, etc in thing_to_use were put into the Foo namespace. If you fully qualified that in package Bar it would work in this instance, but not in general, because other programs might not import thing_to_use into package Foo, but some other package that asked for it first.

The solution would be something like this:

use thing_to_use; package Foo; ...
And then in your code for Bar:
use Bar; package Bar; ...
This way, thing_to_use is imported into main. But, you'll have to qualify calls into thing_to_use with main:: in both Foo and Bar.

In reply to Re: Namespace and use confusion. "Undefined subroutine errors" by shemp
in thread Namespace and use confusion. "Undefined subroutine errors" by Wonko the sane

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.