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::Pulp
  • Perl::Critic
  • Perl::Critic::Policy::CodeLayout::RequireTrailingCommas
  • Perl::Critic::Policy::CodeLayout::RequireTrailingCommaAtNewline

🧚 HOME PAGE 🧚

http://user42.tuxfamily.org/perl-critic-pulp/index.html


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