Login
* Finished adding severity method to all modules (was "priority").
[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;
13use Perl::Critic::Violation;
14use base 'Perl::Critic::Policy';
15
16our $VERSION = '0.13';
17$VERSION = eval $VERSION; ## no critic
18
19my $desc = q{Useless interpolation of literal string};
20my $expl = [51];
21
22#---------------------------------------------------------------------------
23
24sub new {
25 my ( $class, %args ) = @_;
26 my $self = bless {}, $class;
27 $self->{_allow} = [];
28
29 #Set configuration, if defined
30 if ( defined $args{allow} ) {
31 my @allow = split m{ \s+ }mx, $args{allow};
32 #Try to be forgiving with the configuration...
33 for (@allow) { m{ \A qq }mx || ($_ = 'qq' . $_) } #Add 'qq'
34 for (@allow) { (length $_ <= 3) || chop } #Chop closing char
35 $self->{_allow} = \@allow;
36 }
37
38 return $self;
39}
40
41#---------------------------------------------------------------------------
42
bf159007
JRT
43sub applies_to {
44 return 'PPI::Token::Quote::Double', 'PPI::Token::Quote::Interpolate';
45}
46
59b05e08
JRT
47sub violates {
48 my ( $self, $elem, $doc ) = @_;
59b05e08
JRT
49
50 #Overlook allowed quote styles
51 for my $allowed ( @{ $self->{_allow} } ) {
52 return if $elem =~ m{ \A \Q$allowed\E }mx;
53 }
54
55 if ( !_has_interpolation($elem) ) {
56 return Perl::Critic::Violation->new( $desc, $expl, $elem->location() );
57 }
58 return; #ok!
59}
60
61sub _has_interpolation {
62 my $elem = shift || return;
63 return $elem =~ m{ (?<!\\) [\$\@] \S+ }mx #Contains unescaped $. or @.
64 || $elem =~ m{ \\[tnrfae0xcNLuLUEQ] }mx; #Containts escaped metachars
65}
66
671;
68
69__END__
70
71=pod
72
73=head1 NAME
74
75Perl::Critic::Policy::ValuesAndExpressions::ProhibitInterpolationOfLiterals
76
77=head1 DESCRIPTION
78
79Don't use double-quotes or C<qq//> if your string doesn't require
80interpolation. This saves the interpreter a bit of work and it lets
81the reader know that you really did intend the string to be literal.
82
83 print "foobar"; #not ok
84 print 'foobar'; #ok
85 print qq/foobar/; #not ok
86 print q/foobar/; #ok
87
88 print "$foobar"; #ok
89 print "foobar\n"; #ok
90 print qq/$foobar/; #ok
91 print qq/foobar\n/; #ok
92
93 print qq{$foobar}; #preferred
94 print qq{foobar\n}; #preferred
95
96=head1 CONSTRUCTOR
97
98This Policy accepts an additional key-value pair in the constructor,
99The key is 'allow' and the value is a string of quote styles
100that are exempt from this policy. Valid styles are C<qq{}>, C<qq()>,
101C<qq[]>, and C<qq//>. Multiple styles should be separated by
102whitespace. This is useful because some folks have configured their
103editor to apply special syntax highlighting within certain styles of
104quotes. For example, you can tweak C<vim> to use SQL highlighting for
105everything that appears within C<qq{}> or C<qq[]> quotes. But if
106those strings are literal, Perl::Critic will complain. To prevent
107this, put the following in your F<.perlcriticrc> file:
108
109 [ValuesAndExpressions::ProhibitInterpolationOfLiterals]
110 allow = qq{} qq[]
111
112=head1 SEE ALSO
113
114L<Perl::Critic::Policy::ValuesAndExpressions::RequireInterpolationOfMetachars>
115
116=head1 AUTHOR
117
118Jeffrey Ryan Thalhammer <thaljef@cpan.org>
119
120=head1 COPYRIGHT
121
122Copyright (c) 2005 Jeffrey Ryan Thalhammer. All rights reserved.
123
124This program is free software; you can redistribute it and/or modify
125it under the same terms as Perl itself. The full text of this license
126can be found in the LICENSE file included with this module.
127
128=cut