No, using constants allows the Perl debugger to specifically ignore blocks of your code - it can figure out which parts it can ignore at compile-time, and avoid even compiling them. Take for example this script:

use constant DEBUG => 0; if (DEBUG) { print "Calculator with debugging..."; } else { print "Calculator..."; } print "1 + 1 = ", (1 + 1), "\n";

If you save that as, say debug.pl and then run the following command:

perl -MO=Deparse tmp/constants.pl

This uses the B::Deparse module to show you the code that Perl's runtime will actually execute. The result is:

use constant ('DEBUG', 0); do { print 'Calculator...' }; print '1 + 1 = ', 2, "\n";

You will notice that the print "Calculator with debugging..." part is entirely absent, because the Perl compiler knows that DEBUG is false, so that chunk of code will never be run! Perl doesn't bother compiling it. Even the if/else structure has gone.

(You'll notice also that the Perl compiler has added 1+1 so that the Perl runtime doesn't have to. This is called constant folding. In this particular example it doesn't offer any real benefit, but if that sum was inside a loop, then calculating it once at compile time is much better than calculating it every loop at runtime!)

So anyway, using constants for tasks like this allows the Perl compiler to optimize your code. With variables it can't.

So what about variables that have been set using Readonly?

use Readonly; Readonly my $DEBUG => 0; if ($DEBUG) { print "Calculator with debugging..."; } else { print "Calculator..."; } print "1 + 1 = ", (1 + 1), "\n";

Running it through B::Deparse we get...

use Readonly; Readonly my $DEBUG, 0; if ($DEBUG) { print 'Calculator with debugging...'; } else { print 'Calculator...'; } print '1 + 1 = ', 2, "\n";

So the compiler has not been able to do much optimisation.

Also it's worth noting that Readonly is implemented using Perl's tied variable magic, which imposes a considerable performance penalty - Readonly variables are actually slower than normal variables.


In reply to Re^3: How to turn a "developer code" into a "user code" by tobyink
in thread How to turn a "developer code" into a "user code" by mascip

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.