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::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 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