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