Policy/ValuesAndExpressions/ProhibitUnknownBackslash.pm

filter: Statementstmt 96.8% 154 / 159 Branchbran 84.0% 79 / 94 Conditioncond 83.5% 56 / 67 MC/DCmcdc 61.7% 21 / 34 Subroutinesub 94.7% 18 / 19 Podpod 100% 2 / 2 totaltotal 88.0% 330 / 375 CC 72 SCAR 34.6
CC
72
Cov
90%
violates
52.0
_pos_after_interpolate_variable
11.1
applies_to
7.0
CRAP 31.9
« Policy/Compatibility/PerlMinimumVersionAndWhy.pm ↑ Summary Policy/ValuesAndExpressions/ProhibitDuplicateHashKeys.pm »
1 # Copyright 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021, 2025 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 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 40 ▶use version (); # but don't import qv()
Subroutine BEGIN: called CC=1 SCAR=0.0
21 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 # 1.084 for Perl::Critic::Document highest_explicit_perl_version()
24 40 ▶use Perl::Critic::Policy 1.084;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 40 ▶use base 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 40 ▶use Perl::Critic::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 40 ▶use Perl::Critic::Pulp;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 # uncomment this to run the ### lines
31 # use Smart::Comments;
32  
33 40 our $VERSION = 100;
34  
35 40 ▶use constant supported_parameters =>
Subroutine BEGIN: called CC=1 SCAR=0.0
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 },
51 { name => 'charnames',
52 description => 'Checking of character names \\N{}.',
53 behavior => 'string',
54 default_string => 'version',
55 },
56 { name => 'foldcase',
57 description => 'Checking of fold case \\F.',
58 behavior => 'string',
59 default_string => 'version',
60 40 });
61 40 ▶use constant default_severity => $Perl::Critic::Utils::SEVERITY_MEDIUM;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 40 ▶use constant default_themes => qw(pulp cosmetic);
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 302 ▶sub applies_to {
Subroutine applies_to: called CC=6 SCAR=19.5
Pod documented
65 302 my ($policy) = @_;
66 return (($policy->{'_single'} ne 'none'
67 ? ('PPI::Token::Quote::Single', # ''
68 'PPI::Token::Quote::Literal') # q{}
69 : ()),
70  
71 ($policy->{'_single'} ne 'none'
72 || $policy->{'_double'} ne 'none'
73 ? ('PPI::Token::QuoteLike::Command') # qx{} or qx''
74 : ()),
75  
76 ($policy->{'_double'} ne 'none'
77 ? ('PPI::Token::Quote::Double', # ""
78 'PPI::Token::Quote::Interpolate', # qq{}
79 'PPI::Token::QuoteLike::Backtick') # ``
80 : ()),
81  
82 302 ▶ ($policy->{'_heredoc'} ne 'none'
Branch: 4 branches
BranchTrueFalseTotal
$policy->{'_single'} ne 'none' ? : 38 264 302
$policy->{'_single'} ne 'none' || $policy->{'_double'} ne 'none' ? : 302 0 302
$policy->{'_double'} ne 'none' ? : 302 0 302
$policy->{'_heredoc'} ne 'none' ? : 302 0 302
Condition66%2/3
$policy->{"_single"} ne "none" or $policy->{"_double"} ne "none"
l!l&&r!l&&!r
382640
MC/DC0%0/2
$policy->{"_single"} ne "none" or $policy->{"_double"} ne "none"
$policy->{"_single"} ne "none" $policy->{"_double"} ne "none"
Truth table66%2/3
A or B
A= $policy->{"_single"} ne "none"B= $policy->{"_double"} ne "none"
ABresult
1X1
011
000
83 ? ('PPI::Token::HereDoc')
84 : ()));
85 }
86  
87 # for violation messages
88 40 my %charname = ("\n" => '{newline}',
89 "\r" => '{cr}',
90 "\t" => '{tab}',
91 " " => '{space}');
92  
93 40 ▶use constant _KNOWN => (
Subroutine BEGIN: called CC=1 SCAR=0.0
94 't' # \t tab
95 . 'n' # \n newline
96 . 'r' # \r carriage return
97 . 'f' # \f form feed
98 . 'b' # \b backspace
99 . 'a' # \a bell
100 . 'e' # \e esc
101 . '0123' # \377 octal
102 . 'x' # \xFF \x{FF} hex
103 . 'c' # \cX control char
104  
105 . 'l' # \l lowercase one char
106 . 'u' # \u uppercase one char
107 . 'L' # \L lowercase string
108 . 'U' # \U uppercase string
109 . 'E' # \E end case or quote
110 . 'Q' # \Q quotemeta
111 . '$' # non-interpolation
112 . '@' # non-interpolation
113 40 );
114  
115 40 ▶use constant _CONTROL_KNOWN =>
Subroutine BEGIN: called CC=1 SCAR=0.0
116 40 '?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_abcdefghijklmnopqrstuvwxyz'; ## no critic (RequireInterpolationOfMetachars)
117  
118 40 my $quotelike_re = qr/^(?:(q[qrwx]?) # $1 "q" if present
119 (?:(?:\s(?:\s*\#[^\n]*\n)*)\s*)? # possible comments
120 )? # possible "q"
121 (.) # $2 opening quote
122 (.*) # $3 guts
123 (.)$ # $4 closing quote
124 /xs;
125  
126 # extra explanation for double-quote interpolations
127 40 my %explain = ('%' => ' (hashes are not interpolated)',
128 '&' => ' (function calls are not interpolated)',
129 '4' => ' (until Perl 5.6 octal wide chars)',
130 '5' => ' (until Perl 5.6 octal wide chars)',
131 '6' => ' (until Perl 5.6 octal wide chars)',
132 '7' => ' (until Perl 5.6 octal wide chars)',
133 'F' => ' (until Perl 5.16)',
134 'N' => ' (without "use charnames" in scope)',
135 );
136  
137 40 my $v5006 = version->new('5.006');
138 40 my $v5010 = version->new('5.010');
139 40 my $v5016 = version->new('5.016');
140  
141 290 ▶sub violates {
Subroutine violates: called CC=51 SCAR=39.5
Pod documented
142 290 my ($self, $elem, $document) = @_;
143  
144 290 my $have_perl_516;
145 290 ▶ if (defined (my $doc_version = $document->highest_explicit_perl_version)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $doc_version = $document->highest_explicit_perl_version)) 42 248 290
146 42 $have_perl_516 = ($doc_version >= $v5016);
147 }
148  
149 290 my $content = $elem->content;
150 290 my $close = substr ($content, -1, 1);
151 290 my $single = 0;
152 290 my ($param, $str);
153  
154 290 ▶ if ($elem->isa('PPI::Token::HereDoc')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa('PPI::Token::HereDoc')) { } 6 284 290
155 6 ▶ return if ($close eq "'"); # uninterpolated
Branch: 1 branch
BranchTrueFalseTotal
if $close eq "'" 2 4 6
156 4 $param = $self->{_heredoc};
157 4 $str = join ('', $elem->heredoc);
158  
159 } else {
160 284 ▶ if ($elem->can('string')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->can('string')) { } 270 14 284
161 270 $str = $elem->string;
162 } else {
163 14 ▶ $elem =~ $quotelike_re or die "Oops, didn't match quotelike_re";
Branch: 1 branch
BranchTrueFalseTotal
$elem =~ /$quotelike_re/ or die "Oops, didn't match quotelike_re" 14 0 14
164 14 $str = $3;
165 }
166 284 $str =~ s{((^|\G|[^\\])(\\\\)*)\\\Q$close}{$close}sg;
167  
168 284 ▶ if ($elem->isa('PPI::Token::Quote::Single')
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa('PPI::Token::Quote::Single') or $elem->isa('PPI::Token::Quote::Literal') or $elem->isa('PPI::Token::QuoteLike::Command') and $close eq q[']) { } 42 242 284
Condition100%9/9
$elem->isa("PPI::Token::Quote::Single") or $elem->isa("PPI::Token::Quote::Literal") or $elem->isa("PPI::Token::QuoteLike::Command") and $close eq "'"
l!l&&r!l&&!r
348242
$elem->isa("PPI::Token::Quote::Single") or $elem->isa("PPI::Token::Quote::Literal")
l!l&&r!l&&!r
1618250
$elem->isa("PPI::Token::QuoteLike::Command") and $close eq "'"
!ll&&!rl&&r
23848
MC/DC100%4/4
$elem->isa("PPI::Token::Quote::Single") or $elem->isa("PPI::Token::Quote::Literal") or $elem->isa("PPI::Token::QuoteLike::Command") and $close eq "'"
$elem->isa("PPI::Token::Quote::Single") $elem->isa("PPI::Token::Quote::Literal") $elem->isa("PPI::Token::QuoteLike::Command") $close eq "'"
Truth table100%5/5
A or B or C and D
A= $elem->isa("PPI::Token::Quote::Single")B= $elem->isa("PPI::Token::Quote::Literal")C= $elem->isa("PPI::Token::QuoteLike::Command")D= $close eq "'"
ABCDresult
1XXX1
01XX1
00111
000X0
00100
169 || $elem->isa('PPI::Token::Quote::Literal')
170 || ($elem->isa('PPI::Token::QuoteLike::Command')
171 && $close eq "'")) {
172 42 $single = 1;
173 42 $param = $self->{_single};
174  
175 } else {
176 242 $param = $self->{_double};
177 }
178 }
179 288 ▶ return if ($param eq 'none');
Branch: 1 branch
BranchTrueFalseTotal
if $param eq "none" 4 284 288
180  
181 284 my $known = $close;
182  
183 284 ▶ if (! $single) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($single) 246 38 284
184 246 $known .= _KNOWN;
185  
186 # Octal chars above \377 are in 5.6 up.
187 # Consider known if no "use 5.x" at all, or if present and 5.6 up,
188 # so only under explicit "use 5.005" or lower are they not allowed.
189 246 my $perlver = $document->highest_explicit_perl_version;
190 246 ▶ if (! defined $perlver || $perlver >= $v5006) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $perlver or $perlver >= $v5006) 222 24 246
Condition100%3/3
not defined $perlver or $perlver >= $v5006
l!l&&r!l&&!r
2041824
MC/DC100%2/2
not defined $perlver or $perlver >= $v5006
not defined $perlver $perlver >= $v5006
Truth table100%3/3
not(A) or B
A= not defined $perlverB= $perlver >= $v5006
ABresult
1X1
011
000
191 222 $known .= '4567';
192 }
193  
194 # \F is in 5.16 up.
195 246 ▶ if ($self->{_foldcase} ne 'disallow'
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_foldcase"} ne "disallow" and $self->{"_foldcase"} eq "allow" || defined $perlver && $perlver >= $v5010) 6 240 246
Condition100%9/9
$self->{"_foldcase"} ne "disallow" and $self->{"_foldcase"} eq "allow" || defined $perlver && $perlver >= $v5010
!ll&&!rl&&r
342066
$self->{"_foldcase"} eq "allow" or defined $perlver and $perlver >= $v5010
l!l&&r!l&&!r
42206
defined $perlver and $perlver >= $v5010
!ll&&!rl&&r
178282
MC/DC100%4/4
$self->{"_foldcase"} ne "disallow" and $self->{"_foldcase"} eq "allow" || defined $perlver && $perlver >= $v5010
$self->{"_foldcase"} ne "disallow" $self->{"_foldcase"} eq "allow" defined $perlver $perlver >= $v5010
Truth table100%5/5
A and B or C and D
A= $self->{"_foldcase"} ne "disallow"B= $self->{"_foldcase"} eq "allow"C= defined $perlverD= $perlver >= $v5010
ABCDresult
0XXX0
100X0
10100
11XX1
10111
196 && ($self->{_foldcase} eq 'allow'
197 || (defined $perlver && $perlver >= $v5010))) {
198 6 $known .= 'F';
199 }
200 }
201  
202 ### elem: ref $elem
203 ### $content
204 ### $str
205 ### close char: $close
206 ### $known
207 ### perlver: $document->highest_explicit_perl_version
208  
209 284 my $have_use_charnames;
210 284 my $interpolate_var_end = -1;
211 284 my $interpolate_var_colon;
212 my @violations;
213  
214 284 ▶ while ($str =~ /(\$. # $ not at end-of-string
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /(\$. # $ not at end-of-string |\@[[:alnum:]:'\{\$+-]) # @ forms per toke.c S_scan_const() |(\\+) # $2 run of backslashes /gsx) 386 284 670
215 |\@[[:alnum:]:'\{\$+-]) # @ forms per toke.c S_scan_const()
216 |(\\+) # $2 run of backslashes
217 /sgx) {
218 386 ▶ if (defined $1) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $1) 54 332 386
219 # $ or @
220 54 ▶ unless ($single) { # no variables in single-quote
Branch: 1 branch
BranchTrueFalseTotal
unless ($single) 54 0 54
221 ### interpolation at: pos($str)
222 54 ▶ my $new_pos = _pos_after_interpolate_variable
Condition50%1/2
_pos_after_interpolate_variable($str, pos($str) - length($1)) || (last)
l!l
540
MC/DC0%0/1
_pos_after_interpolate_variable($str, pos($str) - length($1)) || (last)
_pos_after_interpolate_variable($str, pos($str) - length($1))
Truth table50%1/2
A
A= _pos_after_interpolate_variable($str, pos($str) - length($1))
Aresult
11
00
223 ($str, pos($str) - length($1))
224 || last;
225 54 pos($str) = $new_pos;
226 ### ends at: pos($str)
227 54 ▶ if (substr($str,pos($str)-1,1) =~ /(\w)|[]}]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($str, pos($str) - 1, 1) =~ /(\w)|[]}]/) 54 0 54
228 54 $interpolate_var_colon = $1;
229 54 $interpolate_var_end = pos($str);
230 ### interpolate_var_end set to: $interpolate_var_end
231 }
232 }
233 54 next;
234 }
235  
236 332 ▶ if ((length($2) & 1) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((length $2 & 1) == 0) 14 318 332
237 # even number of backslashes, not an escape
238 14 next;
239 }
240  
241 # shouldn't have \ as the last char in $str, but if that happends then
242 # $c is empty string ''
243  
244 318 my $c = substr($str,pos($str),1);
245 318 pos($str)++;
246  
247 318 ▶ if (! $single) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($single) 286 32 318
248 286 ▶ if ($c eq 'N') {
Branch: 5 branches
BranchTrueFalseTotal
if ($c eq 'N') { } 28 258 286
elsif ($c eq 'c') { } 32 226 258
elsif ($c eq ':') { } 34 192 226
elsif ($c eq '[' or $c eq '{') { } 26 166 192
elsif ($c eq '-') { } 10 156 166
249 28 ▶ if ($self->{_charnames} eq 'disallow') {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'_charnames'} eq 'disallow') { } 6 22 28
elsif ($self->{'_charnames'} eq 'allow') { } 6 16 22
250 6 push @violations,
251 $self->violation ('charnames \\N disallowed by config',
252 '', $elem);
253 6 next;
254  
255 } elsif ($self->{_charnames} eq 'allow') {
256 6 next; # ok, allow by config
257  
258 } else { # $self->{_charnames} eq 'version'
259 16 ▶ if (! defined $have_use_charnames) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $have_use_charnames) 16 0 16
260 16 $have_use_charnames = _have_use_charnames_in_scope($elem);
261 }
262 16 ▶ if ($have_use_charnames || $have_perl_516) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_use_charnames or $have_perl_516) 8 8 16
Condition100%3/3
$have_use_charnames or $have_perl_516
l!l&&r!l&&!r
448
MC/DC100%2/2
$have_use_charnames or $have_perl_516
$have_use_charnames $have_perl_516
Truth table100%3/3
A or B
A= $have_use_charnamesB= $have_perl_516
ABresult
1X1
011
000
263 8 next; # ok if "use charnames" or perl 5.16 up (which autoloads that)
264 }
265 }
266  
267 } elsif ($c eq 'c') {
268 # \cX control char.
269 # If \c is at end-of-string then new $c is '' and pos() will goes past
270 # length($str). That pos() is ok, the loop regexp gives no-match and
271 # terminates.
272 32 $c = substr ($str, pos($str)++, 1);
273 32 ▶ if ($c eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "") 4 28 32
274 4 push @violations,
275 $self->violation ('Control char \\c at end of string', '', $elem);
276 4 next;
277 }
278 28 ▶ if (index (_CONTROL_KNOWN, $c) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index("?\@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_abcdefghijklmnopqrstuvwxyz", $c) != -1) 22 6 28
279 22 next; # a known escape
280 }
281 6 push @violations,
282 $self->violation ('Unknown control char \\c' . _printable($c),
283 '', $elem);
284 6 next;
285  
286 } elsif ($c eq ':') {
287 34 ▶ if ($interpolate_var_colon) {
Branch: 1 branch
BranchTrueFalseTotal
if ($interpolate_var_colon) 20 14 34
288 ### backslash colon, pos: pos($str)
289 ### $interpolate_var_end
290 ### substr: substr ($str, $interpolate_var_end, 2)
291 20 ▶ if (pos($str) == $interpolate_var_end+2
Branch: 1 branch
BranchTrueFalseTotal
if (pos $str == $interpolate_var_end + 2 or pos $str == $interpolate_var_end + 4 and substr($str, $interpolate_var_end, 2) eq "\\:") 20 0 20
Condition50%3/6
pos $str == $interpolate_var_end + 2 or pos $str == $interpolate_var_end + 4 and substr($str, $interpolate_var_end, 2) eq "\\:"
l!l&&r!l&&!r
1280
pos $str == $interpolate_var_end + 4 and substr($str, $interpolate_var_end, 2) eq "\\:"
!ll&&!rl&&r
008
MC/DC0%0/3
pos $str == $interpolate_var_end + 2 or pos $str == $interpolate_var_end + 4 and substr($str, $interpolate_var_end, 2) eq "\\:"
pos $str == $interpolate_var_end + 2 pos $str == $interpolate_var_end + 4 substr($str, $interpolate_var_end, 2) eq "\\:"
Truth table50%2/4
A or B and C
A= pos $str == $interpolate_var_end + 2B= pos $str == $interpolate_var_end + 4C= substr($str, $interpolate_var_end, 2) eq "\\:"
ABCresult
1XX1
0111
00X0
0100
292 || (pos($str) == $interpolate_var_end+4
293 && substr ($str, $interpolate_var_end, 2) eq '\\:')) {
294 20 next;
295 }
296 }
297  
298 ▶ } elsif ($c eq '[' || $c eq '{') {
Condition100%3/3
$c eq "[" or $c eq "{"
l!l&&r!l&&!r
1412166
MC/DC100%2/2
$c eq "[" or $c eq "{"
$c eq "[" $c eq "{"
Truth table100%3/3
A or B
A= $c eq "["B= $c eq "{"
ABresult
1X1
011
000
299 ### backslash bracket, pos: pos($str)
300 ### $interpolate_var_end
301 26 ▶ if (pos($str) == $interpolate_var_end+2) {
Branch: 1 branch
BranchTrueFalseTotal
if (pos $str == $interpolate_var_end + 2) 18 8 26
302 18 next;
303 }
304  
305 } elsif ($c eq '-') {
306 ### backslash dash: pos($str)
307 10 ▶ if ($str =~ /\G>[[{]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /\G>[[{]/) 4 6 10
308 ### is for bracket or brace, pos now: pos($str)
309 4 next;
310 }
311 }
312 }
313  
314 224 ▶ if ($param eq 'quotemeta') {
Branch: 2 branches
BranchTrueFalseTotal
if ($param eq 'quotemeta') { } 2 222 224
elsif ($param eq 'alnum') { } 0 222 222
315 # only report on chars quotemeta leaves unchanged
316 2 ▶ next if $c ne quotemeta($c);
Branch: 1 branch
BranchTrueFalseTotal
if $c ne quotemeta $c 2 0 2
317 } elsif ($param eq 'alnum') {
318 # only report unknown alphanumerics, like perl does
319 # believe perl only reports ascii alnums as bad, wide char alphas ok
320 0 ▶ next if $c !~ /[a-zA-Z0-9]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $c =~ /[a-zA-Z0-9]/ 0 0 0
321 }
322  
323 # if $c eq '' for end-of-string then index() returns 0, for no violation
324 222 ▶ if (index ($known, $c) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($known, $c) != -1) 92 130 222
325 # a known escape
326 92 next;
327 }
328  
329 130 ▶ my $explain = !$single && ($explain{$c} || '');
Condition100%5/5
!$single && ($explain{$c} || '')
!ll&&!rl&&r
327226
$explain{$c} or ""
l!l
2672
MC/DC100%2/2
!$single && ($explain{$c} || '')
!$single $explain{$c}
Truth table100%3/3
not(A) && B
A= !$singleB= $explain{$c}
ABresult
0X0
100
111
330 130 my $message = ('Unknown or unnecessary backslash \\'._printable($c)
331 . $explain);
332 130 push @violations, $self->violation ($message, '', $elem);
333  
334 # would have to take into account HereDoc begins on next line ...
335 # _violation_elem_offset ($violation, $elem, pos($str)-2);
336 }
337 284 return @violations;
338 }
339  
340 # $pos is a position within $str of a "$" or "@" interpolation.
341 # Return the position within $str after that variable or expression.
342 #
343 # FIXME: Would like PPI to do this. Its PPI::Token::Quote::Double version
344 # 1.236 interpolations() has a comment that returning the expressions would
345 # be good.
346 #
347 72 ▶sub _pos_after_interpolate_variable {
Subroutine _pos_after_interpolate_variable: called CC=10 SCAR=24.1
348 72 my ($str, $pos) = @_;
349 72 $str = substr ($str, $pos);
350 ### _pos_after_interpolate_variable() ...
351 ### $str
352  
353 # PPI circa 1.236 doesn't like to parse non-ascii as program code
354 # identifiers etc, try changing to spaces for measuring.
355 #
356 # Might be happy for it to parse the interpolate expression and ignore
357 # anything bad after, but PPI::Tokenizer crunches a whole line at a time
358 # or something like that.
359 #
360 72 $str =~ s/[^[:print:]\t\r\n]/ /g;
361  
362 72 require PPI::Document;
363 72 my $doc = PPI::Document->new(\$str);
364 72 ▶ my $elem = $doc && $doc->child(0);
Condition33%1/3
$doc && $doc->child(0)
!ll&&!rl&&r
0072
MC/DC0%0/2
$doc && $doc->child(0)
$doc $doc->child(0)
Truth table33%1/3
A && B
A= $docB= $doc->child(0)
ABresult
0X0
100
111
365 72 ▶ $elem = $elem && $elem->child(0);
Condition33%1/3
$elem && $elem->child(0)
!ll&&!rl&&r
0072
MC/DC0%0/2
$elem && $elem->child(0)
$elem $elem->child(0)
Truth table33%1/3
A && B
A= $elemB= $elem->child(0)
ABresult
0X0
100
111
366 72 ▶ if (! $elem) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($elem) 0 72 72
367 0 warn "ProhibitUnknownBackslash: oops, cannot parse interpolation, skipping string";
368 0 return undef;
369 }
370 ### elem: ref $elem
371 ### length: length($elem->content)
372 72 $pos += length($elem->content);
373  
374 72 ▶ if ($elem->isa('PPI::Token::Cast')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($elem->isa('PPI::Token::Cast')) { } 10 62 72
elsif ($elem->isa('PPI::Token::Symbol')) { } 58 4 62
375 # get the PPI::Structure::Block following "$" or "@", can have
376 # whitespace before it too
377 10 ▶ while ($elem = $elem->next_sibling) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem = $elem->next_sibling) 10 0 10
378 ### and: "$elem"
379 ### length: length($elem->content)
380 10 $pos += length($elem->content);
381 10 ▶ last if $elem->isa('PPI::Structure::Block');
Branch: 1 branch
BranchTrueFalseTotal
if $elem->isa("PPI::Structure::Block") 10 0 10
382 }
383  
384 } elsif ($elem->isa('PPI::Token::Symbol')) {
385 # any subscripts 'PPI::Structure::Subscript' following, like "$hash{...}"
386 # whitespace stops the subscripts, so that Struct alone
387 58 for (;;) {
388 92 ▶ $elem = $elem->next_sibling || last;
Condition100%2/2
$elem->next_sibling || (last)
l!l
875
MC/DC100%1/1
$elem->next_sibling || (last)
$elem->next_sibling
Truth table100%2/2
A
A= $elem->next_sibling
Aresult
11
00
389 87 ▶ $elem->isa('PPI::Structure::Subscript') || last;
Branch: 1 branch
BranchTrueFalseTotal
$elem->isa("PPI::Structure::Subscript") or last 34 53 87
390 ### and: "$elem"
391 ### length: length($elem->content)
392 34 $pos += length($elem->content);
393 }
394 }
395  
396 72 return $pos;
397 }
398  
399 # use Perl::Critic::Policy::Compatibility::PodMinimumVersion;
400 0 ▶sub _violation_elem_offset {
Subroutine _violation_elem_offset: not called CC=1 SCAR=6.9
401 0 my ($violation, $elem, $offset) = @_;
402 0 return $violation;
403  
404 #
405 # my $pre = substr ($elem->content, 0, $offset);
406 # my $newlines = ($pre =~ tr/\n//);
407 #
408 # my $document = $elem->document;
409 # my $doc_str = $document->content;
410 #
411 # return Perl::Critic::Pulp::Utils::_violation_override_linenum ($violation, $doc_str, $newlines - 1);
412 }
413  
414 136 ▶sub _printable {
Subroutine _printable: called CC=2 SCAR=7.0
415 136 my ($c) = @_;
416 136 $c =~ s{([^[:graph:]]|[^[:ascii:]])}
417 12 ▶ { $charname{$1} || sprintf('{0x%X}',ord($1)) }e;
Condition66%2/3
$charname{$1} || sprintf('{0x%X}', ord $1)
l!l&&r!l&&!r
660
MC/DC0%0/2
$charname{$1} || sprintf('{0x%X}', ord $1)
$charname{$1} sprintf('{0x%X}', ord $1)
Truth table66%2/3
A || B
A= $charname{$1}B= sprintf('{0x%X}', ord $1)
ABresult
1X1
011
000
418 136 return $c;
419 }
420  
421 # return true if $elem has a 'use charnames' in its lexical scope
422 16 ▶sub _have_use_charnames_in_scope {
Subroutine _have_use_charnames_in_scope: called CC=7 SCAR=19.5
423 16 my ($elem) = @_;
424 16 for (;;) {
425 48 ▶ $elem = $elem->sprevious_sibling || $elem->parent
Condition100%5/5
$elem->sprevious_sibling || $elem->parent || (return 0)
l!l
3612
$elem->sprevious_sibling or $elem->parent
l!l&&r!l&&!r
82812
MC/DC100%2/2
$elem->sprevious_sibling || $elem->parent || (return 0)
$elem->sprevious_sibling $elem->parent
Truth table100%3/3
A or B
A= $elem->sprevious_siblingB= $elem->parent
ABresult
1X1
011
000
426 || return 0;
427 36 ▶ if ($elem->isa ('PPI::Statement::Include')
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa("PPI::Statement::Include") and $elem->type eq "use" and ($elem->module || "") eq "charnames") 4 32 36
Condition87%7/8
$elem->isa("PPI::Statement::Include") and $elem->type eq "use" and ($elem->module || "") eq "charnames"
!ll&&!rl&&r
3024
$elem->isa("PPI::Statement::Include") and $elem->type eq "use"
!ll&&!rl&&r
3006
$elem->module || ''
l!l
42
MC/DC66%2/3
$elem->isa("PPI::Statement::Include") and $elem->type eq "use" and ($elem->module || "") eq "charnames"
$elem->isa("PPI::Statement::Include") $elem->type eq "use" $elem->module
Truth table75%3/4
A and B and C
A= $elem->isa("PPI::Statement::Include")B= $elem->type eq "use"C= $elem->module
ABCresult
0XX0
10X0
1100
1111
428 && $elem->type eq 'use'
429 && ($elem->module || '') eq 'charnames') {
430 4 return 1;
431 }
432 }
433 }
434  
435  
436 #-----------------------------------------------------------------------------
437 # unused bits
438  
439 # # $elem is a PPI::Token::Quote, PPI::Token::QuoteLike or PPI::Token::HereDoc
440 # sub _string {
441 # my ($elem) = @_;
442 # if ($elem->can('heredoc')) {
443 # return join ('', $elem->heredoc);
444 # }
445 # if ($elem->can('string')) {
446 # return $elem->string;
447 # }
448 # $elem =~ $quotelike_re
449 # or die "Oops, didn't match quote_re";
450 # return $3;
451 # }
452  
453 # # $elem is a PPI::Token::Quote or PPI::Token::QuoteLike
454 # # return ($q, $open, $close) where $q is the "q" intro or empty string if
455 # # none, and $open and $close are the quote chars
456 # sub _quote_delims {
457 # my ($elem) = @_;
458 # if ($elem->can('heredoc')) {
459 # return '"', '"';
460 # }
461 # $elem =~ $quotelike_re
462 # or die "Oops, didn't match quote_re";
463 # return ($1||'', $2, $4);
464 # }
465  
466 # perlop "Quote and Quote-like Operators"
467 # my $known = '';
468 # if ($elem->isa ('PPI::Token::Quote::Double')
469 # || $elem->isa ('PPI::Token::Quote::Interpolate')
470 # || $elem->isa ('PPI::Token::QuoteLike::Backtick')
471 # || ($elem->isa ('PPI::Token::QuoteLike::Command')
472 # && $close ne '\'') # no interpolation in qx'echo hi'
473 # ) {
474 # $known = 'tnrfbae0123xcluLUQE$@';
475 #
476 # # \N and octals bigger than 8-bits are in 5.6 up, and allow them if no
477 # # "use 5.x" at all too
478 # my $perlver = $document->highest_explicit_perl_version;
479 # if (! defined $perlver || $perlver >= 5.006) {
480 # $known .= 'N456789';
481 # }
482 # }
483 # $known .= $close;
484 #
485 # my $re = qr/\\+[^\\$known$close]/;
486 # my $unknown = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
487 # $unknown =~ s{(.)}
488 # {index($known,$1) >= 0 ? '' : $1}eg;
489  
490 40 1;
491 __END__
« Policy/Compatibility/PerlMinimumVersionAndWhy.pm ↑ Summary Policy/ValuesAndExpressions/ProhibitDuplicateHashKeys.pm »