1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/ValuesAndExpressions/ProhibitInterpolationOfLiterals.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
8 package Perl::Critic::Policy::ValuesAndExpressions::ProhibitInterpolationOfLiterals;
15 use List::MoreUtils qw(any);
17 use Perl::Critic::Utils qw{ :characters :severities :data_conversion };
18 use base 'Perl::Critic::Policy';
20 our $VERSION = '1.088';
22 #-----------------------------------------------------------------------------
24 Readonly::Scalar my $DESC => q{Useless interpolation of literal string};
25 Readonly::Scalar my $EXPL => [51];
27 #-----------------------------------------------------------------------------
29 sub supported_parameters {
34 'Kinds of delimiters to permit, e.g. "qq{", "qq(", "qq[", "qq/".',
35 default_string => $EMPTY,
36 parser => \&_parse_allow,
41 sub default_severity { return $SEVERITY_LOWEST }
42 sub default_themes { return qw( core pbp cosmetic ) }
43 sub applies_to { return qw(PPI::Token::Quote::Double
44 PPI::Token::Quote::Interpolate) }
46 #-----------------------------------------------------------------------------
48 Readonly::Scalar my $MAX_SPECIFICATION_LENGTH => 3;
51 my ($self, $parameter, $config_string) = @_;
55 if (defined $config_string) {
56 @allow = words_from_string( $config_string );
57 #Try to be forgiving with the configuration...
59 m{ \A qq }mx || ($_ = 'qq' . $_)
62 (length $_ <= $MAX_SPECIFICATION_LENGTH) || chop
66 $self->{_allow} = \@allow;
71 #-----------------------------------------------------------------------------
74 my ( $self, $elem, undef ) = @_;
76 # Skip if this string needs interpolation
77 return if _has_interpolation($elem);
79 # Overlook allowed quote styles
80 return if any { $elem =~ m{ \A \Q$_\E }mx } @{ $self->{_allow} };
83 return $self->violation( $DESC, $EXPL, $elem );
86 #-----------------------------------------------------------------------------
88 sub _has_interpolation {
90 return $elem =~ m{ (?<!\\) [\$\@] \S+ }mx #Contains unescaped $. or @.
91 || $elem =~ m{ \\[tnrfbae0xcNLuLUEQ] }mx; #Containts escaped metachars
98 #-----------------------------------------------------------------------------
104 Perl::Critic::Policy::ValuesAndExpressions::ProhibitInterpolationOfLiterals - Always use single quotes for literal strings.
108 This Policy is part of the core L<Perl::Critic> distribution.
113 Don't use double-quotes or C<qq//> if your string doesn't require
114 interpolation. This saves the interpreter a bit of work and it lets
115 the reader know that you really did intend the string to be literal.
117 print "foobar"; #not ok
119 print qq/foobar/; #not ok
123 print "foobar\n"; #ok
124 print qq/$foobar/; #ok
125 print qq/foobar\n/; #ok
127 print qq{$foobar}; #preferred
128 print qq{foobar\n}; #preferred
132 The types of quoting styles to exempt from this policy can be
133 configured via the C<allow> option. This must be a
134 whitespace-delimited combination of some or all of the following
135 styles: C<qq{}>, C<qq()>, C<qq[]>, and C<qq//>.
137 This is useful because some folks have configured their editor to
138 apply special syntax highlighting within certain styles of quotes.
139 For example, you can tweak C<vim> to use SQL highlighting for
140 everything that appears within C<qq{}> or C<qq[]> quotes. But if
141 those strings are literal, Perl::Critic will complain. To prevent
142 this, put the following in your F<.perlcriticrc> file:
144 [ValuesAndExpressions::ProhibitInterpolationOfLiterals]
149 L<Perl::Critic::Policy::ValuesAndExpressions::RequireInterpolationOfMetachars>
153 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
157 Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer. All rights reserved.
159 This program is free software; you can redistribute it and/or modify
160 it under the same terms as Perl itself. The full text of this license
161 can be found in the LICENSE file included with this module.
167 # cperl-indent-level: 4
169 # indent-tabs-mode: nil
170 # c-indentation-style: bsd
172 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :