Child pages
  • Perl-Critic-Policy-ValuesAndExpressions-ProhibitConstantPragma
Skip to end of metadata
Go to start of metadata

Perl::Critic::Policy::ValuesAndExpressions::ProhibitConstantPragma

We always disable this policy. The reason is that constants are more resourceful than ReadOnly implemented variables.

The perl interpreter does a good job of optimizing out code, which should not be run, so for example a debug flag implemented as a constant does actually mean that debugging code does not weigh down execution on production if set to a false value.

An example:

constantly.pl
#!/usr/bin/perl

use strict;
use warnings;

use constant DEBUG => 0;

if (DEBUG) {
	print "We are debugging\n";
}

print "Done...\n";

B::Terse can show us how the debug section is optimized away:

DEBUG enabled
% perl -MO=Terse constantly.pl 

LISTOP (0x1002dcfa0) leave [1] 
    OP (0x100422180) enter 
    COP (0x1002dcf40) nextstate 
    LISTOP (0x1002b6a80) scope 
        OP (0x1002a64c0) null [177] 
        LISTOP (0x1002dccd0) print 
            OP (0x10020a060) pushmark 
            SVOP (0x10020a0b0) const [3] PV (0x100884cd8) "We are debugging\n" 
    COP (0x1002b7190) nextstate 
    LISTOP (0x1002b7140) print 
        OP (0x1002b6ac0) pushmark 
        SVOP (0x100209e10) const [4] PV (0x100884bd0) "Done...\n" 
constantly.pl syntax OK
DEBUG disabled
% perl -MO=Terse constantly.pl 

LISTOP (0x1002b6a80) leave [1] 
    OP (0x1002b6ac0) enter 
    COP (0x1002a64c0) nextstate 
    OP (0x100209e10) null [5] 
    COP (0x1002dcf40) nextstate 
    LISTOP (0x1002dccd0) print 
        OP (0x10020a060) pushmark 
        SVOP (0x10020a0b0) const [3] PV (0x100884cd8) "Done...\n" 
constantly.pl syntax OK

The same two cases, implemented using ReadOnly.

readonly.pl
#!/usr/bin/perl

use strict;
use warnings;
use Readonly;

Readonly::Scalar  my $debug => 0;

if ($debug) {
	print "We are debugging\n";
}

print "Done...\n";
DEBUG enabled
% perl -MO=Terse readonly.pl 

LISTOP (0x1002b2340) leave [1] 
    OP (0x1002a5ef0) enter 
    COP (0x10020b070) nextstate 
    UNOP (0x100421560) entersub [3] 
        UNOP (0x100427750) null [142] 
            OP (0x1004229d0) pushmark 
            OP (0x1004272d0) padsv [2] 
            SVOP (0x100433af0) const [6] IV (0x100868fc0) 1 
            UNOP (0x1002bb990) null [17] 
                PADOP (0x10020a060) gv  GV (0x100868fa8) *Readonly::Scalar 
    COP (0x1002a5f70) nextstate 
    UNOP (0x1002bbcd0) null 
        LOGOP (0x1002a5c40) and 
            OP (0x100421940) padsv [2] 
            LISTOP (0x1002073f0) scope 
                OP (0x1002bbc30) null [177] 
                LISTOP (0x100421760) print 
                    OP (0x1002b94d0) pushmark 
                    SVOP (0x1002b56b0) const [7] PV (0x100884ae0) "We are debugging\n" 
    COP (0x1002a6020) nextstate 
    LISTOP (0x1002b67e0) print 
        OP (0x1002b95a0) pushmark 
        SVOP (0x1002b53e0) const [8] PV (0x10086d780) "Done...\n" 
readonly.pl syntax OK
DEBUG disabled
% perl -MO=Terse readonly.pl 

LISTOP (0x1002b2340) leave [1] 
    OP (0x1002a5ef0) enter 
    COP (0x10020b070) nextstate 
    UNOP (0x100421560) entersub [3] 
        UNOP (0x100427750) null [142] 
            OP (0x1004229d0) pushmark 
            OP (0x1004272d0) padsv [2] 
            SVOP (0x100433af0) const [6] IV (0x100868fc0) 0 
            UNOP (0x1002bb990) null [17] 
                PADOP (0x10020a060) gv  GV (0x100868fa8) *Readonly::Scalar 
    COP (0x1002a5f70) nextstate 
    UNOP (0x1002bbcd0) null 
        LOGOP (0x1002a5c40) and 
            OP (0x100421940) padsv [2] 
            LISTOP (0x1002073f0) scope 
                OP (0x1002bbc30) null [177] 
                LISTOP (0x100421760) print 
                    OP (0x1002b94d0) pushmark 
                    SVOP (0x1002b56b0) const [7] PV (0x100884ae0) "We are debugging\n" 
    COP (0x1002a6020) nextstate 
    LISTOP (0x1002b67e0) print 
        OP (0x1002b95a0) pushmark 
        SVOP (0x1002b53e0) const [8] PV (0x10086d780) "Done...\n" 
readonly.pl syntax OK

As you can see the version using proper constants is producing a lot shorter with DEBUG enabled output that the ones with DEBUG disabled or implemented using Readonly.

See also: Explanation on PerlMonks.