Update: Forgot to export C3 when testing X_X. Not only does every syntax work with Exporter, further testing with perl -MO=Debug showed me that constant folding *does* apply with Exported constants.

Yes, putting them in a module is a great idea. However, constants are special, and they won't work as expected the way you have the module coded.

use c1; print(&C1, C2(), C3, "\n"); # blahfoobar BINGO! do "c2.pm"; print(&C4, C5(), C6, "\n"); # blahfooC6 PARTIAL FAIL! BEGIN { do "c3.pm"; } print(&C7, C8(), C9, "\n"); # blahfoobar BINGO! c1.pm ===== package c1; require Exporter; use base qw(Exporter); @EXPORT = qw(C1 C2 C3); use constant C1 => 'blah'; use constant C2 => 'foo'; use constant C3 => 'bar'; 1; c2.pm ===== use constant C4 => 'blah'; use constant C5 => 'foo'; use constant C6 => 'bar'; c3.pm ===== use constant C7 => 'blah'; use constant C8 => 'foo'; use constant C9 => 'bar';

That's because Exporter loses the magic that constantss have. Exported constants are not inlined in the importing module, and therefore are not subject to constant folding optimization. That is, unless you use do() at compile time to emulate a C/C++ #include.


In reply to Re: constants in multiple libraries by ikegami
in thread constants in multiple libraries by shemp

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.