line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# Copyright 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 Kevin Ryde |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# Perl-Critic-Pulp is free software; you can redistribute it and/or modify |
4
|
|
|
|
|
|
|
# it under the terms of the GNU General Public License as published by the |
5
|
|
|
|
|
|
|
# Free Software Foundation; either version 3, or (at your option) any later |
6
|
|
|
|
|
|
|
# version. |
7
|
|
|
|
|
|
|
# |
8
|
|
|
|
|
|
|
# Perl-Critic-Pulp is distributed in the hope that it will be useful, but |
9
|
|
|
|
|
|
|
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
10
|
|
|
|
|
|
|
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
11
|
|
|
|
|
|
|
# for more details. |
12
|
|
|
|
|
|
|
# |
13
|
|
|
|
|
|
|
# You should have received a copy of the GNU General Public License along |
14
|
|
|
|
|
|
|
# with Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>. |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
package Perl::Critic::Policy::ValuesAndExpressions::ProhibitUnknownBackslash; |
18
|
40
|
|
|
40
|
|
29860
|
use 5.006; |
|
40
|
|
|
|
|
130
|
|
19
|
40
|
|
|
40
|
|
189
|
use strict; |
|
40
|
|
|
|
|
71
|
|
|
40
|
|
|
|
|
685
|
|
20
|
40
|
|
|
40
|
|
1507
|
use version (); # but don't import qv() |
|
40
|
|
|
|
|
1570
|
|
|
40
|
|
|
|
|
523
|
|
21
|
40
|
|
|
40
|
|
179
|
use warnings; |
|
40
|
|
|
|
|
85
|
|
|
40
|
|
|
|
|
1210
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# 1.084 for Perl::Critic::Document highest_explicit_perl_version() |
24
|
40
|
|
|
40
|
|
600
|
use Perl::Critic::Policy 1.084; |
|
40
|
|
|
|
|
141229
|
|
|
40
|
|
|
|
|
1917
|
|
25
|
40
|
|
|
40
|
|
194
|
use base 'Perl::Critic::Policy'; |
|
40
|
|
|
|
|
96
|
|
|
40
|
|
|
|
|
3822
|
|
26
|
40
|
|
|
40
|
|
241
|
use Perl::Critic::Utils; |
|
40
|
|
|
|
|
105
|
|
|
40
|
|
|
|
|
646
|
|
27
|
|
|
|
|
|
|
|
28
|
40
|
|
|
40
|
|
30445
|
use Perl::Critic::Pulp; |
|
40
|
|
|
|
|
1874
|
|
|
40
|
|
|
|
|
2641
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# uncomment this to run the ### lines |
31
|
|
|
|
|
|
|
# use Smart::Comments; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
our $VERSION = 97; |
34
|
|
|
|
|
|
|
|
35
|
40
|
|
|
|
|
4528
|
use constant supported_parameters => |
36
|
|
|
|
|
|
|
({ name => 'single', |
37
|
|
|
|
|
|
|
description => 'Checking of single-quote strings.', |
38
|
|
|
|
|
|
|
behavior => 'string', |
39
|
|
|
|
|
|
|
default_string => 'none', |
40
|
|
|
|
|
|
|
}, |
41
|
|
|
|
|
|
|
{ name => 'double', |
42
|
|
|
|
|
|
|
description => 'Checking of double-quote strings.', |
43
|
|
|
|
|
|
|
behavior => 'string', |
44
|
|
|
|
|
|
|
default_string => 'all', |
45
|
|
|
|
|
|
|
}, |
46
|
|
|
|
|
|
|
{ name => 'heredoc', |
47
|
|
|
|
|
|
|
description => 'Checking of interpolated here-documents.', |
48
|
|
|
|
|
|
|
behavior => 'string', |
49
|
|
|
|
|
|
|
default_string => 'all', |
50
|
40
|
|
|
40
|
|
226
|
}); |
|
40
|
|
|
|
|
1825
|
|
51
|
40
|
|
|
40
|
|
265
|
use constant default_severity => $Perl::Critic::Utils::SEVERITY_MEDIUM; |
|
40
|
|
|
|
|
76
|
|
|
40
|
|
|
|
|
2046
|
|
52
|
40
|
|
|
40
|
|
305
|
use constant default_themes => qw(pulp cosmetic); |
|
40
|
|
|
|
|
945
|
|
|
40
|
|
|
|
|
8084
|
|
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub applies_to { |
55
|
274
|
|
|
274
|
1
|
1115453
|
my ($policy) = @_; |
56
|
|
|
|
|
|
|
return (($policy->{'_single'} ne 'none' |
57
|
|
|
|
|
|
|
? ('PPI::Token::Quote::Single', # '' |
58
|
|
|
|
|
|
|
'PPI::Token::Quote::Literal') # q{} |
59
|
|
|
|
|
|
|
: ()), |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
($policy->{'_single'} ne 'none' |
62
|
|
|
|
|
|
|
|| $policy->{'_double'} ne 'none' |
63
|
|
|
|
|
|
|
? ('PPI::Token::QuoteLike::Command') # qx{} or qx'' |
64
|
|
|
|
|
|
|
: ()), |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
($policy->{'_double'} ne 'none' |
67
|
|
|
|
|
|
|
? ('PPI::Token::Quote::Double', # "" |
68
|
|
|
|
|
|
|
'PPI::Token::Quote::Interpolate', # qq{} |
69
|
|
|
|
|
|
|
'PPI::Token::QuoteLike::Backtick') # `` |
70
|
|
|
|
|
|
|
: ()), |
71
|
|
|
|
|
|
|
|
72
|
274
|
100
|
66
|
|
|
2137
|
($policy->{'_heredoc'} ne 'none' |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
73
|
|
|
|
|
|
|
? ('PPI::Token::HereDoc') |
74
|
|
|
|
|
|
|
: ())); |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# for violation messages |
78
|
|
|
|
|
|
|
my %charname = ("\n" => '{newline}', |
79
|
|
|
|
|
|
|
"\r" => '{cr}', |
80
|
|
|
|
|
|
|
"\t" => '{tab}', |
81
|
|
|
|
|
|
|
" " => '{space}'); |
82
|
|
|
|
|
|
|
|
83
|
40
|
|
|
|
|
4025
|
use constant _KNOWN => ( |
84
|
|
|
|
|
|
|
't' # \t tab |
85
|
|
|
|
|
|
|
. 'n' # \n newline |
86
|
|
|
|
|
|
|
. 'r' # \r carriage return |
87
|
|
|
|
|
|
|
. 'f' # \f form feed |
88
|
|
|
|
|
|
|
. 'b' # \b backspace |
89
|
|
|
|
|
|
|
. 'a' # \a bell |
90
|
|
|
|
|
|
|
. 'e' # \e esc |
91
|
|
|
|
|
|
|
. '0123' # \377 octal |
92
|
|
|
|
|
|
|
. 'x' # \xFF \x{FF} hex |
93
|
|
|
|
|
|
|
. 'c' # \cX control char |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
. 'l' # \l lowercase one char |
96
|
|
|
|
|
|
|
. 'u' # \u uppercase one char |
97
|
|
|
|
|
|
|
. 'L' # \L lowercase string |
98
|
|
|
|
|
|
|
. 'U' # \U uppercase string |
99
|
|
|
|
|
|
|
. 'E' # \E end case or quote |
100
|
|
|
|
|
|
|
. 'Q' # \Q quotemeta |
101
|
|
|
|
|
|
|
. '$' # non-interpolation |
102
|
|
|
|
|
|
|
. '@' # non-interpolation |
103
|
40
|
|
|
40
|
|
1075
|
); |
|
40
|
|
|
|
|
77
|
|
104
|
|
|
|
|
|
|
|
105
|
40
|
|
|
|
|
51897
|
use constant _CONTROL_KNOWN => |
106
|
40
|
|
|
40
|
|
209
|
'?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_abcdefghijklmnopqrstuvwxyz'; ## no critic (RequireInterpolationOfMetachars) |
|
40
|
|
|
|
|
73
|
|
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
my $quotelike_re = qr/^(?:(q[qrwx]?) # $1 "q" if present |
109
|
|
|
|
|
|
|
(?:(?:\s(?:\s*\#[^\n]*\n)*)\s*)? # possible comments |
110
|
|
|
|
|
|
|
)? # possible "q" |
111
|
|
|
|
|
|
|
(.) # $2 opening quote |
112
|
|
|
|
|
|
|
(.*) # $3 guts |
113
|
|
|
|
|
|
|
(.)$ # $4 closing quote |
114
|
|
|
|
|
|
|
/xs; |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
# extra explanation for double-quote interpolations |
117
|
|
|
|
|
|
|
my %explain = ('%' => ' (hashes are not interpolated)', |
118
|
|
|
|
|
|
|
'&' => ' (function calls are not interpolated)', |
119
|
|
|
|
|
|
|
'4' => ' (until Perl 5.6 octal wide chars)', |
120
|
|
|
|
|
|
|
'5' => ' (until Perl 5.6 octal wide chars)', |
121
|
|
|
|
|
|
|
'6' => ' (until Perl 5.6 octal wide chars)', |
122
|
|
|
|
|
|
|
'7' => ' (until Perl 5.6 octal wide chars)', |
123
|
|
|
|
|
|
|
'N' => ' (without "use charnames" in scope)', |
124
|
|
|
|
|
|
|
); |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
my $v5016 = version->new('5.016'); |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
sub violates { |
129
|
262
|
|
|
262
|
1
|
8213
|
my ($self, $elem, $document) = @_; |
130
|
|
|
|
|
|
|
|
131
|
262
|
|
|
|
|
407
|
my $have_perl_516; |
132
|
262
|
100
|
|
|
|
570
|
if (defined (my $doc_version = $document->highest_explicit_perl_version)) { |
133
|
32
|
|
|
|
|
5714
|
$have_perl_516 = ($doc_version >= $v5016); |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
262
|
|
|
|
|
17966
|
my $content = $elem->content; |
137
|
262
|
|
|
|
|
1022
|
my $close = substr ($content, -1, 1); |
138
|
262
|
|
|
|
|
412
|
my $single = 0; |
139
|
262
|
|
|
|
|
404
|
my ($param, $str); |
140
|
|
|
|
|
|
|
|
141
|
262
|
100
|
|
|
|
868
|
if ($elem->isa('PPI::Token::HereDoc')) { |
142
|
6
|
100
|
|
|
|
17
|
return if ($close eq "'"); # uninterpolated |
143
|
4
|
|
|
|
|
9
|
$param = $self->{_heredoc}; |
144
|
4
|
|
|
|
|
12
|
$str = join ('', $elem->heredoc); |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
} else { |
147
|
256
|
100
|
|
|
|
805
|
if ($elem->can('string')) { |
148
|
242
|
|
|
|
|
579
|
$str = $elem->string; |
149
|
|
|
|
|
|
|
} else { |
150
|
14
|
50
|
|
|
|
62
|
$elem =~ $quotelike_re or die "Oops, didn't match quotelike_re"; |
151
|
14
|
|
|
|
|
122
|
$str = $3; |
152
|
|
|
|
|
|
|
} |
153
|
256
|
|
|
|
|
3366
|
$str =~ s{((^|\G|[^\\])(\\\\)*)\\\Q$close}{$close}sg; |
154
|
|
|
|
|
|
|
|
155
|
256
|
100
|
100
|
|
|
2220
|
if ($elem->isa('PPI::Token::Quote::Single') |
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
156
|
|
|
|
|
|
|
|| $elem->isa('PPI::Token::Quote::Literal') |
157
|
|
|
|
|
|
|
|| ($elem->isa('PPI::Token::QuoteLike::Command') |
158
|
|
|
|
|
|
|
&& $close eq "'")) { |
159
|
42
|
|
|
|
|
73
|
$single = 1; |
160
|
42
|
|
|
|
|
89
|
$param = $self->{_single}; |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
} else { |
163
|
214
|
|
|
|
|
481
|
$param = $self->{_double}; |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
} |
166
|
260
|
100
|
|
|
|
541
|
return if ($param eq 'none'); |
167
|
|
|
|
|
|
|
|
168
|
256
|
|
|
|
|
397
|
my $known = $close; |
169
|
|
|
|
|
|
|
|
170
|
256
|
100
|
|
|
|
486
|
if (! $single) { |
171
|
218
|
|
|
|
|
364
|
$known .= _KNOWN; |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
# Octal chars above \377 are in 5.6 up. |
174
|
|
|
|
|
|
|
# Consider known if no "use 5.x" at all, or if present and 5.6 up, |
175
|
|
|
|
|
|
|
# so only under explicit "use 5.005" or lower are they not allowed. |
176
|
218
|
|
|
|
|
495
|
my $perlver = $document->highest_explicit_perl_version; |
177
|
218
|
100
|
100
|
|
|
2102
|
if (! defined $perlver || $perlver >= 5.006) { |
178
|
194
|
|
|
|
|
304
|
$known .= '4567'; |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
### elem: ref $elem |
183
|
|
|
|
|
|
|
### $content |
184
|
|
|
|
|
|
|
### $str |
185
|
|
|
|
|
|
|
### close char: $close |
186
|
|
|
|
|
|
|
### $known |
187
|
|
|
|
|
|
|
### perlver: $document->highest_explicit_perl_version |
188
|
|
|
|
|
|
|
|
189
|
256
|
|
|
|
|
353
|
my $have_charnames; |
190
|
256
|
|
|
|
|
379
|
my $interpolate_var_end = -1; |
191
|
256
|
|
|
|
|
434
|
my $interpolate_var_colon; |
192
|
|
|
|
|
|
|
my @violations; |
193
|
|
|
|
|
|
|
|
194
|
256
|
|
|
|
|
1159
|
while ($str =~ /(\$. # $ not at end-of-string |
195
|
|
|
|
|
|
|
|\@[[:alnum:]:'\{\$+-]) # @ forms per toke.c S_scan_const() |
196
|
|
|
|
|
|
|
|(\\+) # $2 run of backslashes |
197
|
|
|
|
|
|
|
/sgx) { |
198
|
358
|
100
|
|
|
|
2397
|
if (defined $1) { |
199
|
|
|
|
|
|
|
# $ or @ |
200
|
54
|
50
|
|
|
|
112
|
unless ($single) { # no variables in single-quote |
201
|
|
|
|
|
|
|
### interpolation at: pos($str) |
202
|
54
|
|
50
|
|
|
148
|
my $new_pos = _pos_after_interpolate_variable |
203
|
|
|
|
|
|
|
($str, pos($str) - length($1)) |
204
|
|
|
|
|
|
|
|| last; |
205
|
54
|
|
|
|
|
3962
|
pos($str) = $new_pos; |
206
|
|
|
|
|
|
|
### ends at: pos($str) |
207
|
54
|
50
|
|
|
|
290
|
if (substr($str,pos($str)-1,1) =~ /(\w)|[]}]/) { |
208
|
54
|
|
|
|
|
122
|
$interpolate_var_colon = $1; |
209
|
54
|
|
|
|
|
91
|
$interpolate_var_end = pos($str); |
210
|
|
|
|
|
|
|
### interpolate_var_end set to: $interpolate_var_end |
211
|
|
|
|
|
|
|
} |
212
|
|
|
|
|
|
|
} |
213
|
54
|
|
|
|
|
200
|
next; |
214
|
|
|
|
|
|
|
} |
215
|
|
|
|
|
|
|
|
216
|
304
|
100
|
|
|
|
755
|
if ((length($2) & 1) == 0) { |
217
|
|
|
|
|
|
|
# even number of backslashes, not an escape |
218
|
14
|
|
|
|
|
44
|
next; |
219
|
|
|
|
|
|
|
} |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
# shouldn't have \ as the last char in $str, but if that happends then |
222
|
|
|
|
|
|
|
# $c is empty string '' |
223
|
|
|
|
|
|
|
|
224
|
290
|
|
|
|
|
558
|
my $c = substr($str,pos($str),1); |
225
|
290
|
|
|
|
|
621
|
pos($str)++; |
226
|
|
|
|
|
|
|
|
227
|
290
|
100
|
|
|
|
616
|
if (! $single) { |
228
|
258
|
100
|
100
|
|
|
1027
|
if ($c eq 'N') { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
229
|
12
|
50
|
|
|
|
27
|
if (! defined $have_charnames) { |
230
|
12
|
|
|
|
|
27
|
$have_charnames = _charnames_in_scope($elem); |
231
|
|
|
|
|
|
|
} |
232
|
12
|
100
|
100
|
|
|
105
|
if ($have_charnames || $have_perl_516) { |
233
|
8
|
|
|
|
|
47
|
next; # ok if "use charnames" or perl 5.16 up (which autoloads that) |
234
|
|
|
|
|
|
|
} |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
} elsif ($c eq 'c') { |
237
|
|
|
|
|
|
|
# \cX control char. |
238
|
|
|
|
|
|
|
# If \c is at end-of-string then new $c is '' and pos() will goes past |
239
|
|
|
|
|
|
|
# length($str). That pos() is ok, the loop regexp gives no-match and |
240
|
|
|
|
|
|
|
# terminates. |
241
|
32
|
|
|
|
|
88
|
$c = substr ($str, pos($str)++, 1); |
242
|
32
|
100
|
|
|
|
70
|
if ($c eq '') { |
243
|
4
|
|
|
|
|
14
|
push @violations, |
244
|
|
|
|
|
|
|
$self->violation ('Control char \\c at end of string', '', $elem); |
245
|
4
|
|
|
|
|
616
|
next; |
246
|
|
|
|
|
|
|
} |
247
|
28
|
100
|
|
|
|
70
|
if (index (_CONTROL_KNOWN, $c) >= 0) { |
248
|
22
|
|
|
|
|
68
|
next; # a known escape |
249
|
|
|
|
|
|
|
} |
250
|
6
|
|
|
|
|
16
|
push @violations, |
251
|
|
|
|
|
|
|
$self->violation ('Unknown control char \\c' . _printable($c), |
252
|
|
|
|
|
|
|
'', $elem); |
253
|
6
|
|
|
|
|
832
|
next; |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
} elsif ($c eq ':') { |
256
|
34
|
100
|
|
|
|
70
|
if ($interpolate_var_colon) { |
257
|
|
|
|
|
|
|
### backslash colon, pos: pos($str) |
258
|
|
|
|
|
|
|
### $interpolate_var_end |
259
|
|
|
|
|
|
|
### substr: substr ($str, $interpolate_var_end, 2) |
260
|
20
|
50
|
33
|
|
|
74
|
if (pos($str) == $interpolate_var_end+2 |
|
|
|
66
|
|
|
|
|
261
|
|
|
|
|
|
|
|| (pos($str) == $interpolate_var_end+4 |
262
|
|
|
|
|
|
|
&& substr ($str, $interpolate_var_end, 2) eq '\\:')) { |
263
|
20
|
|
|
|
|
60
|
next; |
264
|
|
|
|
|
|
|
} |
265
|
|
|
|
|
|
|
} |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
} elsif ($c eq '[' || $c eq '{') { |
268
|
|
|
|
|
|
|
### backslash bracket, pos: pos($str) |
269
|
|
|
|
|
|
|
### $interpolate_var_end |
270
|
26
|
100
|
|
|
|
65
|
if (pos($str) == $interpolate_var_end+2) { |
271
|
18
|
|
|
|
|
77
|
next; |
272
|
|
|
|
|
|
|
} |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
} elsif ($c eq '-') { |
275
|
|
|
|
|
|
|
### backslash dash: pos($str) |
276
|
10
|
100
|
|
|
|
32
|
if ($str =~ /\G>[[{]/) { |
277
|
|
|
|
|
|
|
### is for bracket or brace, pos now: pos($str) |
278
|
4
|
|
|
|
|
16
|
next; |
279
|
|
|
|
|
|
|
} |
280
|
|
|
|
|
|
|
} |
281
|
|
|
|
|
|
|
} |
282
|
|
|
|
|
|
|
|
283
|
208
|
100
|
|
|
|
481
|
if ($param eq 'quotemeta') { |
|
|
50
|
|
|
|
|
|
284
|
|
|
|
|
|
|
# only report on chars quotemeta leaves unchanged |
285
|
2
|
50
|
|
|
|
23
|
next if $c ne quotemeta($c); |
286
|
|
|
|
|
|
|
} elsif ($param eq 'alnum') { |
287
|
|
|
|
|
|
|
# only report unknown alphanumerics, like perl does |
288
|
|
|
|
|
|
|
# believe perl only reports ascii alnums as bad, wide char alphas ok |
289
|
0
|
0
|
|
|
|
0
|
next if $c !~ /[a-zA-Z0-9]/; |
290
|
|
|
|
|
|
|
} |
291
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
# if $c eq '' for end-of-string then index() returns 0, for no violation |
293
|
206
|
100
|
|
|
|
491
|
if (index ($known, $c) >= 0) { |
294
|
|
|
|
|
|
|
# a known escape |
295
|
86
|
|
|
|
|
271
|
next; |
296
|
|
|
|
|
|
|
} |
297
|
|
|
|
|
|
|
|
298
|
120
|
|
100
|
|
|
496
|
my $explain = !$single && ($explain{$c} || ''); |
299
|
120
|
|
|
|
|
287
|
my $message = ('Unknown or unnecessary backslash \\'._printable($c) |
300
|
|
|
|
|
|
|
. $explain); |
301
|
120
|
|
|
|
|
419
|
push @violations, $self->violation ($message, '', $elem); |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
# would have to take into account HereDoc begins on next line ... |
304
|
|
|
|
|
|
|
# _violation_elem_offset ($violation, $elem, pos($str)-2); |
305
|
|
|
|
|
|
|
} |
306
|
256
|
|
|
|
|
16664
|
return @violations; |
307
|
|
|
|
|
|
|
} |
308
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
# $pos is a position within $str of a "$" or "@" interpolation. |
310
|
|
|
|
|
|
|
# Return the position within $str after that variable or expression. |
311
|
|
|
|
|
|
|
# |
312
|
|
|
|
|
|
|
# FIXME: Would like PPI to do this. Its PPI::Token::Quote::Double version |
313
|
|
|
|
|
|
|
# 1.236 interpolations() has a comment that returning the expressions would |
314
|
|
|
|
|
|
|
# be good. |
315
|
|
|
|
|
|
|
# |
316
|
|
|
|
|
|
|
sub _pos_after_interpolate_variable { |
317
|
72
|
|
|
72
|
|
10932
|
my ($str, $pos) = @_; |
318
|
72
|
|
|
|
|
143
|
$str = substr ($str, $pos); |
319
|
|
|
|
|
|
|
### _pos_after_interpolate_variable() ... |
320
|
|
|
|
|
|
|
### $str |
321
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
# PPI circa 1.236 doesn't like to parse non-ascii as program code |
323
|
|
|
|
|
|
|
# identifiers etc, try changing to spaces for measuring. |
324
|
|
|
|
|
|
|
# |
325
|
|
|
|
|
|
|
# Might be happy for it to parse the interpolate expression and ignore |
326
|
|
|
|
|
|
|
# anything bad after, but PPI::Tokenizer crunches a whole line at a time |
327
|
|
|
|
|
|
|
# or something like that. |
328
|
|
|
|
|
|
|
# |
329
|
72
|
|
|
|
|
175
|
$str =~ s/[^[:print:]\t\r\n]/ /g; |
330
|
|
|
|
|
|
|
|
331
|
72
|
|
|
|
|
739
|
require PPI::Document; |
332
|
72
|
|
|
|
|
32161
|
my $doc = PPI::Document->new(\$str); |
333
|
72
|
|
33
|
|
|
85050
|
my $elem = $doc && $doc->child(0); |
334
|
72
|
|
33
|
|
|
680
|
$elem = $elem && $elem->child(0); |
335
|
72
|
50
|
|
|
|
501
|
if (! $elem) { |
336
|
0
|
|
|
|
|
0
|
warn "ProhibitUnknownBackslash: oops, cannot parse interpolation, skipping string"; |
337
|
0
|
|
|
|
|
0
|
return undef; |
338
|
|
|
|
|
|
|
} |
339
|
|
|
|
|
|
|
### elem: ref $elem |
340
|
|
|
|
|
|
|
### length: length($elem->content) |
341
|
72
|
|
|
|
|
178
|
$pos += length($elem->content); |
342
|
|
|
|
|
|
|
|
343
|
72
|
100
|
|
|
|
453
|
if ($elem->isa('PPI::Token::Cast')) { |
|
|
100
|
|
|
|
|
|
344
|
|
|
|
|
|
|
# get the PPI::Structure::Block following "$" or "@", can have |
345
|
|
|
|
|
|
|
# whitespace before it too |
346
|
10
|
|
|
|
|
50
|
while ($elem = $elem->next_sibling) { |
347
|
|
|
|
|
|
|
### and: "$elem" |
348
|
|
|
|
|
|
|
### length: length($elem->content) |
349
|
10
|
|
|
|
|
271
|
$pos += length($elem->content); |
350
|
10
|
50
|
|
|
|
377
|
last if $elem->isa('PPI::Structure::Block'); |
351
|
|
|
|
|
|
|
} |
352
|
|
|
|
|
|
|
|
353
|
|
|
|
|
|
|
} elsif ($elem->isa('PPI::Token::Symbol')) { |
354
|
|
|
|
|
|
|
# any subscripts 'PPI::Structure::Subscript' following, like "$hash{...}" |
355
|
|
|
|
|
|
|
# whitespace stops the subscripts, so that Struct alone |
356
|
58
|
|
|
|
|
79
|
for (;;) { |
357
|
92
|
|
100
|
|
|
937
|
$elem = $elem->next_sibling || last; |
358
|
87
|
100
|
|
|
|
1738
|
$elem->isa('PPI::Structure::Subscript') || last; |
359
|
|
|
|
|
|
|
### and: "$elem" |
360
|
|
|
|
|
|
|
### length: length($elem->content) |
361
|
34
|
|
|
|
|
83
|
$pos += length($elem->content); |
362
|
|
|
|
|
|
|
} |
363
|
|
|
|
|
|
|
} |
364
|
|
|
|
|
|
|
|
365
|
72
|
|
|
|
|
287
|
return $pos; |
366
|
|
|
|
|
|
|
} |
367
|
|
|
|
|
|
|
|
368
|
|
|
|
|
|
|
# use Perl::Critic::Policy::Compatibility::PodMinimumVersion; |
369
|
|
|
|
|
|
|
sub _violation_elem_offset { |
370
|
0
|
|
|
0
|
|
0
|
my ($violation, $elem, $offset) = @_; |
371
|
0
|
|
|
|
|
0
|
return $violation; |
372
|
|
|
|
|
|
|
|
373
|
|
|
|
|
|
|
# |
374
|
|
|
|
|
|
|
# my $pre = substr ($elem->content, 0, $offset); |
375
|
|
|
|
|
|
|
# my $newlines = ($pre =~ tr/\n//); |
376
|
|
|
|
|
|
|
# |
377
|
|
|
|
|
|
|
# my $document = $elem->document; |
378
|
|
|
|
|
|
|
# my $doc_str = $document->content; |
379
|
|
|
|
|
|
|
# |
380
|
|
|
|
|
|
|
# return Perl::Critic::Pulp::Utils::_violation_override_linenum ($violation, $doc_str, $newlines - 1); |
381
|
|
|
|
|
|
|
} |
382
|
|
|
|
|
|
|
|
383
|
|
|
|
|
|
|
sub _printable { |
384
|
126
|
|
|
126
|
|
251
|
my ($c) = @_; |
385
|
126
|
100
|
|
|
|
350
|
$c =~ s{([^[:graph:]]|[^[:ascii:]])} |
|
12
|
|
|
|
|
75
|
|
386
|
126
|
|
|
|
|
351
|
{ $charname{$1} || sprintf('{0x%X}',ord($1)) }e; |
387
|
|
|
|
|
|
|
return $c; |
388
|
|
|
|
|
|
|
} |
389
|
|
|
|
|
|
|
|
390
|
|
|
|
|
|
|
# return true if $elem has a 'use charnames' in its lexical scope |
391
|
12
|
|
|
12
|
|
18
|
sub _charnames_in_scope { |
392
|
12
|
|
|
|
|
21
|
my ($elem) = @_; |
393
|
36
|
|
100
|
|
|
179
|
for (;;) { |
394
|
|
|
|
|
|
|
$elem = $elem->sprevious_sibling || $elem->parent |
395
|
28
|
100
|
66
|
|
|
676
|
|| return 0; |
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
396
|
|
|
|
|
|
|
if ($elem->isa ('PPI::Statement::Include') |
397
|
|
|
|
|
|
|
&& $elem->type eq 'use' |
398
|
4
|
|
|
|
|
167
|
&& ($elem->module || '') eq 'charnames') { |
399
|
|
|
|
|
|
|
return 1; |
400
|
|
|
|
|
|
|
} |
401
|
|
|
|
|
|
|
} |
402
|
|
|
|
|
|
|
} |
403
|
|
|
|
|
|
|
|
404
|
|
|
|
|
|
|
|
405
|
|
|
|
|
|
|
#----------------------------------------------------------------------------- |
406
|
|
|
|
|
|
|
# unused bits |
407
|
|
|
|
|
|
|
|
408
|
|
|
|
|
|
|
# # $elem is a PPI::Token::Quote, PPI::Token::QuoteLike or PPI::Token::HereDoc |
409
|
|
|
|
|
|
|
# sub _string { |
410
|
|
|
|
|
|
|
# my ($elem) = @_; |
411
|
|
|
|
|
|
|
# if ($elem->can('heredoc')) { |
412
|
|
|
|
|
|
|
# return join ('', $elem->heredoc); |
413
|
|
|
|
|
|
|
# } |
414
|
|
|
|
|
|
|
# if ($elem->can('string')) { |
415
|
|
|
|
|
|
|
# return $elem->string; |
416
|
|
|
|
|
|
|
# } |
417
|
|
|
|
|
|
|
# $elem =~ $quotelike_re |
418
|
|
|
|
|
|
|
# or die "Oops, didn't match quote_re"; |
419
|
|
|
|
|
|
|
# return $3; |
420
|
|
|
|
|
|
|
# } |
421
|
|
|
|
|
|
|
|
422
|
|
|
|
|
|
|
# # $elem is a PPI::Token::Quote or PPI::Token::QuoteLike |
423
|
|
|
|
|
|
|
# # return ($q, $open, $close) where $q is the "q" intro or empty string if |
424
|
|
|
|
|
|
|
# # none, and $open and $close are the quote chars |
425
|
|
|
|
|
|
|
# sub _quote_delims { |
426
|
|
|
|
|
|
|
# my ($elem) = @_; |
427
|
|
|
|
|
|
|
# if ($elem->can('heredoc')) { |
428
|
|
|
|
|
|
|
# return '"', '"'; |
429
|
|
|
|
|
|
|
# } |
430
|
|
|
|
|
|
|
# $elem =~ $quotelike_re |
431
|
|
|
|
|
|
|
# or die "Oops, didn't match quote_re"; |
432
|
|
|
|
|
|
|
# return ($1||'', $2, $4); |
433
|
|
|
|
|
|
|
# } |
434
|
|
|
|
|
|
|
|
435
|
|
|
|
|
|
|
# perlop "Quote and Quote-like Operators" |
436
|
|
|
|
|
|
|
# my $known = ''; |
437
|
|
|
|
|
|
|
# if ($elem->isa ('PPI::Token::Quote::Double') |
438
|
|
|
|
|
|
|
# || $elem->isa ('PPI::Token::Quote::Interpolate') |
439
|
|
|
|
|
|
|
# || $elem->isa ('PPI::Token::QuoteLike::Backtick') |
440
|
|
|
|
|
|
|
# || ($elem->isa ('PPI::Token::QuoteLike::Command') |
441
|
|
|
|
|
|
|
# && $close ne '\'') # no interpolation in qx'echo hi' |
442
|
|
|
|
|
|
|
# ) { |
443
|
|
|
|
|
|
|
# $known = 'tnrfbae0123xcluLUQE$@'; |
444
|
|
|
|
|
|
|
# |
445
|
|
|
|
|
|
|
# # \N and octals bigger than 8-bits are in 5.6 up, and allow them if no |
446
|
|
|
|
|
|
|
# # "use 5.x" at all too |
447
|
|
|
|
|
|
|
# my $perlver = $document->highest_explicit_perl_version; |
448
|
|
|
|
|
|
|
# if (! defined $perlver || $perlver >= 5.006) { |
449
|
|
|
|
|
|
|
# $known .= 'N456789'; |
450
|
|
|
|
|
|
|
# } |
451
|
|
|
|
|
|
|
# } |
452
|
|
|
|
|
|
|
# $known .= $close; |
453
|
|
|
|
|
|
|
# |
454
|
|
|
|
|
|
|
# my $re = qr/\\+[^\\$known$close]/; |
455
|
|
|
|
|
|
|
# my $unknown = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
456
|
|
|
|
|
|
|
# $unknown =~ s{(.)} |
457
|
|
|
|
|
|
|
# {index($known,$1) >= 0 ? '' : $1}eg; |
458
|
|
|
|
|
|
|
|
459
|
|
|
|
|
|
|
1; |
460
|
|
|
|
|
|
|
__END__ |
461
|
|
|
|
|
|
|
|
462
|
|
|
|
|
|
|
=for stopwords backslashed upcase FS unicode ascii non-ascii ok alnum quotemeta backslashing backticks Ryde coderef alphanumerics arrowed |
463
|
|
|
|
|
|
|
|
464
|
|
|
|
|
|
|
=head1 NAME |
465
|
|
|
|
|
|
|
|
466
|
|
|
|
|
|
|
Perl::Critic::Policy::ValuesAndExpressions::ProhibitUnknownBackslash - don't use undefined backslash forms |
467
|
|
|
|
|
|
|
|
468
|
|
|
|
|
|
|
=head1 DESCRIPTION |
469
|
|
|
|
|
|
|
|
470
|
|
|
|
|
|
|
This policy is part of the L<C<Perl::Critic::Pulp>|Perl::Critic::Pulp> |
471
|
|
|
|
|
|
|
add-on. It checks for unknown backslash escapes like |
472
|
|
|
|
|
|
|
|
473
|
|
|
|
|
|
|
print "\*.c"; # bad |
474
|
|
|
|
|
|
|
|
475
|
|
|
|
|
|
|
This is harmless, assuming the intention is a literal "*" (which it gives), |
476
|
|
|
|
|
|
|
but unnecessary, and on that basis this policy is under the C<cosmetic> |
477
|
|
|
|
|
|
|
theme (see L<Perl::Critic/POLICY THEMES>). Sometimes it can be a |
478
|
|
|
|
|
|
|
misunderstanding or a typo though, for instance a backslashed newline is a |
479
|
|
|
|
|
|
|
newline, but perhaps you thought it meant a continuation. |
480
|
|
|
|
|
|
|
|
481
|
|
|
|
|
|
|
print "this\ # bad |
482
|
|
|
|
|
|
|
is a newline"; |
483
|
|
|
|
|
|
|
|
484
|
|
|
|
|
|
|
Perl already warns about unknown escaped alphanumerics like C<\v> under |
485
|
|
|
|
|
|
|
C<perl -w> or C<use warnings> (see L<perldiag/Unrecognized escape \%c passed |
486
|
|
|
|
|
|
|
through>). |
487
|
|
|
|
|
|
|
|
488
|
|
|
|
|
|
|
print "\v"; # bad, and provokes Perl warning |
489
|
|
|
|
|
|
|
|
490
|
|
|
|
|
|
|
This policy extends to report on any unknown escape, with options below to |
491
|
|
|
|
|
|
|
vary the strictness and to check single-quote strings too if desired. |
492
|
|
|
|
|
|
|
|
493
|
|
|
|
|
|
|
=head2 Control Characters \c |
494
|
|
|
|
|
|
|
|
495
|
|
|
|
|
|
|
Control characters C<\cX> are checked and only the conventional A-Z a-z @ [ |
496
|
|
|
|
|
|
|
\ ] ^ _ ? are considered known. |
497
|
|
|
|
|
|
|
|
498
|
|
|
|
|
|
|
print "\c*"; # bad |
499
|
|
|
|
|
|
|
|
500
|
|
|
|
|
|
|
Perl accepts any C<\c> and does an upcase xor 0x40, so C<\c*> is letter "j", |
501
|
|
|
|
|
|
|
at least on an ASCII system. But that's obscure and likely to be a typo or |
502
|
|
|
|
|
|
|
error. |
503
|
|
|
|
|
|
|
|
504
|
|
|
|
|
|
|
For reference, C<\c\> is the ASCII FS "file separator" and the second |
505
|
|
|
|
|
|
|
backslash is not an escape, except for a closing quote character, which it |
506
|
|
|
|
|
|
|
does escape (basically because Perl scans for a closing quote before |
507
|
|
|
|
|
|
|
considering interpolations). Thus, |
508
|
|
|
|
|
|
|
|
509
|
|
|
|
|
|
|
print " \c\ "; # ok, control-\ FS |
510
|
|
|
|
|
|
|
print " \c\" "; # bad, control-" is unknown |
511
|
|
|
|
|
|
|
print qq[ \c\] ]; # ok, control-] GS |
512
|
|
|
|
|
|
|
|
513
|
|
|
|
|
|
|
=head2 Ending Interpolation |
514
|
|
|
|
|
|
|
|
515
|
|
|
|
|
|
|
A backslashed colon, bracket, brace or dash is allowed after an interpolated |
516
|
|
|
|
|
|
|
variable or element to stop interpolation at that point. |
517
|
|
|
|
|
|
|
|
518
|
|
|
|
|
|
|
print "$foo\::bar"; # ok, $foo |
519
|
|
|
|
|
|
|
print "@foo\::"; # ok, @foo |
520
|
|
|
|
|
|
|
|
521
|
|
|
|
|
|
|
print "$foo[0]\[1]"; # ok, is $foo[0] |
522
|
|
|
|
|
|
|
print "$esc\[1m"; # ok |
523
|
|
|
|
|
|
|
|
524
|
|
|
|
|
|
|
print "$foo\{k}"; # ok |
525
|
|
|
|
|
|
|
print "$foo\{k}"; # ok |
526
|
|
|
|
|
|
|
print "$foo{k}\[0]"; # ok, is $foo{k} |
527
|
|
|
|
|
|
|
print "@foo\{1,2}"; # ok, is @foo |
528
|
|
|
|
|
|
|
|
529
|
|
|
|
|
|
|
print "$foo\->[0]"; # ok, is $foo |
530
|
|
|
|
|
|
|
print "$foo\->{zz}"; # ok |
531
|
|
|
|
|
|
|
|
532
|
|
|
|
|
|
|
A single backslash like C<"\::"> is enough for the colon case, but |
533
|
|
|
|
|
|
|
backslashing the second too as C<"\:\:"> is quite common and is allowed. |
534
|
|
|
|
|
|
|
|
535
|
|
|
|
|
|
|
print "$#foo\:\:bar"; # ok |
536
|
|
|
|
|
|
|
|
537
|
|
|
|
|
|
|
Only a C<-E<gt>[]> or C<-E<gt>{}> needs a C<\-> to stop interpolation. |
538
|
|
|
|
|
|
|
Other cases such as an apparent method call or arrowed coderef call don't |
539
|
|
|
|
|
|
|
interpolate and the backslash is treated as unknown since unnecessary. |
540
|
|
|
|
|
|
|
|
541
|
|
|
|
|
|
|
print "$coderef\->(123)"; # bad, unnecessary |
542
|
|
|
|
|
|
|
print "Usage: $class\->foo()"; # bad, unnecessary |
543
|
|
|
|
|
|
|
|
544
|
|
|
|
|
|
|
For reference, the alternative in all the above is to write C<{}> braces |
545
|
|
|
|
|
|
|
around the variable or element to delimit from anything following. Doing so |
546
|
|
|
|
|
|
|
may be clearer than backslashing, |
547
|
|
|
|
|
|
|
|
548
|
|
|
|
|
|
|
print "${foo}::bar"; # alternatives |
549
|
|
|
|
|
|
|
print "@{foo}::bar"; |
550
|
|
|
|
|
|
|
print "$#{foo}th"; |
551
|
|
|
|
|
|
|
print "${foo[0]}[1]"; # array element $foo[0] |
552
|
|
|
|
|
|
|
|
553
|
|
|
|
|
|
|
The full horror story of backslashing interpolations can be found in |
554
|
|
|
|
|
|
|
L<perlop/Gory details of parsing quoted constructs>. |
555
|
|
|
|
|
|
|
|
556
|
|
|
|
|
|
|
=head2 Octal Wide Chars |
557
|
|
|
|
|
|
|
|
558
|
|
|
|
|
|
|
Octal escapes above C<\400> to C<\777> for wide chars 256 to 511 are new in |
559
|
|
|
|
|
|
|
Perl 5.6. They're considered unknown in 5.005 and earlier (where they end |
560
|
|
|
|
|
|
|
up chopped to 8-bits 0 to 255). Currently if there's no C<use> etc Perl |
561
|
|
|
|
|
|
|
version then it's presumed a high octal is intentional and is allowed. |
562
|
|
|
|
|
|
|
|
563
|
|
|
|
|
|
|
print "\400"; # ok |
564
|
|
|
|
|
|
|
|
565
|
|
|
|
|
|
|
use 5.006; |
566
|
|
|
|
|
|
|
print "\777"; # ok |
567
|
|
|
|
|
|
|
|
568
|
|
|
|
|
|
|
use 5.005; |
569
|
|
|
|
|
|
|
print "\777"; # bad in 5.005 and earlier |
570
|
|
|
|
|
|
|
|
571
|
|
|
|
|
|
|
|
572
|
|
|
|
|
|
|
=head2 Named Chars |
573
|
|
|
|
|
|
|
|
574
|
|
|
|
|
|
|
Named chars C<\N{SOME THING}> are added by L<charnames>, new in Perl 5.6, |
575
|
|
|
|
|
|
|
and it is autoloaded in Perl 5.16 up when used. C<\N> is treated as known |
576
|
|
|
|
|
|
|
if C<use 5.016> or higher, |
577
|
|
|
|
|
|
|
|
578
|
|
|
|
|
|
|
use 5.016; |
579
|
|
|
|
|
|
|
print "\N{EQUALS SIGN}"; # ok with 5.16 automatic charnames |
580
|
|
|
|
|
|
|
|
581
|
|
|
|
|
|
|
Or if C<use charnames> in the lexical scope, |
582
|
|
|
|
|
|
|
|
583
|
|
|
|
|
|
|
{ |
584
|
|
|
|
|
|
|
use charnames ':full'; |
585
|
|
|
|
|
|
|
print "\N{APOSTROPHE}"; # ok |
586
|
|
|
|
|
|
|
} |
587
|
|
|
|
|
|
|
print "\N{COLON}"; # bad, no charnames in lexical scope |
588
|
|
|
|
|
|
|
|
589
|
|
|
|
|
|
|
In Perl 5.6 through 5.14 a C<\N> without C<charnames> is a compile error so |
590
|
|
|
|
|
|
|
would normally be seen immediately anyway. There's no check of the |
591
|
|
|
|
|
|
|
character name appearing in the C<\N>. C<charnames> gives an error for |
592
|
|
|
|
|
|
|
unknown names. |
593
|
|
|
|
|
|
|
|
594
|
|
|
|
|
|
|
=head2 Other Notes |
595
|
|
|
|
|
|
|
|
596
|
|
|
|
|
|
|
In the violation messages a non-ascii or non-graphical escaped char is shown |
597
|
|
|
|
|
|
|
as hex like C<\{0x263A}>, to ensure the message is printable and |
598
|
|
|
|
|
|
|
unambiguous. |
599
|
|
|
|
|
|
|
|
600
|
|
|
|
|
|
|
Interpolated C<$foo> or C<@{expr}> variables and expressions are parsed like |
601
|
|
|
|
|
|
|
Perl does, so backslashes for refs within are ok, in particular tricks like |
602
|
|
|
|
|
|
|
C<${\scalar ...}> are fine (see L<perlfaq4/How do I expand function calls in |
603
|
|
|
|
|
|
|
a string?>). |
604
|
|
|
|
|
|
|
|
605
|
|
|
|
|
|
|
print "this ${\(some()+thing())}"; # ok |
606
|
|
|
|
|
|
|
|
607
|
|
|
|
|
|
|
=head2 Disabling |
608
|
|
|
|
|
|
|
|
609
|
|
|
|
|
|
|
As always, if you're not interested in any of this then you can disable |
610
|
|
|
|
|
|
|
C<ProhibitUnknownBackslash> from your F<.perlcriticrc> in the usual way (see |
611
|
|
|
|
|
|
|
L<Perl::Critic/CONFIGURATION>), |
612
|
|
|
|
|
|
|
|
613
|
|
|
|
|
|
|
[-ValuesAndExpressions::ProhibitUnknownBackslash] |
614
|
|
|
|
|
|
|
|
615
|
|
|
|
|
|
|
=head1 CONFIGURATION |
616
|
|
|
|
|
|
|
|
617
|
|
|
|
|
|
|
=over 4 |
618
|
|
|
|
|
|
|
|
619
|
|
|
|
|
|
|
=item C<double> (string, default "all") |
620
|
|
|
|
|
|
|
|
621
|
|
|
|
|
|
|
=item C<heredoc> (string, default "all") |
622
|
|
|
|
|
|
|
|
623
|
|
|
|
|
|
|
C<double> applies to double-quote strings C<"">, C<qq{}>, C<qx{}>, etc. |
624
|
|
|
|
|
|
|
C<heredoc> applies to interpolated here-documents C<E<lt>E<lt>HERE> etc. |
625
|
|
|
|
|
|
|
The possible values are |
626
|
|
|
|
|
|
|
|
627
|
|
|
|
|
|
|
none don't report anything |
628
|
|
|
|
|
|
|
alnum report unknown alphanumerics, like Perl's warning |
629
|
|
|
|
|
|
|
quotemeta report anything quotemeta() doesn't escape |
630
|
|
|
|
|
|
|
all report all unknowns |
631
|
|
|
|
|
|
|
|
632
|
|
|
|
|
|
|
"alnum" does no more than compiling with C<perl -w>, but might be good for |
633
|
|
|
|
|
|
|
checking code you don't want to run. |
634
|
|
|
|
|
|
|
|
635
|
|
|
|
|
|
|
"quotemeta" reports escapes not produced by C<quotemeta()>. For example |
636
|
|
|
|
|
|
|
C<quotemeta> escapes a C<*>, so C<\*> is not reported, but it doesn't escape |
637
|
|
|
|
|
|
|
an underscore C<_>, so C<\_> is reported. The effect is to prohibit a few |
638
|
|
|
|
|
|
|
more escapes than "alnum". One use is to check code generated by other code |
639
|
|
|
|
|
|
|
where you've used C<quotemeta> to produce double-quoted strings and thus may |
640
|
|
|
|
|
|
|
have escaping which is unnecessary but works fine. |
641
|
|
|
|
|
|
|
|
642
|
|
|
|
|
|
|
=item C<single> (string, default "none") |
643
|
|
|
|
|
|
|
|
644
|
|
|
|
|
|
|
C<single> applies to single-quote strings C<''>, C<q{}>, C<qx''>, etc. The |
645
|
|
|
|
|
|
|
possible values are as above, though only "all" or "none" make much sense. |
646
|
|
|
|
|
|
|
|
647
|
|
|
|
|
|
|
none don't report anything |
648
|
|
|
|
|
|
|
all report all unknowns |
649
|
|
|
|
|
|
|
|
650
|
|
|
|
|
|
|
The default is "none" because literal backslashes in single-quotes are |
651
|
|
|
|
|
|
|
usually both what you want and quite convenient. Setting "all" effectively |
652
|
|
|
|
|
|
|
means you must write backslashes as C<\\>. |
653
|
|
|
|
|
|
|
|
654
|
|
|
|
|
|
|
print 'c:\my\msdos\filename'; # bad under "single=all" |
655
|
|
|
|
|
|
|
print 'c:\\my\\msdos\\filename'; # ok |
656
|
|
|
|
|
|
|
|
657
|
|
|
|
|
|
|
Doubled backslashing like this is correct, and can emphasize that you really |
658
|
|
|
|
|
|
|
did want a backslash, but it's tedious and not easy on the eye and so left |
659
|
|
|
|
|
|
|
only as an option. |
660
|
|
|
|
|
|
|
|
661
|
|
|
|
|
|
|
For reference, single-quote here-documents C<E<lt>E<lt>'HERE'> don't have |
662
|
|
|
|
|
|
|
any backslash escapes and so are not considered by this policy. C<qx{}> |
663
|
|
|
|
|
|
|
command backticks are double-quote but as C<qx''> is single-quote. They are |
664
|
|
|
|
|
|
|
treated per the corresponding C<single> or C<double> option. |
665
|
|
|
|
|
|
|
|
666
|
|
|
|
|
|
|
=back |
667
|
|
|
|
|
|
|
|
668
|
|
|
|
|
|
|
=head1 BUGS |
669
|
|
|
|
|
|
|
|
670
|
|
|
|
|
|
|
Interpolations in double-quote strings are found by some code here in |
671
|
|
|
|
|
|
|
C<ProhibitUnknownBackslash> (re-parse the string content as Perl code |
672
|
|
|
|
|
|
|
starting from the C<$> or C<@>). If this fails for some reason then a |
673
|
|
|
|
|
|
|
warning is given and the rest of the string is unchecked. In the future |
674
|
|
|
|
|
|
|
would like PPI to parse interpolations, for the benefit of string chopping |
675
|
|
|
|
|
|
|
like here or checking of code in an interpolation. |
676
|
|
|
|
|
|
|
|
677
|
|
|
|
|
|
|
=head1 SEE ALSO |
678
|
|
|
|
|
|
|
|
679
|
|
|
|
|
|
|
L<Perl::Critic::Pulp>, |
680
|
|
|
|
|
|
|
L<Perl::Critic> |
681
|
|
|
|
|
|
|
|
682
|
|
|
|
|
|
|
L<perlop/Quote and Quote-like Operators> |
683
|
|
|
|
|
|
|
|
684
|
|
|
|
|
|
|
=head1 HOME PAGE |
685
|
|
|
|
|
|
|
|
686
|
|
|
|
|
|
|
http://user42.tuxfamily.org/perl-critic-pulp/index.html |
687
|
|
|
|
|
|
|
|
688
|
|
|
|
|
|
|
=head1 COPYRIGHT |
689
|
|
|
|
|
|
|
|
690
|
|
|
|
|
|
|
Copyright 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 Kevin Ryde |
691
|
|
|
|
|
|
|
|
692
|
|
|
|
|
|
|
Perl-Critic-Pulp is free software; you can redistribute it and/or modify it |
693
|
|
|
|
|
|
|
under the terms of the GNU General Public License as published by the Free |
694
|
|
|
|
|
|
|
Software Foundation; either version 3, or (at your option) any later |
695
|
|
|
|
|
|
|
version. |
696
|
|
|
|
|
|
|
|
697
|
|
|
|
|
|
|
Perl-Critic-Pulp is distributed in the hope that it will be useful, but |
698
|
|
|
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
699
|
|
|
|
|
|
|
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
700
|
|
|
|
|
|
|
more details. |
701
|
|
|
|
|
|
|
|
702
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with |
703
|
|
|
|
|
|
|
Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses>. |
704
|
|
|
|
|
|
|
|
705
|
|
|
|
|
|
|
=cut |