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