in reply to constants in multiple libraries

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.

Replies are listed 'Best First'.
Re^2: constants in multiple libraries
by shemp (Deacon) on Sep 17, 2004 at 16:41 UTC
    The first version will work if you also export C3

    Well it worked for me on:
    perl -v
    This is perl, v5.8.0 built for i386-linux-thread-multi ...