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