Kevin Ryde > Perl-Critic-Pulp > Perl::Critic::Policy::ValuesAndExpressions::ProhibitDuplicateHashKeys



Annotate this POD



New  1
Open  0
View/Report Bugs
Module Version: 81   Source  


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


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. Doing this at runtime 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 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


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),



Perl::Critic::Pulp, Perl::Critic

Perl::Critic::Policy::CodeLayout::RequireTrailingCommas, Perl::Critic::Policy::CodeLayout::RequireTrailingCommaAtNewline



Copyright 2011, 2012, 2013 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 <>.

syntax highlighting: