-
-
28 Feb 2021 08:34:14 UTC
- Distribution: Perl-Critic-Pulp
- Module version: 99
- Source (raw)
- Browse (raw)
- Changes
- Homepage
- How to Contribute
- Issues (7)
- Testers (975 / 10 / 0)
- Kwalitee
Bus factor: 1- 81.76% Coverage
- License: gpl_3
- Perl: v5.6.0
- Activity
24 month- Tools
- Download (217.29KB)
- MetaCPAN Explorer
- Permissions
- Subscribe to distribution
- Permalinks
- This version
- Latest version
- Dependencies
- IO::String
- List::MoreUtils
- List::Util
- PPI
- PPI::Document
- PPI::Dumper
- Perl::Critic
- Perl::Critic::Policy
- Perl::Critic::Utils
- Perl::Critic::Utils::PPI
- Perl::Critic::Violation
- Pod::Escapes
- Pod::MinimumVersion
- Pod::ParseLink
- Pod::Parser
- Scalar::Util
- Test::More
- version
- and possibly others
- Reverse dependencies
- CPAN Testers List
- Dependency graph
NAME
Perl::Critic::Policy::Compatibility::ConstantPragmaHash - new enough "constant" module for multiple constants
DESCRIPTION
This policy is part of the
Perl::Critic::Pulp
add-on. It requires that when you use the hash style multiple constants ofuse constant
that you explicitly declare either Perl 5.8 orconstant
1.03 or higher.use constant { AA => 1, BB => 2 }; # bad use 5.008; use constant { CC => 1, DD => 2 }; # ok use constant 1.03; use constant { EE => 1, FF => 2 }; # ok use constant 1.03 { GG => 1, HH => 2 }; # ok
The idea is to keep you from using the multi-constant feature in code which might run on Perl 5.6, or might in principle still run there. On that basis this policy is under the "compatibility" theme (see "POLICY THEMES" in Perl::Critic).
If you declare
constant 1.03
then the code can still run on Perl 5.6 and perhaps earlier if the user gets a suitably newerconstant
module from CPAN. Or of course for past compatibility just don't use the hash style at all!Details
A version declaration must be before the first multi-constant, so it's checked before the multi-constant is attempted and gives an obscure error.
use constant { X => 1, Y => 2 }; # bad use 5.008;
A
require
for the perl version is not enough sinceuse constant
is atBEGIN
time, before plain code.require 5.008; # doesn't run early enough use constant { X => 1, Y => 2 }; # bad
But a
require
within aBEGIN
block is ok (a past style, still found occasionally).BEGIN { require 5.008 } use constant { X => 1, Y => 2 }; # ok BEGIN { require 5.008; and_other_setups ...; } use constant { X => 1, Y => 2 }; # ok
Currently
ConstantPragmaHash
pays no attention to any conditionals within theBEGIN
, it assumes anyrequire
there always runs. It could be tricked by some obscure tests but hopefully anything like that is rare or does the right thing anyway.A quoted version number like
use constant '1.03'; # no good
is no good, only a bare number is recognised by
use
and acted on by ConstantPragmaHash. A string like that goes through toconstant
as if a name to define (which you'll see it objects to as soon as you try run it).Drawbacks
Explicitly adding required version numbers in the code can be irritating, especially if other things you're doing only run on 5.8 up anyway. But declaring what code needs is accurate, it allows maybe for backports of modules, and explicit versions can be grepped out to create or check Makefile.PL or Build.PL prereqs.
As always if you don't care about this or if you only ever use Perl 5.8 anyway then you can disable
ConstantPragmaHash
from your .perlcriticrc in the usual way (see "CONFIGURATION" in Perl::Critic),[-Compatibility::ConstantPragmaHash]
SEE ALSO
Perl::Critic::Pulp, Perl::Critic, Perl::Critic::Policy::Compatibility::ConstantLeadingUnderscore, Perl::Critic::Policy::ValuesAndExpressions::ProhibitConstantPragma, Perl::Critic::Policy::Modules::RequirePerlVersion
constant, "Constant Functions" in perlsub
HOME PAGE
http://user42.tuxfamily.org/perl-critic-pulp/index.html
COPYRIGHT
Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde
Perl-Critic-Pulp is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version.
Perl-Critic-Pulp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>.
Module Install Instructions
To install Perl::Critic::Pulp, copy and paste the appropriate command in to your terminal.
cpanm Perl::Critic::Pulp
perl -MCPAN -e shell install Perl::Critic::Pulp
For more information on module installation, please visit the detailed CPAN module installation guide.