« blib/lib/Perl/Critic/Policy/InputOutput/ProhibitJoinedReadline.pm
↑ Summary
blib/lib/Perl/Critic/Policy/Variables/ProhibitMatchVars.pm »
| 1 | package Perl::Critic::Policy::ValuesAndExpressions::ProhibitInterpolationOfLiterals; | ||||||||||
| 2 | |||||||||||
| 3 | 40 | ▶ | use 5.010001; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 40 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 40 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 40 | ▶ | use Readonly; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 40 | ▶ | use List::SomeUtils qw(any); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 40 | ▶ | use Perl::Critic::Utils qw{ :characters :severities :data_conversion }; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 40 | ▶ | use parent 'Perl::Critic::Policy'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 40 | our $VERSION = '1.150'; | |||||||||
| 14 | |||||||||||
| 15 | #----------------------------------------------------------------------------- | ||||||||||
| 16 | |||||||||||
| 17 | 40 | Readonly::Scalar my $DESC => q{Useless interpolation of literal string}; | |||||||||
| 18 | 40 | Readonly::Scalar my $EXPL => [51]; | |||||||||
| 19 | |||||||||||
| 20 | #----------------------------------------------------------------------------- | ||||||||||
| 21 | |||||||||||
| 22 | sub supported_parameters { | ||||||||||
| 23 | return ( | ||||||||||
| 24 | { | ||||||||||
| 25 | 92 | ▶ | name => 'allow', | ||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 26 | description => | ||||||||||
| 27 | 'Kinds of delimiters to permit, e.g. "qq{", "qq(", "qq[", "qq/".', | ||||||||||
| 28 | default_string => $EMPTY, | ||||||||||
| 29 | parser => \&_parse_allow, | ||||||||||
| 30 | }, | ||||||||||
| 31 | { | ||||||||||
| 32 | name => 'allow_if_string_contains_single_quote', | ||||||||||
| 33 | description => | ||||||||||
| 34 | q<If the string contains ' characters, allow "" to quote it.>, | ||||||||||
| 35 | default_string => '0', | ||||||||||
| 36 | behavior => 'boolean', | ||||||||||
| 37 | }, | ||||||||||
| 38 | ); | ||||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | 76 | ▶ | sub default_severity { return $SEVERITY_LOWEST } | ||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 42 | 84 | ▶ | sub default_themes { return qw( core pbp cosmetic ) } | ||||||||
|
Subroutine
default_themes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 43 | 31 | ▶ | sub applies_to { return qw(PPI::Token::Quote::Double | ||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 44 | PPI::Token::Quote::Interpolate) } | ||||||||||
| 45 | |||||||||||
| 46 | #----------------------------------------------------------------------------- | ||||||||||
| 47 | |||||||||||
| 48 | 40 | Readonly::Scalar my $MAX_SPECIFICATION_LENGTH => 3; | |||||||||
| 49 | |||||||||||
| 50 | sub _parse_allow { | ||||||||||
| 51 | 90 | ▶ | my ($self, $parameter, $config_string) = @_; | ||||||||
|
Subroutine
_parse_allow: called CC=6 SCAR=20.7
| |||||||||||
| 52 | |||||||||||
| 53 | 90 | my @allow; | |||||||||
| 54 | |||||||||||
| 55 | 90 | ▶ | if (defined $config_string) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | 2 | @allow = words_from_string( $config_string ); | |||||||||
| 57 | #Try to be forgiving with the configuration... | ||||||||||
| 58 | 2 | for (@allow) { | |||||||||
| 59 | 0 | ▶ | m{ \A qq }xms || ($_ = 'qq' . $_) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | } #Add 'qq' | ||||||||||
| 61 | 2 | for (@allow) { | |||||||||
| 62 | 0 | ▶ | (length $_ <= $MAX_SPECIFICATION_LENGTH) || chop | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | } #Chop closing char | ||||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | 90 | $self->{_allow} = \@allow; | |||||||||
| 67 | |||||||||||
| 68 | 90 | return; | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | #----------------------------------------------------------------------------- | ||||||||||
| 72 | |||||||||||
| 73 | sub violates { | ||||||||||
| 74 | 1 | ▶ | my ( $self, $elem, undef ) = @_; | ||||||||
|
Subroutine
violates: called CC=5 SCAR=19.6
Pod
documented
| |||||||||||
| 75 | |||||||||||
| 76 | # Skip if this string needs interpolation | ||||||||||
| 77 | 1 | ▶ | return if _has_interpolation($elem); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | |||||||||||
| 79 | # Overlook allowed quote styles | ||||||||||
| 80 | 1 | ▶ | return if any { $elem =~ m{ \A \Q$_\E }xms } @{ $self->{_allow} }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.6
Branch: 1 branch
| |||||||||||
| 81 | |||||||||||
| 82 | # If the flag is set, allow "I'm here". | ||||||||||
| 83 | 1 | ▶ | if ( $self->{_allow_if_string_contains_single_quote} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | 0 | ▶ | return if index ($elem, $QUOTE) >= 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | # Must be a violation | ||||||||||
| 88 | 1 | return $self->violation( $DESC, $EXPL, $elem ); | |||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | #----------------------------------------------------------------------------- | ||||||||||
| 92 | |||||||||||
| 93 | sub _has_interpolation { | ||||||||||
| 94 | 1 | ▶ | my $elem = shift; | ||||||||
|
Subroutine
_has_interpolation: called CC=1 SCAR=0.0
| |||||||||||
| 95 | 1 | return $elem =~ m< | |||||||||
| 96 | (?: \A | [^\\] ) | ||||||||||
| 97 | (?: \\{2} )* | ||||||||||
| 98 | (?: [\$\@] \S+ | \\[tnrfbae0xcNLuLUEQ] ) | ||||||||||
| 99 | >xmso; | ||||||||||
| 100 | } | ||||||||||
| 101 | |||||||||||
| 102 | 40 | 1; | |||||||||
| 103 | |||||||||||
| 104 | __END__ | ||||||||||
| 105 | |||||||||||
| 106 | #----------------------------------------------------------------------------- | ||||||||||
| 107 | |||||||||||
| 108 | =pod | ||||||||||
| 109 | |||||||||||
| 110 | =head1 NAME | ||||||||||
| 111 | |||||||||||
| 112 | Perl::Critic::Policy::ValuesAndExpressions::ProhibitInterpolationOfLiterals - Always use single quotes for literal strings. | ||||||||||
| 113 | |||||||||||
| 114 | |||||||||||
| 115 | =head1 AFFILIATION | ||||||||||
| 116 | |||||||||||
| 117 | This Policy is part of the core L<Perl::Critic|Perl::Critic> | ||||||||||
| 118 | distribution. | ||||||||||
| 119 | |||||||||||
| 120 | |||||||||||
| 121 | =head1 DESCRIPTION | ||||||||||
| 122 | |||||||||||
| 123 | Don't use double-quotes or C<qq//> if your string doesn't require | ||||||||||
| 124 | interpolation. This saves the interpreter a bit of work and it lets | ||||||||||
| 125 | the reader know that you really did intend the string to be literal. | ||||||||||
| 126 | |||||||||||
| 127 | print "foobar"; #not ok | ||||||||||
| 128 | print 'foobar'; #ok | ||||||||||
| 129 | print qq/foobar/; #not ok | ||||||||||
| 130 | print q/foobar/; #ok | ||||||||||
| 131 | |||||||||||
| 132 | print "$foobar"; #ok | ||||||||||
| 133 | print "foobar\n"; #ok | ||||||||||
| 134 | print qq/$foobar/; #ok | ||||||||||
| 135 | print qq/foobar\n/; #ok | ||||||||||
| 136 | |||||||||||
| 137 | print qq{$foobar}; #preferred | ||||||||||
| 138 | print qq{foobar\n}; #preferred | ||||||||||
| 139 | |||||||||||
| 140 | Use of double-quotes might be reasonable if the string contains single | ||||||||||
| 141 | quote (') characters: | ||||||||||
| 142 | |||||||||||
| 143 | print "it's me"; # ok, if configuration flag set | ||||||||||
| 144 | |||||||||||
| 145 | |||||||||||
| 146 | =head1 CONFIGURATION | ||||||||||
| 147 | |||||||||||
| 148 | The types of quoting styles to exempt from this policy can be | ||||||||||
| 149 | configured via the C<allow> option. This must be a | ||||||||||
| 150 | whitespace-delimited combination of some or all of the following | ||||||||||
| 151 | styles: C<qq{}>, C<qq()>, C<qq[]>, and C<qq//>. | ||||||||||
| 152 | |||||||||||
| 153 | This is useful because some folks have configured their editor to | ||||||||||
| 154 | apply special syntax highlighting within certain styles of quotes. | ||||||||||
| 155 | For example, you can tweak C<vim> to use SQL highlighting for | ||||||||||
| 156 | everything that appears within C<qq{}> or C<qq[]> quotes. But if | ||||||||||
| 157 | those strings are literal, Perl::Critic will complain. To prevent | ||||||||||
| 158 | this, put the following in your F<.perlcriticrc> file: | ||||||||||
| 159 | |||||||||||
| 160 | [ValuesAndExpressions::ProhibitInterpolationOfLiterals] | ||||||||||
| 161 | allow = qq{} qq[] | ||||||||||
| 162 | |||||||||||
| 163 | The flag C<allow_if_string_contains_single_quote> permits | ||||||||||
| 164 | double-quoted strings if the string contains a single quote (') | ||||||||||
| 165 | character. It defaults to off; to turn it on put the following in | ||||||||||
| 166 | your F<.perlcriticrc> file: | ||||||||||
| 167 | |||||||||||
| 168 | [ValuesAndExpressions::ProhibitInterpolationOfLiterals] | ||||||||||
| 169 | allow_if_string_contains_single_quote = 1 | ||||||||||
| 170 | |||||||||||
| 171 | |||||||||||
| 172 | =head1 SEE ALSO | ||||||||||
| 173 | |||||||||||
| 174 | L<Perl::Critic::Policy::ValuesAndExpressions::RequireInterpolationOfMetachars|Perl::Critic::Policy::ValuesAndExpressions::RequireInterpolationOfMetachars> | ||||||||||
| 175 | |||||||||||
| 176 | =head1 AUTHOR | ||||||||||
| 177 | |||||||||||
| 178 | Jeffrey Ryan Thalhammer <jeff@imaginative-software.com> | ||||||||||
| 179 | |||||||||||
| 180 | =head1 COPYRIGHT | ||||||||||
| 181 | |||||||||||
| 182 | Copyright (c) 2005-2021 Imaginative Software Systems. All rights reserved. | ||||||||||
| 183 | |||||||||||
| 184 | This program is free software; you can redistribute it and/or modify | ||||||||||
| 185 | it under the same terms as Perl itself. The full text of this license | ||||||||||
| 186 | can be found in the LICENSE file included with this module. | ||||||||||
| 187 | |||||||||||
| 188 | =cut | ||||||||||
| 189 | |||||||||||
| 190 | # Local Variables: | ||||||||||
| 191 | # mode: cperl | ||||||||||
| 192 | # cperl-indent-level: 4 | ||||||||||
| 193 | # fill-column: 78 | ||||||||||
| 194 | # indent-tabs-mode: nil | ||||||||||
| 195 | # c-indentation-style: bsd | ||||||||||
| 196 | # End: | ||||||||||
| 197 | # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround : | ||||||||||