Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I AM CONSTANT AS THE NORTHERN STAR!

I revels now are ended Max, oh come now be honest, Co-Rion, you do prefer it this way, the way it was meant to be, no peace in our time.

Once more unto the breach, dear friends.

taH pagh taHbe'

I wonder; why it is that you are always the one in the bunker.

Replies are listed 'Best First'.
Re: Perl Constants
by zgrim (Friar) on Dec 29, 2005 at 03:01 UTC
    Besides readability and safety upon forbidden keywords ? Not much, i guess.
    TIMTOWTDI applies. Of course, you could always just - when in need of speed:
    # my constant FOO sub FOO () { 'bar'; }
    which would provide you with a compile-time constant and not use the "glob syntax" you mentioned. Last I saw on p5p, constants had this bad habbit of eating large chunks of ram. So abusing them in such circumstances would not be advisable. YMMV($code).
Re: Perl Constants
by astroboy (Chaplain) on Dec 29, 2005 at 11:31 UTC
    You should check out the Readonly module
Re: Perl Constants
by robin (Chaplain) on Dec 29, 2005 at 17:24 UTC
    There are at least two significant advantages:
    1. The use constant syntax is easier to read and understand, especially to someone who is not a Perl expert.
    2. A constant defined with use constant will be inlined at compile time, which is more efficient than looking up the value at run time (if the constant is used more than once).
    The disadvantage is that it's more difficult to include the constant value in a string: you have to say
    print "The value is ".CONSTANT.".\n";
    instead of
    print "The value is $CONSTANT.\n";
    which is a little more visually cluttered. On the other hand, the advantage of inlining is even more pronounced in this case, as the concatenations will be optimized away in the first example, but not in the second.

    On balance, I think it's usually better to use constant.