Login
Change name of option in ProhibitInterpolationOfLiterals to
[gknop/Perl-Critic.git] / lib / Perl / Critic / Policy / ValuesAndExpressions / ProhibitInterpolationOfLiterals.pm
CommitLineData
6036a254 1##############################################################################
dff08b70
JRT
2# $URL$
3# $Date$
4# $Author$
5# $Revision$
6036a254 6##############################################################################
59b05e08
JRT
7
8package Perl::Critic::Policy::ValuesAndExpressions::ProhibitInterpolationOfLiterals;
9
df6dee2b 10use 5.006001;
59b05e08
JRT
11use strict;
12use warnings;
c680a9c9
ES
13use Readonly;
14
fd5bd7b5 15use List::MoreUtils qw(any);
005d2cb9 16
8e1d6b24 17use Perl::Critic::Utils qw{ :characters :severities :data_conversion };
59b05e08
JRT
18use base 'Perl::Critic::Policy';
19
a8da49fd 20our $VERSION = '1.088';
59b05e08 21
6036a254 22#-----------------------------------------------------------------------------
86bf4aa6 23
c680a9c9
ES
24Readonly::Scalar my $DESC => q{Useless interpolation of literal string};
25Readonly::Scalar my $EXPL => [51];
59b05e08 26
6036a254 27#-----------------------------------------------------------------------------
59b05e08 28
8e1d6b24
ES
29sub 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 },
dcfc3546 38 {
06f6fa23 39 name => 'allow_if_string_contains_single_quote',
dcfc3546
ES
40 description =>
41 q<If the string contains ' characters, allow "" to quote it.>,
42 default_string => '0',
43 behavior => 'boolean',
44 },
8e1d6b24
ES
45 );
46}
47
c680a9c9
ES
48sub default_severity { return $SEVERITY_LOWEST }
49sub default_themes { return qw( core pbp cosmetic ) }
50sub applies_to { return qw(PPI::Token::Quote::Double
51 PPI::Token::Quote::Interpolate) }
86bf4aa6 52
6036a254 53#-----------------------------------------------------------------------------
86bf4aa6 54
05b5f925
ES
55Readonly::Scalar my $MAX_SPECIFICATION_LENGTH => 3;
56
8e1d6b24
ES
57sub _parse_allow {
58 my ($self, $parameter, $config_string) = @_;
85da2bf2 59
8e1d6b24 60 my @allow;
59b05e08 61
8e1d6b24
ES
62 if (defined $config_string) {
63 @allow = words_from_string( $config_string );
0a6f07d0
AL
64 #Try to be forgiving with the configuration...
65 for (@allow) {
66 m{ \A qq }mx || ($_ = 'qq' . $_)
67 } #Add 'qq'
68 for (@allow) {
05b5f925 69 (length $_ <= $MAX_SPECIFICATION_LENGTH) || chop
0a6f07d0 70 } #Chop closing char
59b05e08
JRT
71 }
72
8e1d6b24
ES
73 $self->{_allow} = \@allow;
74
75 return;
59b05e08
JRT
76}
77
6036a254 78#-----------------------------------------------------------------------------
59b05e08
JRT
79
80sub violates {
e5f6c18d 81 my ( $self, $elem, undef ) = @_;
59b05e08 82
fd5bd7b5 83 # Skip if this string needs interpolation
45acb16b
CD
84 return if _has_interpolation($elem);
85
8e1d6b24 86 # Overlook allowed quote styles
fd5bd7b5
JRT
87 return if any { $elem =~ m{ \A \Q$_\E }mx } @{ $self->{_allow} };
88
dcfc3546 89 # If the flag is set, allow "I'm here".
06f6fa23 90 if ( $self->{_allow_if_string_contains_single_quote} ) {
dcfc3546
ES
91 return if index ($elem, $QUOTE) >= 0;
92 }
93
fd5bd7b5 94 # Must be a violation
c680a9c9 95 return $self->violation( $DESC, $EXPL, $elem );
59b05e08
JRT
96}
97
fd5bd7b5
JRT
98#-----------------------------------------------------------------------------
99
59b05e08 100sub _has_interpolation {
45acb16b 101 my $elem = shift;
dcfc3546
ES
102 return $elem =~ m{ (?<!\\) [\$\@] \S+ }mx #Contains unescaped $. or @.
103 || $elem =~ m{ \\[tnrfbae0xcNLuLUEQ] }mx; #Containts escaped metachars
59b05e08
JRT
104}
105
1061;
107
108__END__
109
6036a254 110#-----------------------------------------------------------------------------
86bf4aa6 111
59b05e08
JRT
112=pod
113
114=head1 NAME
115
a1b75332 116Perl::Critic::Policy::ValuesAndExpressions::ProhibitInterpolationOfLiterals - Always use single quotes for literal strings.
59b05e08 117
dcfc3546 118
af93c316
ES
119=head1 AFFILIATION
120
11f53956
ES
121This Policy is part of the core L<Perl::Critic|Perl::Critic>
122distribution.
af93c316
ES
123
124
59b05e08
JRT
125=head1 DESCRIPTION
126
127Don't use double-quotes or C<qq//> if your string doesn't require
128interpolation. This saves the interpreter a bit of work and it lets
129the reader know that you really did intend the string to be literal.
130
11f53956
ES
131 print "foobar"; #not ok
132 print 'foobar'; #ok
133 print qq/foobar/; #not ok
134 print q/foobar/; #ok
59b05e08 135
11f53956
ES
136 print "$foobar"; #ok
137 print "foobar\n"; #ok
138 print qq/$foobar/; #ok
139 print qq/foobar\n/; #ok
59b05e08 140
11f53956
ES
141 print qq{$foobar}; #preferred
142 print qq{foobar\n}; #preferred
59b05e08 143
dcfc3546
ES
144Use of double-quotes might be reasonable if the string contains single
145quote (') characters:
146
147 print "it's me"; # ok, if configuration flag set
148
149
8a25c8a0
ES
150=head1 CONFIGURATION
151
152The types of quoting styles to exempt from this policy can be
153configured via the C<allow> option. This must be a
154whitespace-delimited combination of some or all of the following
155styles: C<qq{}>, C<qq()>, C<qq[]>, and C<qq//>.
156
157This is useful because some folks have configured their editor to
158apply special syntax highlighting within certain styles of quotes.
159For example, you can tweak C<vim> to use SQL highlighting for
59b05e08
JRT
160everything that appears within C<qq{}> or C<qq[]> quotes. But if
161those strings are literal, Perl::Critic will complain. To prevent
162this, put the following in your F<.perlcriticrc> file:
163
11f53956
ES
164 [ValuesAndExpressions::ProhibitInterpolationOfLiterals]
165 allow = qq{} qq[]
59b05e08 166
06f6fa23 167The flag C<allow_if_string_contains_single_quote> permits
dcfc3546
ES
168double-quoted strings if the string contains a single quote (')
169character. It defaults to off; to turn it on put the following in
170your F<.perlcriticrc> file:
171
172 [ValuesAndExpressions::ProhibitInterpolationOfLiterals]
173 allow_double_quote_if_string_contains_single = 1
174
175
59b05e08
JRT
176=head1 SEE ALSO
177
11f53956 178L<Perl::Critic::Policy::ValuesAndExpressions::RequireInterpolationOfMetachars|Perl::Critic::Policy::ValuesAndExpressions::RequireInterpolationOfMetachars>
59b05e08
JRT
179
180=head1 AUTHOR
181
182Jeffrey Ryan Thalhammer <thaljef@cpan.org>
183
184=head1 COPYRIGHT
185
20dfddeb 186Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer. All rights reserved.
59b05e08
JRT
187
188This program is free software; you can redistribute it and/or modify
189it under the same terms as Perl itself. The full text of this license
190can be found in the LICENSE file included with this module.
191
192=cut
737d3b65
CD
193
194# Local Variables:
195# mode: cperl
196# cperl-indent-level: 4
197# fill-column: 78
198# indent-tabs-mode: nil
199# c-indentation-style: bsd
200# End:
96fed375 201# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :