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