Modified source files and compiled any and armel versions of packages
[pkg-perl] / deb-src / libperl-critic-perl / libperl-critic-perl-1.088 / lib / Perl / Critic / Policy / CodeLayout / ProhibitQuotedWordLists.pm
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) $
4 #   $Author: clonezone $
5 # $Revision: 2489 $
6 ##############################################################################
7
8 package Perl::Critic::Policy::CodeLayout::ProhibitQuotedWordLists;
9
10 use 5.006001;
11 use strict;
12 use warnings;
13 use Readonly;
14
15 use Perl::Critic::Utils qw{ :characters :severities };
16 use base 'Perl::Critic::Policy';
17
18 our $VERSION = '1.088';
19
20 #-----------------------------------------------------------------------------
21
22 Readonly::Scalar my $DESC => q{List of quoted literal words};
23 Readonly::Scalar my $EXPL => q{Use 'qw()' instead};
24
25 #-----------------------------------------------------------------------------
26
27 sub supported_parameters {
28     return (
29         {
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',
34             integer_minimum => 1,
35         },
36     );
37 }
38
39 sub default_severity { return $SEVERITY_LOW          }
40 sub default_themes   { return qw( core cosmetic )    }
41 sub applies_to       { return 'PPI::Structure::List' }
42
43 #-----------------------------------------------------------------------------
44
45 sub violates {
46     my ( $self, $elem, undef ) = @_;
47
48     #Don't worry about subroutine calls
49     my $sib = $elem->sprevious_sibling();
50     return if !$sib;
51     return if $sib->isa('PPI::Token::Word');
52     return if $sib->isa('PPI::Token::Symbol');
53
54     #Get the list elements
55     my $expr = $elem->schild(0);
56     return if !$expr;
57     my @children = $expr->schildren();
58     return if !@children;
59
60     my $count = 0;
61     for my $child ( @children ) {
62         next if $child->isa('PPI::Token::Operator')  && $child eq $COMMA;
63
64         #All elements must be literal strings,
65         #of non-zero length, with no whitespace
66
67         return if ! _is_literal($child);
68
69         my $string = $child->string();
70         return if $string =~ m{ \s }mx;
71         return if $string eq $EMPTY;
72         $count++;
73     }
74
75     #Were there enough?
76     return if $count < $self->{_min_elements};
77
78     #If we get here, then all elements were literals
79     return $self->violation( $DESC, $EXPL, $elem );
80 }
81
82 sub _is_literal {
83     my $elem = shift;
84     return $elem->isa('PPI::Token::Quote::Single')
85         || $elem->isa('PPI::Token::Quote::Literal');
86 }
87
88 1;
89
90 __END__
91
92 =pod
93
94 =head1 NAME
95
96 Perl::Critic::Policy::CodeLayout::ProhibitQuotedWordLists - Write C<qw(foo bar baz)> instead of C<('foo', 'bar', 'baz')>.
97
98 =head1 AFFILIATION
99
100 This Policy is part of the core L<Perl::Critic> distribution.
101
102
103 =head1 DESCRIPTION
104
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.
109
110   @list = ('foo', 'bar', 'baz');  #not ok
111   @list = qw(foo bar baz);        #ok
112
113 =head1 CONFIGURATION
114
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:
120
121   [CodeLayout::ProhibitQuotedWordLists]
122   min_elements = 4
123
124 This would cause this policy to only complain about lists containing
125 four or more words.
126
127 =head1 NOTES
128
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.
133
134 =head1 IMPORTANT CHANGES
135
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.
140
141
142 =head1 AUTHOR
143
144 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
145
146 Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer.  All rights reserved.
147
148 =head1 COPYRIGHT
149
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.
153
154 =cut
155
156 # Local Variables:
157 #   mode: cperl
158 #   cperl-indent-level: 4
159 #   fill-column: 78
160 #   indent-tabs-mode: nil
161 #   c-indentation-style: bsd
162 # End:
163 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :