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