zerohero has asked for the wisdom of the Perl Monks concerning the following question:
I'd like to have read-only constants which exist in a module that I include. The purpose of the constant is to eliminate "magic numbers", as one would do with a #define in C.
What I've been doing, which has some drawbacks, is to make a module with Exporter and do the following, and then import each constant into the package it's used in.
package MyConstants; use base qw (Exporter); our @EXPORT_OK = qw (SOME_CONSTANT); + use constant SOME_CONSTANT => 'the value';
Is there a better method than this? This suffers the drawback that inside of a hash, it doesn't get interpolated (i.e. $h{SOME_CONSTANT} doesn't behave as one would "like").
Back to
Seekers of Perl Wisdom