in reply to Re^3: Can't import constants in multiple namespaces
in thread Can't import constants in multiple namespaces

Thanks, that does in deed work but it has strayed a somewhat from what I was trying to do which is probably because I tried to simplify too much. I am actually trying to subclass Perl DBI so I need:
DBIx::MyMod DBIx::MyMod::db DBIx::MyMod::st
The user of DBIx::MyMod does
use DBIx::MyMod qw(MYMOD_CONSTANT); # connect in the root my $h = DBIx::MyMod->connect(x,y, {XXX => MYMOD_CONSTANT});

I override connect in DBIx::MyMod and other DBI methods in DBIx::MyMod::db and DBIx::MyMod::st. I want to define and export the constants in DBIx::MyMod but still use them in DBIx::MyMod::db. I believe this would be equivalent to your fred.pl and the previous MyMod example doing:

fred.pl use MyMod; # assuming all constants in EXPORT MyMod::db->fred();
and
use strict; use warnings; package MyMod; use MyMod::db; use constant MYMOD_DEFAULT => 1; use base qw (Exporter); our @EXPORT = qw(MYMOD_DEFAULT); 1;
use strict; use warnings; package MyMod::db; use MyMod; sub fred { print "hello" if (MYMOD_DEFAULT); } 1;

Do you know if it is possible to make that work?

Replies are listed 'Best First'.
Re^5: Can't import constants in multiple namespaces
by mje (Curate) on Mar 28, 2006 at 16:13 UTC
    Thanks to all. The solution which worked (avoiding the MyMod::MYMOD_DEFAULT redefined at /usr/local/lib/perl5/5.8.7/constant.pm message and allowing the constant to be seen "above" and "below" the module which defined it was:
    fred.pl use MyMod; MyMod::db->fred(MYMOD_DEFAULT);
    MyMod.pm use strict; use warnings; package MyMod; use constant MYMOD_DEFAULT => 1; use Exporter qw(import); our @EXPORT = qw (MYMOD_DEFAULT); # changing the use to require did the trick: require MyMod::db; sub dave { print "The constant is ", $_[1], "\n"; } 1;
    MyMod/db.pm use strict; use warnings; package MyMod::db; use MyMod; sub fred { print "hello\n" if (MYMOD_DEFAULT); } 1;
Re^5: Can't import constants in multiple namespaces
by xdg (Monsignor) on Mar 28, 2006 at 16:43 UTC

    Just be careful not to create circular dependencies. MyMod uses MyMod::db and vice versa. That's likely to cause trouble. Instead, refactor all your constants to MyMod::constants and use that in MyMod and MyMod::db. In MyMod you can import from MyMod::constants and still re-export them in @MyMod::EXPORT.

    -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.

      Yes, I had tried that as a solution and I agree it is better but I could not understand why I could not make it work as it was. Thanks again for the help.