I was starting to write a constants module (and hating it) when the following funkiness sprang to mind.
This seems nicer to me than the standard way because I don't have to keep track of them in two places (in the constants pragma *and* the export list).
You can also do something like the second one if you really want to go all out.
Update: This only works with 5.8.0. 5.6.1 + earlier don't support creating multiple constants with a single use statement:(
package foo;
use base qw( Exporter );
my %constants;
use constant +{
%constants = (
AAA => 1,
BBB => 2,
CCC => 3,
DDD => 4,
)
};
our @EXPORT = keys %constants;
1;
package foo2;
use base qw( Exporter );
my %all;
my %foo;
my %bar;
use constant +{
%all = (
%foo = (
AAA => 1,
BBB => 2,
),
%bar = (
CCC => 3,
DDD => 4,
),
)
};
our @EXPORT_OK = keys %all;
our %EXPORT_TAGS = (
all => [ keys %all ],
foo => [ keys %foo ],
bar => [ keys %bar ],
);
1;