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.

In reply to Problems importing constants in multiple namespaces by Weevil

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.