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 / ValuesAndExpressions / ProhibitInterpolationOfLiterals.pm
1 ##############################################################################
2 #      $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/ValuesAndExpressions/ProhibitInterpolationOfLiterals.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::ValuesAndExpressions::ProhibitInterpolationOfLiterals;
9
10 use 5.006001;
11 use strict;
12 use warnings;
13 use Readonly;
14
15 use List::MoreUtils qw(any);
16
17 use Perl::Critic::Utils qw{ :characters :severities :data_conversion };
18 use base 'Perl::Critic::Policy';
19
20 our $VERSION = '1.088';
21
22 #-----------------------------------------------------------------------------
23
24 Readonly::Scalar my $DESC => q{Useless interpolation of literal string};
25 Readonly::Scalar my $EXPL => [51];
26
27 #-----------------------------------------------------------------------------
28
29 sub supported_parameters {
30     return (
31         {
32             name               => 'allow',
33             description        =>
34                 'Kinds of delimiters to permit, e.g. "qq{", "qq(", "qq[", "qq/".',
35             default_string     => $EMPTY,
36             parser             => \&_parse_allow,
37         },
38     );
39 }
40
41 sub default_severity { return $SEVERITY_LOWEST        }
42 sub default_themes   { return qw( core pbp cosmetic ) }
43 sub applies_to       { return qw(PPI::Token::Quote::Double
44                                  PPI::Token::Quote::Interpolate) }
45
46 #-----------------------------------------------------------------------------
47
48 Readonly::Scalar my $MAX_SPECIFICATION_LENGTH => 3;
49
50 sub _parse_allow {
51     my ($self, $parameter, $config_string) = @_;
52
53     my @allow;
54
55     if (defined $config_string) {
56         @allow = words_from_string( $config_string );
57         #Try to be forgiving with the configuration...
58         for (@allow) {
59             m{ \A qq }mx || ($_ = 'qq' . $_)
60         }  #Add 'qq'
61         for (@allow) {
62             (length $_ <= $MAX_SPECIFICATION_LENGTH) || chop
63         }    #Chop closing char
64     }
65
66     $self->{_allow} = \@allow;
67
68     return;
69 }
70
71 #-----------------------------------------------------------------------------
72
73 sub violates {
74     my ( $self, $elem, undef ) = @_;
75
76     # Skip if this string needs interpolation
77     return if _has_interpolation($elem);
78
79     # Overlook allowed quote styles
80     return if any { $elem =~ m{ \A \Q$_\E }mx } @{ $self->{_allow} };
81
82     # Must be a violation
83     return $self->violation( $DESC, $EXPL, $elem );
84 }
85
86 #-----------------------------------------------------------------------------
87
88 sub _has_interpolation {
89     my $elem = shift;
90     return $elem =~ m{ (?<!\\) [\$\@] \S+ }mx      #Contains unescaped $. or @.
91         || $elem =~ m{ \\[tnrfbae0xcNLuLUEQ] }mx;   #Containts escaped metachars
92 }
93
94 1;
95
96 __END__
97
98 #-----------------------------------------------------------------------------
99
100 =pod
101
102 =head1 NAME
103
104 Perl::Critic::Policy::ValuesAndExpressions::ProhibitInterpolationOfLiterals - Always use single quotes for literal strings.
105
106 =head1 AFFILIATION
107
108 This Policy is part of the core L<Perl::Critic> distribution.
109
110
111 =head1 DESCRIPTION
112
113 Don't use double-quotes or C<qq//> if your string doesn't require
114 interpolation.  This saves the interpreter a bit of work and it lets
115 the reader know that you really did intend the string to be literal.
116
117   print "foobar";     #not ok
118   print 'foobar';     #ok
119   print qq/foobar/;   #not ok
120   print q/foobar/;    #ok
121
122   print "$foobar";    #ok
123   print "foobar\n";   #ok
124   print qq/$foobar/;  #ok
125   print qq/foobar\n/; #ok
126
127   print qq{$foobar};  #preferred
128   print qq{foobar\n}; #preferred
129
130 =head1 CONFIGURATION
131
132 The types of quoting styles to exempt from this policy can be
133 configured via the C<allow> option.  This must be a
134 whitespace-delimited combination of some or all of the following
135 styles: C<qq{}>, C<qq()>, C<qq[]>, and C<qq//>.
136
137 This is useful because some folks have configured their editor to
138 apply special syntax highlighting within certain styles of quotes.
139 For example, you can tweak C<vim> to use SQL highlighting for
140 everything that appears within C<qq{}> or C<qq[]> quotes.  But if
141 those strings are literal, Perl::Critic will complain.  To prevent
142 this, put the following in your F<.perlcriticrc> file:
143
144   [ValuesAndExpressions::ProhibitInterpolationOfLiterals]
145   allow = qq{} qq[]
146
147 =head1 SEE ALSO
148
149 L<Perl::Critic::Policy::ValuesAndExpressions::RequireInterpolationOfMetachars>
150
151 =head1 AUTHOR
152
153 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
154
155 =head1 COPYRIGHT
156
157 Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer.  All rights reserved.
158
159 This program is free software; you can redistribute it and/or modify
160 it under the same terms as Perl itself.  The full text of this license
161 can be found in the LICENSE file included with this module.
162
163 =cut
164
165 # Local Variables:
166 #   mode: cperl
167 #   cperl-indent-level: 4
168 #   fill-column: 78
169 #   indent-tabs-mode: nil
170 #   c-indentation-style: bsd
171 # End:
172 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :