in reply to Re: Constants refferences
in thread Constants refferences

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 ?

Replies are listed 'Best First'.
Re^3: Constants refferences
by perrin (Chancellor) on Aug 21, 2008 at 16:11 UTC
    No. Just use package variables instead.
Re^3: Constants refferences
by ikegami (Patriarch) on Aug 21, 2008 at 22:00 UTC

    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.

Re^3: Constants refferences
by TGI (Parson) on Aug 22, 2008 at 18:02 UTC

    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