1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/CodeLayout/ProhibitQuotedWordLists.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
8 package Perl::Critic::Policy::CodeLayout::ProhibitQuotedWordLists;
15 use Perl::Critic::Utils qw{ :characters :severities };
16 use base 'Perl::Critic::Policy';
18 our $VERSION = '1.088';
20 #-----------------------------------------------------------------------------
22 Readonly::Scalar my $DESC => q{List of quoted literal words};
23 Readonly::Scalar my $EXPL => q{Use 'qw()' instead};
25 #-----------------------------------------------------------------------------
27 sub supported_parameters {
30 name => 'min_elements',
31 description => 'The minimum number of words in a list that will be complained about.',
32 default_string => '2',
33 behavior => 'integer',
39 sub default_severity { return $SEVERITY_LOW }
40 sub default_themes { return qw( core cosmetic ) }
41 sub applies_to { return 'PPI::Structure::List' }
43 #-----------------------------------------------------------------------------
46 my ( $self, $elem, undef ) = @_;
48 #Don't worry about subroutine calls
49 my $sib = $elem->sprevious_sibling();
51 return if $sib->isa('PPI::Token::Word');
52 return if $sib->isa('PPI::Token::Symbol');
54 #Get the list elements
55 my $expr = $elem->schild(0);
57 my @children = $expr->schildren();
61 for my $child ( @children ) {
62 next if $child->isa('PPI::Token::Operator') && $child eq $COMMA;
64 #All elements must be literal strings,
65 #of non-zero length, with no whitespace
67 return if ! _is_literal($child);
69 my $string = $child->string();
70 return if $string =~ m{ \s }mx;
71 return if $string eq $EMPTY;
76 return if $count < $self->{_min_elements};
78 #If we get here, then all elements were literals
79 return $self->violation( $DESC, $EXPL, $elem );
84 return $elem->isa('PPI::Token::Quote::Single')
85 || $elem->isa('PPI::Token::Quote::Literal');
96 Perl::Critic::Policy::CodeLayout::ProhibitQuotedWordLists - Write C<qw(foo bar baz)> instead of C<('foo', 'bar', 'baz')>.
100 This Policy is part of the core L<Perl::Critic> distribution.
105 Conway doesn't mention this, but I think C<qw()> is an underused
106 feature of Perl. Whenever you need to declare a list of one-word
107 literals, the C<qw()> operator is wonderfully concise, and makes
108 it easy to add to the list in the future.
110 @list = ('foo', 'bar', 'baz'); #not ok
111 @list = qw(foo bar baz); #ok
115 This policy can be configured to only pay attention to word lists with
116 at least a particular number of elements. By default, this value is 2,
117 which means that lists containing zero or one elements are ignored.
118 The minimum list size to be looked at can be specified by giving a
119 value for C<min_elements> in F<.perlcriticrc> like this:
121 [CodeLayout::ProhibitQuotedWordLists]
124 This would cause this policy to only complain about lists containing
129 In the PPI parlance, a "list" is almost anything with parentheses.
130 I've tried to make this Policy smart by targeting only "lists" that
131 could be sensibly expressed with C<qw()>. However, there may be some
132 edge cases that I haven't covered. If you find one, send me a note.
134 =head1 IMPORTANT CHANGES
136 This policy was formerly called C<RequireQuotedWords> which seemed a
137 little counter-intuitive. If you get lots of "Cannot load policy
138 module" errors, then you probably need to change C<RequireQuotedWords>
139 to C<ProhibitQuotedWordLists> in your F<.perlcriticrc> file.
144 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
146 Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer. All rights reserved.
150 This program is free software; you can redistribute it and/or modify
151 it under the same terms as Perl itself. The full text of this license
152 can be found in the LICENSE file included with this module.
158 # cperl-indent-level: 4
160 # indent-tabs-mode: nil
161 # c-indentation-style: bsd
163 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :