Perl::Critic::Policy::CodeLayout::ProhibitHashBarewords - Perl::Critic::Policy::CodeLayout::ProhibitHashBarewords documentation


Perl-Critic-Itch documentation Contained in the Perl-Critic-Itch distribution.

Index


Code Index:

NAME

Top

Perl::Critic::Policy::CodeLayout::ProhibitHashBarewords

AFFILIATION

Top

This policy is part of Perl::Critic::Itch.

VERSION

Top

0.07

DESCRIPTION

Top

This Policy forces (single) quotes on all hash keys barewords.

When specifying constant string hash keys, you should use (single) quotes. E.g., $my_hash{'some_key'}

This is the appropriate choice because it results in consistent formatting and if you forget to use quotes sometimes, you have to remember to add them when your key contains internal hyphens, spaces, or other special characters.

Quoted keys are also more likely to be syntax-highlighted by your editor.

INTERFACE

Top

Standard for a Perl::Critic::Policy.

ACKNOWLEDGMENTS

Top

Thanks to

* Jose Carlos Pereira for pointing me in the right direction!
* All Perl::Critic::Policy contributors. Their code examples were quite useful.

Perl-Critic-Itch documentation Contained in the Perl-Critic-Itch distribution.

package Perl::Critic::Policy::CodeLayout::ProhibitHashBarewords;

use strict;
use warnings;
use Perl::Critic::Utils;
use base 'Perl::Critic::Policy';

our $VERSION = '0.07';


sub default_severity { return $SEVERITY_MEDIUM }
sub default_themes   { return qw(itch) }
sub applies_to       { return 'PPI::Token::Word' }

sub violates {
    my ( $self, $elem ) = @_;

    #we only want the check hash keys
    return if !is_hash_key($elem);

    return if is_method_call($elem);
    return if is_function_call($elem);

    my $desc = q{Hash key with bareword};
    my $expl = q{Place quotes on all hash key barewords};
    return $self->violation( $desc, $expl, $elem );
}

1;

__END__