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:
# 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;
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.
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.