legLess has asked for the wisdom of the Perl Monks concerning the following question:
I need to use constants that are (a) usually numeric, and (b) often included in text sent to the user. Of the WTDI I'm sure there are many, but I've found two, and I hope there are better ones.
Method numero uno uses use constant. An advantages is that everything looks like a constant; a disadvantage is that the dot construct looks kind of ugly.
use constant MIN_CHAR => scalar 1; use constant MAX_CHAR => scalar 12; print 'x '.MIN_CHAR.'-'.MAX_CHAR.' x';
Method numero two uses a hash. An advantage is that there's no dot concatenation; a disadvantage is that they don't look much like constants anymore.
my %constants = ( min_char => 1, max_char => 12 ); print "x $constants{'min_char'}-$constants{'max_char'} x";
Left to my own devices, I'd use the first. The dots are ugly, but at least I'm using constants like constants. Does anyone have a better method?
Thanks
--
man with no legs, inc.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Use constant vs. hashes
by MeowChow (Vicar) on Jul 03, 2001 at 07:20 UTC | |
|
Re: Use constant vs. hashes
by bikeNomad (Priest) on Jul 03, 2001 at 08:14 UTC | |
by repson (Chaplain) on Jul 03, 2001 at 10:20 UTC | |
by legLess (Hermit) on Jul 03, 2001 at 21:38 UTC | |
by frag (Hermit) on Jul 03, 2001 at 09:41 UTC | |
|
Re: Use constant vs. hashes
by tachyon (Chancellor) on Jul 03, 2001 at 06:20 UTC | |
|
Re: Use constant vs. hashes
by John M. Dlugosz (Monsignor) on Jul 03, 2001 at 07:53 UTC | |
|
Re: Use constant vs. hashes
by legLess (Hermit) on Jul 03, 2001 at 06:04 UTC |