Login
Importing Perl-Critic-0.13.
[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
37sub violates {
38 my ( $self, $elem, $doc ) = @_;
39 $elem->isa('PPI::Token::Quote::Double')
40 || $elem->isa('PPI::Token::Quote::Interpolate')
41 || return;
42
43 #Overlook allowed quote styles
44 for my $allowed ( @{ $self->{_allow} } ) {
45 return if $elem =~ m{ \A \Q$allowed\E }mx;
46 }
47
48 if ( !_has_interpolation($elem) ) {
49 return Perl::Critic::Violation->new( $desc, $expl, $elem->location() );
50 }
51 return; #ok!
52}
53
54sub _has_interpolation {
55 my $elem = shift || return;
56 return $elem =~ m{ (?<!\\) [\$\@] \S+ }mx #Contains unescaped $. or @.
57 || $elem =~ m{ \\[tnrfae0xcNLuLUEQ] }mx; #Containts escaped metachars
58}
59
601;
61
62__END__
63
64=pod
65
66=head1 NAME
67
68Perl::Critic::Policy::ValuesAndExpressions::ProhibitInterpolationOfLiterals
69
70=head1 DESCRIPTION
71
72Don't use double-quotes or C<qq//> if your string doesn't require
73interpolation. This saves the interpreter a bit of work and it lets
74the reader know that you really did intend the string to be literal.
75
76 print "foobar"; #not ok
77 print 'foobar'; #ok
78 print qq/foobar/; #not ok
79 print q/foobar/; #ok
80
81 print "$foobar"; #ok
82 print "foobar\n"; #ok
83 print qq/$foobar/; #ok
84 print qq/foobar\n/; #ok
85
86 print qq{$foobar}; #preferred
87 print qq{foobar\n}; #preferred
88
89=head1 CONSTRUCTOR
90
91This Policy accepts an additional key-value pair in the constructor,
92The key is 'allow' and the value is a string of quote styles
93that are exempt from this policy. Valid styles are C<qq{}>, C<qq()>,
94C<qq[]>, and C<qq//>. Multiple styles should be separated by
95whitespace. This is useful because some folks have configured their
96editor to apply special syntax highlighting within certain styles of
97quotes. For example, you can tweak C<vim> to use SQL highlighting for
98everything that appears within C<qq{}> or C<qq[]> quotes. But if
99those strings are literal, Perl::Critic will complain. To prevent
100this, put the following in your F<.perlcriticrc> file:
101
102 [ValuesAndExpressions::ProhibitInterpolationOfLiterals]
103 allow = qq{} qq[]
104
105=head1 SEE ALSO
106
107L<Perl::Critic::Policy::ValuesAndExpressions::RequireInterpolationOfMetachars>
108
109=head1 AUTHOR
110
111Jeffrey Ryan Thalhammer <thaljef@cpan.org>
112
113=head1 COPYRIGHT
114
115Copyright (c) 2005 Jeffrey Ryan Thalhammer. All rights reserved.
116
117This program is free software; you can redistribute it and/or modify
118it under the same terms as Perl itself. The full text of this license
119can be found in the LICENSE file included with this module.
120
121=cut