Login
Put severity setting in the perlcriticrc file
[gknop/Perl-Critic.git] / lib / Perl / Critic / Policy / ValuesAndExpressions / ProhibitInterpolationOfLiterals.pm
CommitLineData
dff08b70
JRT
1#######################################################################
2# $URL$
3# $Date$
4# $Author$
5# $Revision$
6########################################################################
59b05e08
JRT
7
8package Perl::Critic::Policy::ValuesAndExpressions::ProhibitInterpolationOfLiterals;
9
10use strict;
11use warnings;
12use Perl::Critic::Utils;
59b05e08
JRT
13use base 'Perl::Critic::Policy';
14
a7340650 15our $VERSION = 0.22;
59b05e08 16
86bf4aa6
JRT
17#---------------------------------------------------------------------------
18
59b05e08
JRT
19my $desc = q{Useless interpolation of literal string};
20my $expl = [51];
21
22#---------------------------------------------------------------------------
23
9f1d5408 24sub default_severity { return $SEVERITY_LOWEST }
faa35de4
JRT
25sub default_themes { return qw(pbp cosmetic) }
26sub applies_to { return qw(PPI::Token::Quote::Double
27 PPI::Token::Quote::Interpolate) }
86bf4aa6
JRT
28
29#---------------------------------------------------------------------------
30
59b05e08
JRT
31sub new {
32 my ( $class, %args ) = @_;
33 my $self = bless {}, $class;
34 $self->{_allow} = [];
35
36 #Set configuration, if defined
37 if ( defined $args{allow} ) {
0a6f07d0
AL
38 my @allow = split m{ \s+ }mx, $args{allow};
39 #Try to be forgiving with the configuration...
40 for (@allow) {
41 m{ \A qq }mx || ($_ = 'qq' . $_)
42 } #Add 'qq'
43 for (@allow) {
44 (length $_ <= 3) || chop
45 } #Chop closing char
46 $self->{_allow} = \@allow;
59b05e08
JRT
47 }
48
49 return $self;
50}
51
52#---------------------------------------------------------------------------
53
54sub violates {
e5f6c18d 55 my ( $self, $elem, undef ) = @_;
59b05e08
JRT
56
57 #Overlook allowed quote styles
58 for my $allowed ( @{ $self->{_allow} } ) {
59 return if $elem =~ m{ \A \Q$allowed\E }mx;
60 }
61
45acb16b
CD
62 return if _has_interpolation($elem);
63
64 return $self->violation( $desc, $expl, $elem );
59b05e08
JRT
65}
66
67sub _has_interpolation {
45acb16b 68 my $elem = shift;
59b05e08 69 return $elem =~ m{ (?<!\\) [\$\@] \S+ }mx #Contains unescaped $. or @.
45acb16b 70 || $elem =~ m{ \\[tnrfae0xcNLuLUEQ] }mx; #Containts escaped metachars
59b05e08
JRT
71}
72
731;
74
75__END__
76
86bf4aa6
JRT
77#---------------------------------------------------------------------------
78
59b05e08
JRT
79=pod
80
81=head1 NAME
82
83Perl::Critic::Policy::ValuesAndExpressions::ProhibitInterpolationOfLiterals
84
85=head1 DESCRIPTION
86
87Don't use double-quotes or C<qq//> if your string doesn't require
88interpolation. This saves the interpreter a bit of work and it lets
89the reader know that you really did intend the string to be literal.
90
91 print "foobar"; #not ok
92 print 'foobar'; #ok
93 print qq/foobar/; #not ok
94 print q/foobar/; #ok
95
96 print "$foobar"; #ok
97 print "foobar\n"; #ok
98 print qq/$foobar/; #ok
99 print qq/foobar\n/; #ok
100
101 print qq{$foobar}; #preferred
102 print qq{foobar\n}; #preferred
103
104=head1 CONSTRUCTOR
105
106This Policy accepts an additional key-value pair in the constructor,
107The key is 'allow' and the value is a string of quote styles
108that are exempt from this policy. Valid styles are C<qq{}>, C<qq()>,
109C<qq[]>, and C<qq//>. Multiple styles should be separated by
110whitespace. This is useful because some folks have configured their
111editor to apply special syntax highlighting within certain styles of
112quotes. For example, you can tweak C<vim> to use SQL highlighting for
113everything that appears within C<qq{}> or C<qq[]> quotes. But if
114those strings are literal, Perl::Critic will complain. To prevent
115this, put the following in your F<.perlcriticrc> file:
116
117 [ValuesAndExpressions::ProhibitInterpolationOfLiterals]
118 allow = qq{} qq[]
119
120=head1 SEE ALSO
121
122L<Perl::Critic::Policy::ValuesAndExpressions::RequireInterpolationOfMetachars>
123
124=head1 AUTHOR
125
126Jeffrey Ryan Thalhammer <thaljef@cpan.org>
127
128=head1 COPYRIGHT
129
c3c88e54 130Copyright (c) 2005-2006 Jeffrey Ryan Thalhammer. All rights reserved.
59b05e08
JRT
131
132This program is free software; you can redistribute it and/or modify
133it under the same terms as Perl itself. The full text of this license
134can be found in the LICENSE file included with this module.
135
136=cut
737d3b65
CD
137
138# Local Variables:
139# mode: cperl
140# cperl-indent-level: 4
141# fill-column: 78
142# indent-tabs-mode: nil
143# c-indentation-style: bsd
144# End:
dbb78cdc 145# ex: set ts=8 sts=4 sw=4 expandtab :