in reply to How to include a large number of constants?

Yes, it is a compile-time vs. run-time issue. You could enclose your require statement in a BEGIN{} block and do just fine.

As for your second question, you could have all your constants have a common prefix, like C_. Then, you can do a little introspection as so:

package Constants; use base 'Exporter'; our @EXPORT = grep { substr( $_, 0, 2 ) eq 'C_' && __PACKAGE__->can( $_ ); } keys %Constants::; use constant C_FOO => 1; use constant C_BAR => 2; use constant C_BAZ => 3;

The Perfect is the Enemy of the Good.

Replies are listed 'Best First'.
Re^2: How to include a large number of constants?
by 5mi11er (Deacon) on Apr 27, 2005 at 17:55 UTC
    ++ to the last three answers!    I am currently in need of just this type of thing.
    I'm out of votes today, and needed a 'bookmark' here to remember to come back tomorrow...

    -Scott

Re^2: How to include a large number of constants?
by doom (Deacon) on May 03, 2005 at 03:52 UTC
    This is actually very cool:
    package Constants; use base 'Exporter'; our @EXPORT = grep { substr( $_, 0, 2 ) eq 'C_' && __PACKAGE__->can( $_ ); } keys %Constants::;
    The way I used to do it was to essentially use a source filter: I'd have a BEGIN block inside my Definitions.pm file read the file itself in and look for "use constant" lines:
    Constant Amusement (auto export trick)

    Anyone doing a lot of work with perl constants might want to skim through those talk notes, by the way. And in general, these days I'd recommend staying away from constants altogether -- their utility rarely makes up for their annoyances (e.g no sigil, so they don't interpolate very smoothly). Also a case can be made for keeping definitions of things in a YAML file instead of using perl code to store configuration (programmatic modification of YAML is a lot easier than parsing perl).