Login
Added hack to prevent RequireUseStrict and RequireUseWarnings from
[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
a7340650 15our $VERSION = 0.22;
59b05e08 16
86bf4aa6
JRT
17#---------------------------------------------------------------------------
18
59b05e08
JRT
19my $desc = q{Useless interpolation of literal string};
20my $expl = [51];
21
22#---------------------------------------------------------------------------
23
9f1d5408 24sub default_severity { return $SEVERITY_LOWEST }
faa35de4
JRT
25sub default_themes { return qw(pbp cosmetic) }
26sub applies_to { return qw(PPI::Token::Quote::Double
27 PPI::Token::Quote::Interpolate) }
86bf4aa6
JRT
28
29#---------------------------------------------------------------------------
30
59b05e08
JRT
31sub new {
32 my ( $class, %args ) = @_;
33 my $self = bless {}, $class;
34 $self->{_allow} = [];
35
36 #Set configuration, if defined
37 if ( defined $args{allow} ) {
0a6f07d0
AL
38 my @allow = split m{ \s+ }mx, $args{allow};
39 #Try to be forgiving with the configuration...
40 for (@allow) {
41 m{ \A qq }mx || ($_ = 'qq' . $_)
42 } #Add 'qq'
43 for (@allow) {
44 (length $_ <= 3) || chop
45 } #Chop closing char
46 $self->{_allow} = \@allow;
59b05e08
JRT
47 }
48
49 return $self;
50}
51
52#---------------------------------------------------------------------------
53
54sub violates {
e5f6c18d 55 my ( $self, $elem, undef ) = @_;
59b05e08
JRT
56
57 #Overlook allowed quote styles
58 for my $allowed ( @{ $self->{_allow} } ) {
59 return if $elem =~ m{ \A \Q$allowed\E }mx;
60 }
61
45acb16b
CD
62 return if _has_interpolation($elem);
63
64 return $self->violation( $desc, $expl, $elem );
59b05e08
JRT
65}
66
67sub _has_interpolation {
45acb16b 68 my $elem = shift;
59b05e08 69 return $elem =~ m{ (?<!\\) [\$\@] \S+ }mx #Contains unescaped $. or @.
45acb16b 70 || $elem =~ m{ \\[tnrfae0xcNLuLUEQ] }mx; #Containts escaped metachars
59b05e08
JRT
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
8a25c8a0
ES
104=head1 CONFIGURATION
105
106The types of quoting styles to exempt from this policy can be
107configured via the C<allow> option. This must be a
108whitespace-delimited combination of some or all of the following
109styles: C<qq{}>, C<qq()>, C<qq[]>, and C<qq//>.
110
111This is useful because some folks have configured their editor to
112apply special syntax highlighting within certain styles of quotes.
113For example, you can tweak C<vim> to use SQL highlighting for
59b05e08
JRT
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
737d3b65
CD
138
139# Local Variables:
140# mode: cperl
141# cperl-indent-level: 4
142# fill-column: 78
143# indent-tabs-mode: nil
144# c-indentation-style: bsd
145# End:
345c7562 146# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab :