Perl::...ashKeys(3pm)
πΈ Perl::Critic::Policy::ValuesAndExpressions::ProhibitDuplicateHashKeys π
okay bestie, so this page lives in the User Contributed Perl Documentation manual, and it's a Perl::Critic policy. What's it about? disallow duplicate literal hash keys. That's it. That's the whole vibe check. β¨
π NAME π
Perl::Critic::Policy::ValuesAndExpressions::ProhibitDuplicateHashKeys β no cap, this one's job is to disallow duplicate literal hash keys.
π¦ DESCRIPTION π¦
So this policy is part of the Perl::Critic::Pulp add-on. π It snitches on duplicate literal hash keys β whether they show up in a hash assignment or in an anonymous hashref. Like, it will literally catch this:
my %hash = (red => 1,
green => 2,
red => 3, # bad
);
my $hashref = { red => 1,
red => 3, # bad
};
Writing duplicate literal keys is probably a mistake, or honestly just too much cut and paste energy π β and when the values are different, it makes it super unclear to human readers what was actually meant. Based on that, this policy sits under the "bugs" theme and is medium severity (peep "POLICY THEMES" in Perl::Critic for the deets).
Now here's the thing bestie β Perl is totally happy to run code like the above. π«Ά The value of the last "red" is what gets stored. And as runtime behaviour? That's actually kind of iconic, because it means you can give defaults that further values from a caller (or whatever) get to replace. For example:
sub new {
my $class = shift;
return bless { foo => 'default',
bar => 'default',
@_ }, $class;
}
MyClass->new (foo => 'caller value'); # overriding 'default'
β¨ Expressions β¨
Expressions sitting inside a hash list cannot be checked in general. Sorry babe, it's just not possible. π Some concatenations of literals do get recognised though β although ngl, they're probably pretty unusual.
my %hash = (ab => 1,
'a'.'b' => 2); # bad
my %hash = (__PACKAGE__.'a' => 1,
__PACKAGE__.'a' => 2); # bad
Function calls and friends inside a list might return an odd or an even number of values β chaotic, I know π. So fat commas => get taken as indicating a key when in doubt.
my %hash = (blah() => 1, # guided by =>
a => 2,
a => 3); # bad
my %hash = (blah(),
a => 2, # guided by =>
a => 3); # bad
A hash substitution is always an even number of arguments, periodt:
my %hash = (a => 1,
%blah, # even number
a => 5); # bad, duplicate
And qw() words get recognised too π
my %hash = (qw(foo value1
foo value2)); # bad
π Disabling π
If you literally do not care about any of this, you can always disable "ProhibitDuplicateHashKeys" from your .perlcriticrc file in the usual way (see "CONFIGURATION" in Perl::Critic) β like so:
[-ValuesAndExpressions::ProhibitDuplicateHashKeys]
π SEE ALSO π
Perl::Critic::PulpPerl::CriticPerl::Critic::Policy::CodeLayout::RequireTrailingCommasPerl::Critic::Policy::CodeLayout::RequireTrailingCommaAtNewline
π§ HOME PAGE π§
http://user42.tuxfamily.org/perl-critic-pulp/index.html
π COPYRIGHT π
Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2020, 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/>.
okay that's the whole page, bestie, you're so welcome π go forth and not write duplicate hash keys, mwah π
πΈ that's a wrap on the perl v5.40.1 docs, dated 2025-10-21 β slay, bye β¨
π¦ 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