Weevil has asked for the wisdom of the Perl Monks concerning the following question:
Hello monks, my problem is as follows. I have a module (let's call it Module), which has a few sub-modules (let us call them Module::Monkey and Module::Ape).
Module exports some constants through use constant using Exporter. The code looks like this:
Currently, my only solution that works is to define the constants in a seperate package file and use that in all my modules that need the constants (kind of like a .h file in C), but that seems a messy way of doing things. Is there an accepted way of defining constants that work across all my sub-packages? Am I being silly by doing use Module::Monkey; and so on in Module.pm and then doing use Module; in Module/Monkey.pm to import Module's constants?
Man, I hope I explained that right, it's confusing the heck out of me. I hope you can guide me.
Module exports some constants through use constant using Exporter. The code looks like this:
If I use Module; in main, the constants are imported just fine. However, if I use Module; in Module/Ape.pm or Module/Monkey.pm, the constants are not imported. I suspect it's because I did use Module::Monkey; and use Module::Ape; in Module.pm, and this is processed before the code to export the constants ever gets done. I tried putting the exporting code inside a BEGIN block, but that did not fix the problem, and surely if I put use Module; in Module/Monkey.pm, the constants should be re-imported regardless of whether Module.pm has already been loaded.# Module.pm package Module; require Exporter; # Make sure these modules are available to any script that does "use M +odule;": use Module::Monkey; use Module::Ape; use vars qw(@EXPORT @EXPORT_OK %EXPORT_TAGS @ISA); @ISA = qw(Exporter); @EXPORT = qw( MDB_INT MDB_FLOAT MDB_STRING ); use constant { MDB_INT => 0, MDB_FLOAT => 1, MDB_STRING => 2, }; # bla bla bla... 1;
Currently, my only solution that works is to define the constants in a seperate package file and use that in all my modules that need the constants (kind of like a .h file in C), but that seems a messy way of doing things. Is there an accepted way of defining constants that work across all my sub-packages? Am I being silly by doing use Module::Monkey; and so on in Module.pm and then doing use Module; in Module/Monkey.pm to import Module's constants?
Man, I hope I explained that right, it's confusing the heck out of me. I hope you can guide me.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Problems importing constants in multiple namespaces
by ysth (Canon) on Aug 19, 2004 at 21:36 UTC | |
by Weevil (Novice) on Aug 19, 2004 at 23:51 UTC | |
Re: Problems importing constants in multiple namespaces
by Joost (Canon) on Aug 19, 2004 at 20:43 UTC | |
Re: Problems importing constants in multiple namespaces
by naChoZ (Curate) on Aug 19, 2004 at 18:22 UTC | |
Re: Problems importing constants in multiple namespaces
by dragonchild (Archbishop) on Aug 19, 2004 at 18:23 UTC |
Back to
Seekers of Perl Wisdom