Recently I came across a problem with namespaces that I do not fully understand.
I dont know enough about namespaces though to understand exactly what is going on here.

I was under the assumption that the same module could be 'used' or required
multiple times, throughout an application and its modules, without a problem.
Even if this created a sort of 'use' loop; one module using another, that in turn uses the first one again.

AB_prod.pm => SYS_mail.pm => AB_prod.pm

I am finding though, that this kind of setup causes problems in some situations,
depending on the order that the modules use statements are listed in the App.

When modules are used in a certain order, I start getting runtime error messages like this one.

#-------------------------------------------------------------------- Undefined subroutine &AB_prod::send_mail called at /usr/web/wonko/test/lib/AB/AB_prod.pm line 18. #--------------------------------------------------------------------

Here is an simple test setup that I have made up to show what I am talking about....

In this hypothetical example, the modules used by the Application are
divided up into layers. Each set of layer modules is kept in its own directory.

/u/web/wonko/test/lib/AB/ -- Contains Applied Business rules.
/u/web/wonko/test/lib/SYS/ -- Contains System level functions.

A simple test script that uses an AB and a SYS layer function.
/usr/web/wonko/test/test1.pl #-------------------------------------------------------------------- #!/usr/local/bin/perl -w use strict; use lib qq{/usr/web/wonko/test/lib/SYS}; use lib qq{/usr/web/wonko/test/lib/AB}; use SYS_mail qw( send_mail ); # Move after AB_prod & error goes away use AB_prod qw( send_notice ); # uses SYS_mail.pm as well. send_notice(); exit; #--------------------------------------------------------------------
This module 'uses' the SYS_mail module, just as the test1.pl script does. This in itself does not seem to be a problem.
/usr/web/wonko/test/AB/AB_prod.pm #-------------------------------------------------------------------- package AB_prod; use lib qq{/usr/web/wonko/test/lib/SYS}; use SYS_mail qw ( send_mail ); use strict; use vars qw(@ISA @EXPORT_OK); use Exporter(); @ISA = qw(Exporter); @EXPORT_OK = qw( send_notice ); sub send_notice { print qq{Hello from AB_prod::send_notice\n}; send_mail(); } 1; #--------------------------------------------------------------------
Here is where the problem develops.
This module uses the'AB_prod.pm' module, which is already using the 'SYS_mail.pm'
'AB_prod' => 'SYS_mail' => 'AB_prod'. This causes a situation where
it looks like the namespace is being modified to where the AB_prod module
cannot find the SYS_mail module again, during runtime.
This situation does not generate any errors and passes fine under a
'perl -cw'. The error only surfaces, when the actual function(SYS_mail::send_mail)
is called at runtime.
It like its entry in the Symbol table got stomped or something.
/usr/web/wonko/test/lib/SYS/SYS_mail.pm #-------------------------------------------------------------------- package SYS_mail; use lib qq{/usr/web/wonko/test/lib/AB}; use AB_prod qw( send_notice ); use strict; use vars qw(@ISA @EXPORT_OK); use Exporter(); @ISA = qw(Exporter); @EXPORT_OK = qw( send_mail ); sub send_mail { print qq{Hello from SYS_mail::send_mail\n}; } 1; #--------------------------------------------------------------------
When the test1.pl script is run, the following runtime error is generated.
#-------------------------------------------------------------------- :!./test1.pl Hello from AB_prod::send_notice Undefined subroutine &AB_prod::send_mail called at /usr/web/wonko/test/lib/AB/AB_prod.pm line 18. #--------------------------------------------------------------------

Notice that it appears to be looking for the send_mail function in
the wrong name space.

What is very strange, is that changing the order of the AB and SYS use
statements in the test1.pl script, DOES make the error message go away and
everything functions normally.

Is the Symbol table entry for this function actually getting squashed?

Is this type of setup for Modules just unacceptable?

Any help is much apprecited,
Wonko

In reply to 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.