in reply to Constants refferences

I am with brother Tanktalus. I would like to add that the usage of Readonly is also a Perl Best Practice. if you can, use it unless the performance issue is more important to you. The constant in Perl is a bit ugly, In Perl6 they will fix it, see Make constants look like variables for the limitations of constant and how they will solve it in Perl6.

Replies are listed 'Best First'.
Re^2: Constants refferences
by leonidlm (Pilgrim) on Aug 21, 2008 at 15:30 UTC
    Adding another related question:
    Can I load a constant variable values at runtime ?
    For example if I have a configuration file that overwrites couple of the constants ?
      No. Just use package variables instead.

      What's special about constants is that they are replaced with their value when a statement that uses them is compiled. It doesn't make sense to replace them after they're no longer used.

      Note that is possible to execute code (such as loading your config file) before other code (such as the code using the constants) is compiled. But why bother.

      This is one place where Readonly shines. You can create your variables, initialize them and then mark as read only.

      Also worth noting is that arrays and hash references point to data structures that can be modified. This is covered in the constant perldoc.

      I have found constants useful for simple things like naming array indexes data structures. Otherwise, if I need to be sure a data-structure is immutable, I use Readonly or lock hashes using Hash::Util, depending on the complexity of the structure.


      TGI says moo