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