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").
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Perl "Constants"
by GrandFather (Saint) on Jul 07, 2009 at 01:54 UTC | |
by spazm (Monk) on Nov 11, 2011 at 22:36 UTC | |
|
Re: Perl "Constants"
by perrin (Chancellor) on Jul 07, 2009 at 03:55 UTC | |
|
Re: Perl "Constants"
by tprocter (Sexton) on Jul 07, 2009 at 02:34 UTC | |
by thunders (Priest) on Jul 07, 2009 at 03:13 UTC | |
|
Re: Perl "Constants"
by JavaFan (Canon) on Jul 07, 2009 at 07:19 UTC | |
by Marshall (Canon) on Jul 07, 2009 at 07:37 UTC | |
|
Re: Perl "Constants"
by Marshall (Canon) on Jul 07, 2009 at 07:14 UTC | |
|
Re: Perl "Constants"
by Arunbear (Prior) on Jul 07, 2009 at 16:24 UTC | |
by vessko (Initiate) on Feb 03, 2011 at 23:04 UTC |