NAME

Perl::Critic::Policy::ValuesAndExpressions::ProhibitDuplicateHashKeys - disallow duplicate literal hash keys

DESCRIPTION

This policy is part of the Perl::Critic::Pulp add-on. It reports duplicate literal hash keys in a hash assignment or anonymous hashref.

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 too much cut and paste, and if the values are different will make it unclear to human readers what was meant. On that basis this policy is under the "bugs" theme and medium severity (see "POLICY THEMES" in Perl::Critic).

Perl is happy to run code like the above. The value of the last "red" is stored. As runtime behaviour, this is good since you can give defaults which further values from a caller or similar can replace. For example,

sub new {
  my $class = shift;
  return bless { foo => 'default',
                 bar => 'default',
                 @_ }, $class;
}

MyClass->new (foo => 'caller value'); # overriding 'default'

Expressions

Expressions within a hash list cannot be checked in general. Some concatenations of literals are recognised though they're probably unusual.

my %hash = (ab      => 1,
            'a'.'b' => 2);  # bad

my %hash = (__PACKAGE__.'a' => 1,
            __PACKAGE__.'a' => 2);  # bad

Function calls etc within a list might return an odd or even number of values. Fat commas => are 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,

my %hash = (a         => 1,
            %blah,           # even number
            a         => 5); # bad, duplicate

qw() words are recognised too

my %hash = (qw(foo value1
               foo value2));  # bad

Disabling

If you don't care about this you can always disable ProhibitDuplicateHashKeys from your .perlcriticrc file in the usual way (see "CONFIGURATION" in Perl::Critic),

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

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/>.