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