in reply to Re^2: Can't import constants in multiple namespaces
in thread Can't import constants in multiple namespaces
I guess we're all reading these a little too fast. And I think I confused you with my comment about OO and use base. You don't need that for what you're doing, though you do need it for Exporter which we overlooked before.
Here's an annotated version of something that should work. I've reordered/simplified lines for clarity. For example, you don't really need the BEGIN block and assuming you're using Perl 5.6 or later, you can replace all the use vars with our.
# MyMod.pm use strict; use warnings; package MyMod; use constant MYMOD_DEFAULT => 1; use base qw( Exporter ); # You need to subclass Exporter our @EXPORT = qw( MYMOD_DEFAULT ); # No extra parens needed 1;
# MyMod/db.pm use strict; use warnings; package MyMod::db; use MyMod; # imports MYMOD_DEFAULT automatically sub fred { print "hello" if (MYMOD_DEFAULT); } 1;
# fred.pl use strict; use warnings; use MyMod::db; MyMod::db::fred(); # fully qualified call
Output of fred.pl:
hello
-xdg
Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Can't import constants in multiple namespaces
by mje (Curate) on Mar 28, 2006 at 14:53 UTC | |
by mje (Curate) on Mar 28, 2006 at 16:13 UTC | |
by xdg (Monsignor) on Mar 28, 2006 at 16:43 UTC | |
by mje (Curate) on Mar 28, 2006 at 16:50 UTC |