Perl Monk, Perl Meditation | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
'use constant' doesn't appear to be terribly popualar,
as 'my' does effectively the same thing, without the child-proof
safety locks that prevent you from modifying variables
declared that way.
Instead of declaring a whole whack of constants using my(), such as: You could always convert this to a hash-structure, such as: This has the advantage of allowing you to declare virtually unlimited constants, without having to explicitly declare them on their own my() line. Additionally, you can load these from a file quickly and easily (i.e. an INI-type file, an Apache-style file, or something else) such that you can vary the configuration without modifying the program itself. Of course, using a hash-lookup is slower than a regular variable, but the price you pay would depend on frequency of use, and in practice, should be so slight as to really be irrelevant. In most cases where a constant is used frequently, such as inside a loop, you would expect to see something like: In this admittedly simple example, the hash is only used once, and the variable itself is repeatedly used. If you really don't trust yourself to keep your const's constant, and to not modify them in any way, write a function to extract parameters from it, such as: sub Config { return $config{$_[0]}; } Then you could keep %config in some sort of codeblock or module which nobody has access too. This is kind of extreme, though. In reply to Re: my $var; vs. use constant VAR = '';
by tadman
|
|