Perl::...gmaHash(3pm)
๐ธ NAME ๐ธ
Perl::Critic::Policy::Compatibility::ConstantPragmaHash โ bestie, this one's all about having a new enough constant module so you can slam down multiple constants at once ๐
(and hey, it comes to you courtesy of the User Contributed Perl Documentation, so you know it's a whole community effort ๐ซถ)
โจ DESCRIPTION โจ
Okay so this policy lives inside the Perl::Critic::Pulp add-on ๐. Here's the tea: if you're using the hash style of multiple constants with use constant, you gotta explicitly declare either Perl 5.8 or constant 1.03 or higher. No cap.
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 whole vibe here is protecting you from using the multi-constant feature in code that might run on Perl 5.6, or that could, in principle, still run there ๐ญ. Which is why this policy sits under the compatibility theme (peep "POLICY THEMES" in Perl::Critic).
And if you declare constant 1.03, the code can still run on Perl 5.6 and maybe even earlier, as long as the user grabs a suitably newer constant module off CPAN ๐ฆ. Or, honestly? For past compatibility, just... don't use the hash style at all, queen ๐
.
๐ Details ๐
A version declaration must come before the first multi-constant โ that way it gets checked before the multi-constant is even attempted, instead of you getting some obscure cryptic error ๐
use constant { X => 1, Y => 2 }; # bad
use 5.008;
A require for the perl version is not enough, bestie, because use constant runs at BEGIN time โ before plain code even shows up to the party ๐
require 5.008; # doesn't run early enough
use constant { X => 1, Y => 2 }; # bad
But a require inside a BEGIN block? Totally fine ๐ (it's a bit of a past style, but you still see it around sometimes).
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
Right now, ConstantPragmaHash pays zero attention to any conditionals inside that BEGIN โ it just assumes any require in there always runs. Ngl, it could get tricked by some obscure tests, but hopefully anything like that is rare or does the right thing anyway ๐คท
And 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 just goes through to constant as if it were a name to define โ which, as you'll see, it objects to the absolute second you try to run it ๐ญ
๐ฎ๐จ Drawbacks ๐ฎ๐จ
Okay, real talk: explicitly adding required version numbers in your code can be so irritating, especially if everything else you're doing only runs on 5.8 and up anyway ๐. But โ declaring what your code actually needs is accurate, it lets you maybe allow for backports of modules, and those explicit versions can get grepped out to create or check Makefile.PL or Build.PL prereqs. So like... it's giving useful, honestly โจ
And as always, if you do not care about any of this, or you only ever use Perl 5.8 anyway, then you can disable ConstantPragmaHash from your .perlcriticrc the usual way (see "CONFIGURATION" in Perl::Critic) ๐
[-Compatibility::ConstantPragmaHash]
๐ SEE ALSO ๐
Perl::Critic::PulpPerl::CriticPerl::Critic::Policy::Compatibility::ConstantLeadingUnderscorePerl::Critic::Policy::ValuesAndExpressions::ProhibitConstantPragmaPerl::Critic::Policy::Modules::RequirePerlVersionconstant- "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/>.
and that's the whole page, bestie ๐ซถ โ brought to you by perl v5.40.1 with the timestamp 2025-10-21, signing off under the name Perl::...gmaHash(3pm) (yes the name is literally cut off mid-word, we love her for that ๐
โจ)
๐ฆ where this page comes from
- ubuntu resolute: libperl-critic-pulp-perl 100-1 ยท GPL-3.0-or-later
- ubuntu stonking: libperl-critic-pulp-perl 100-1 ยท GPL-3.0-or-later
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms