Is using constants slower than using variables
Considering that Perl doesn't have constants, the question isn't clear. It will depend on what you consider a constant. Many people consider a variable in all caps to be a constant. That of course is as fast as using constants.

Others use "use constant" or tiny subs with an empty prototype returning a literal as constants. Are they faster than variables? Well, that depends. If it's just fetching a value (read access), yes, then they are faster as the fetch can be done at compile time. But such constants lack a sigil. So you cannot easily interpolate them. Hence, you might write:

printf "The value is %d.\n", CONSTANT;
instead of
print "The value is $CONSTANT.\n";
The former means parsing a format, and substituting. That may very well be slower than the interpolation.

Now, you are using methods as constants. I haven't benchmarked it, but I'd be quite surprised if that wasn't the slowest solution of the three (variable, empty prototype sub, method).

Personally, I prefer variable. In all caps. Speed difference isn't an issue for me. Variables interpolate; subs and methods don't. If I want to trap a possible assignment to such a variable, I use Readonly. But usually, I do not care. If someone wants to assign to something that's supposed to constant, it's their own responsibility. Who am I to stop them?


In reply to Re: The best way to use constants by JavaFan
in thread The best way to use constants by saurabh.hirani

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.