Wonko the sane has asked for the wisdom of the Perl Monks concerning the following question:
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....
/u/web/wonko/test/lib/AB/ -- Contains Applied Business rules.
/u/web/wonko/test/lib/SYS/ -- Contains System level functions.
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/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; #--------------------------------------------------------------------
Here is where the problem develops./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; #--------------------------------------------------------------------
When the test1.pl script is run, the following runtime error is generated./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; #--------------------------------------------------------------------
#-------------------------------------------------------------------- :!./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?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Namespace and use confusion. "Undefined subroutine errors"
by ikegami (Patriarch) on May 05, 2005 at 19:49 UTC | |
by Wonko the sane (Curate) on May 06, 2005 at 12:01 UTC | |
by bart (Canon) on May 07, 2005 at 06:50 UTC | |
|
Re: Namespace and use confusion. "Undefined subroutine errors"
by shemp (Deacon) on May 05, 2005 at 19:52 UTC |