QuoteLike/Utils.pm

filter: Statementstmt 75.4% 154 / 204 Branchbran 50.0% 56 / 112 Conditioncond 13.6% 9 / 66 MC/DCmcdc 2.7% 1 / 36 Subroutinesub 96.5% 28 / 29 Podpod 100% 7 / 7 totaltotal 56.1% 255 / 454 CC 95 SCAR 50.0
CC
95
Cov
57%
__variables
364.8
_is_bareword_subscript
123.7
__ANON__
72.0
CRAP 148.4
↑ Summary QuoteLike/Dumper.pm »
1 package PPIx::QuoteLike::Utils;
2  
3 8 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 8 ▶use base qw{ Exporter };
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use PPIx::QuoteLike::Constant qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
12 HAVE_PPIX_REGEXP
13 LOCATION_LINE
14 LOCATION_CHARACTER
15 LOCATION_COLUMN
16 LOCATION_LOGICAL_LINE
17 LOCATION_LOGICAL_FILE
18 VARIABLE_RE
19 @CARP_NOT
20 8 };
21 8 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 8 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 8 ▶use constant LEFT_CURLY => q<{>;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 8 ▶use constant RIGHT_CURLY => q<}>;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 8 our @EXPORT_OK = qw{
28 column_number
29 is_ppi_quotelike_element
30 line_number
31 logical_filename
32 logical_line_number
33 statement
34 visual_column_number
35 __instance
36 __match_enclosed
37 __matching_delimiter
38 __normalize_interpolation_for_ppi
39 __variables
40 };
41  
42 8 our $VERSION = '0.024';
43  
44 # Readonly::Scalar my $BRACED_RE => __match_enclosed( LEFT_CURLY );
45 8 Readonly::Scalar my $BRACKETED_RE => __match_enclosed( '[' ); # ]
46 8 Readonly::Scalar my $PARENTHESIZED_RE => __match_enclosed( '(' ); # )
47  
48 8 Readonly::Scalar my $SIGIL_AND_CAST_RE => qr/ \$ \# \$* | [\@\$] \$* /smx;
49 # The following is an interpretation of perldata Identifier Parsing for
50 # Perls before 5.10.
51 8 Readonly::Scalar my $NORMAL_SYMBOL_NAME_RE => qr/
52 (?:
53 (?: :: )* '?
54 \w+ (?: (?: (?: :: )+ '? | (?: :: )* ' ) \w+ )*
55 (?: :: )* |
56 [[:punct:]]
57 )
58 /smx;
59  
60 8 Readonly::Scalar my $SYMBOL_NAME_RE => qr/
61 \^ \w+ | $NORMAL_SYMBOL_NAME_RE
62 /smxo;
63  
64 2 ▶sub column_number {
Subroutine column_number: called CC=2 SCAR=7.2
Pod documented
65 2 my ( $self ) = @_;
66 2 ▶ return ( $self->location() || [] )->[LOCATION_CHARACTER];
Condition50%1/2
$self->location || []
l!l
20
MC/DC0%0/1
$self->location || []
$self->location
Truth table50%1/2
A
A= $self->location
Aresult
11
00
67 }
68  
69 {
70  
71 8 my @relevant_ppi_classes = qw{
72 PPI::Token::Quote
73 PPI::Token::QuoteLike::Backtick
74 PPI::Token::QuoteLike::Command
75 PPI::Token::QuoteLike::Readline
76 PPI::Token::HereDoc
77 };
78  
79 9 ▶ sub is_ppi_quotelike_element {
Subroutine is_ppi_quotelike_element: called CC=5 SCAR=16.1
Pod documented
80 9 my ( $elem ) = @_;
81  
82 9 ▶ ref $elem
Branch: 1 branch
BranchTrueFalseTotal
ref $elem or return 8 1 9
83 or return;
84  
85 8 ▶ Scalar::Util::blessed( $elem )
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($elem) or return 7 1 8
86 or return;
87  
88 7 foreach my $class ( @relevant_ppi_classes ) {
89 25 ▶ $elem->isa( $class )
Branch: 1 branch
BranchTrueFalseTotal
$elem->isa($class) and return 1 5 20 25
90 and return 1;
91 }
92  
93 2 return;
94 }
95  
96 my $cast_allowed_for_bare_bracketed_variable = {
97 8 map { $_ => 1 } qw{ @ $ % } };
98  
99 66 ▶ sub __variables {
Subroutine __variables: called CC=43 SCAR=59.0
100 66 my ( $ppi ) = @_;
101  
102 # In case we need to manufacture any.
103 66 require PPIx::QuoteLike;
104  
105 66 ▶ Scalar::Util::blessed( $ppi )
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($ppi) or croak("Argument must be an object") 66 0 66
106 or croak 'Argument must be an object';
107  
108 # TODO the following two lines are a crock, but there does not
109 # seem to be a good alternative. Bad alternatives:
110 # * Introduce PPIx::QuoteLike::Element. But it seems stupid to
111 # introduce a class simply to mark these as members of the
112 # PPIx::QuoteLike family.
113 # If I go this way at all, PPIx::QuoteLike::Element should be
114 # analogous to PPIx::Regexp::Element in that it carries at
115 # least the navigational and Perl version methods.
116 # * Use DOES(). But that was not introduced until 5.10. So I
117 # could:
118 # - Depend on UNIVERSAL::DOES. This kindly steps aside if
119 # UNIVERSAL::DOES() exists, but it seems stupid to introduce
120 # a dependency that is only needed under really old Perls.
121 # - Same as above, only make the dependence conditional on the
122 # version of Perl. This may actually be the best
123 # alternative, but it's still pretty crufty.
124 66 ▶ $ppi->isa( 'PPIx::QuoteLike' )
Branch: 1 branch
BranchTrueFalseTotal
$ppi->isa("PPIx::QuoteLike") and return $ppi->variables 1 65 66
125 and return $ppi->variables();
126 65 ▶ $ppi->isa( 'PPIx::QuoteLike::Token' )
Branch: 1 branch
BranchTrueFalseTotal
$ppi->isa("PPIx::QuoteLike::Token") and return $ppi->variables 0 65 65
127 and return $ppi->variables();
128  
129 65 my %var;
130  
131 $ppi->isa( 'PPIx::Regexp::Element' )
132 65 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
$ppi->isa("PPIx::Regexp::Element") and do { foreach my $code (@{$ppi->find("PPIx::Regexp::Token::Code") or [];}) { foreach my $name (__variables($code->ppi)) { $var{$name} = 1; } ; } ; return keys %var } 0 65 65
133 0 ▶ foreach my $code ( @{ $ppi->find(
Condition0%0/2
$ppi->find('PPIx::Regexp::Token::Code') || []
l!l
00
MC/DC0%0/1
$ppi->find('PPIx::Regexp::Token::Code') || []
$ppi->find('PPIx::Regexp::Token::Code')
Truth table0%0/2
A
A= $ppi->find('PPIx::Regexp::Token::Code')
Aresult
11
00
134 'PPIx::Regexp::Token::Code' ) || [] } ) {
135 0 foreach my $name ( __variables( $code->ppi() ) ) {
136 0 $var{ $name } = 1;
137 }
138 }
139 0 return keys %var;
140 };
141  
142 65 ▶ $ppi->isa( 'PPI::Element' )
Branch: 1 branch
BranchTrueFalseTotal
$ppi->isa("PPI::Element") or croak("Argument must be a PPI::Element, ", "PPIx::Regexp::Element, PPIx::QuoteLike, or ", "PPIx::QuoteLike::Token") 65 0 65
143 or croak 'Argument must be a PPI::Element, ',
144 'PPIx::Regexp::Element, PPIx::QuoteLike, or ',
145 'PPIx::QuoteLike::Token';
146  
147 65 foreach my $sym ( _find( $ppi, 'PPI::Token::Symbol' ) ) {
148 # Eliminate rogue subscripts
149 57 ▶ _is_bareword_subscript( $sym )
Branch: 1 branch
BranchTrueFalseTotal
_is_bareword_subscript($sym) and next 0 57 57
150 and next;
151 57 ▶ if ( defined( my $name = _name_from_misparsed_magic( $sym ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $name = _name_from_misparsed_magic($sym))) { } 0 57 57
152 ) {
153 # We're $${name}, which is a dereference of $name
154 0 $var{$name} = 1;
155 } else {
156 # PPI got it right.
157 57 $var{ $sym->symbol() } = 1;
158 }
159 }
160  
161 # For some reason, PPI parses '$#foo' as a
162 # PPI::Token::ArrayIndex. $#$foo is parsed as a Cast followed
163 # by a Symbol, so as long as nobody decides the '$#' cast causes
164 # $elem->symbol() to return something other than '$foo', we're
165 # cool.
166 65 foreach my $elem ( _find( $ppi, 'PPI::Token::ArrayIndex' ) ) {
167 0 my $name = $elem->content();
168 0 ▶ $name =~ s/ \A \$ [#] /@/smx or next;
Branch: 1 branch
BranchTrueFalseTotal
$name =~ s/ \A \$ [#] /\@/msx or next 0 0 0
169 0 $var{$name} = 1;
170 }
171  
172 # Occasionally you see something like ${foo} outside quotes.
173 # This is legitimate, though PPI parses it as a cast followed by
174 # a block. On the assumption that there are fewer blocks than
175 # words in most Perl, we start at the top and work down. Perl
176 # also handles punctuation variables specified this way, but
177 # since PPI goes berserk when it sees this, we won't bother.
178 65 foreach my $elem ( _find( $ppi, 'PPI::Structure::Block' ) ) {
179  
180 7 ▶ my $previous = $elem->sprevious_sibling()
Branch: 1 branch
BranchTrueFalseTotal
my $previous = $elem->sprevious_sibling or next 7 0 7
181 or next;
182 7 ▶ $previous->isa( 'PPI::Token::Cast' )
Branch: 1 branch
BranchTrueFalseTotal
$previous->isa("PPI::Token::Cast") or next 7 0 7
183 or next;
184 7 my $sigil = $previous->content();
185 7 ▶ $cast_allowed_for_bare_bracketed_variable->{ $sigil }
Branch: 1 branch
BranchTrueFalseTotal
$cast_allowed_for_bare_bracketed_variable->{$sigil} or next 7 0 7
186 or next;
187  
188 7 ▶ if ( my @kids = $elem->schildren() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@kids) = $elem->schildren) { } 7 0 7
189 # The simple case: we parsed a block whose contents,
190 # however they were parsed, are the contents of the
191 # token.
192 7 ▶ 1 == @kids
Branch: 1 branch
BranchTrueFalseTotal
1 == @kids or next 7 0 7
193 or next;
194 7 ▶ $kids[0]->isa( 'PPI::Statement' )
Branch: 1 branch
BranchTrueFalseTotal
$kids[0]->isa("PPI::Statement") or next 7 0 7
195 or next;
196  
197 7 ▶ ( my $name = join '', map { $_->content() }
Branch: 1 branch
BranchTrueFalseTotal
(my $name = join("", map({$_->content;} $kids[0]->children))) =~ m( \A @{[qr/ [[:alpha:]_]\w* (?: :: [[:alpha:]_] \w* )* | \^ [A-Z_] | [0-9]+ | [-!"#\$%&'()+,.\/:;<=>?[\\\]^_`{|}~] /msx];} \z )mosx or next 2 5 7
198 2 $kids[0]->children() ) =~ m/ \A @{[ VARIABLE_RE ]} \z /smxo
199 or next;
200  
201 2 $var{ "$sigil$name" } = 1;
202 } else {
203 # The downright ugly case. We have something like ${]}
204 # where PPI can't find the terminator. To solve this we
205 # need to go blundering through the parse until we find
206 # the closing terminator.
207 0 ▶ my $stmt = $elem->statement()
Branch: 1 branch
BranchTrueFalseTotal
my $stmt = $elem->statement or next 0 0 0
208 or next;
209 0 ▶ if ( my $finish = $elem->finish() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $finish = $elem->finish) { } 0 0 0
210 # If we appear to have a terminated block, we may # {{
211 # have ${}}, which is the same as $}
212 0 ▶ my $next = $stmt->next_sibling()
Branch: 1 branch
BranchTrueFalseTotal
my $next = $stmt->next_sibling or next 0 0 0
213 or next;
214 0 ▶ $next->isa( 'PPI::Statement::UnmatchedBrace' )
Branch: 1 branch
BranchTrueFalseTotal
$next->isa("PPI::Statement::UnmatchedBrace") and "}" eq $next->content or next 0 0 0
Condition0%0/3
$next->isa("PPI::Statement::UnmatchedBrace") and "}" eq $next->content
!ll&&!rl&&r
000
MC/DC0%0/2
$next->isa("PPI::Statement::UnmatchedBrace") and "}" eq $next->content
$next->isa("PPI::Statement::UnmatchedBrace") "}" eq $next->content
Truth table0%0/3
A and B
A= $next->isa("PPI::Statement::UnmatchedBrace")B= "}" eq $next->content
ABresult
0X0
100
111
215 and RIGHT_CURLY eq $next->content()
216 or next;
217 0 $var{ $sigil . $finish->content() } = 1;
218 } else {
219 # Otherwise we have something like # [
220 # ${]}
221 0 ▶ my $next = $stmt->next_sibling()
Branch: 1 branch
BranchTrueFalseTotal
my $next = $stmt->next_sibling or next 0 0 0
222 or next;
223 0 ▶ my $finish = $next->next_sibling()
Branch: 1 branch
BranchTrueFalseTotal
my $finish = $next->next_sibling or next 0 0 0
224 or next;
225 0 ▶ $finish->isa( 'PPI::Statement::UnmatchedBrace' )
Branch: 1 branch
BranchTrueFalseTotal
$finish->isa("PPI::Statement::UnmatchedBrace") and "}" eq $finish->content or next 0 0 0
Condition0%0/3
$finish->isa("PPI::Statement::UnmatchedBrace") and "}" eq $finish->content
!ll&&!rl&&r
000
MC/DC0%0/2
$finish->isa("PPI::Statement::UnmatchedBrace") and "}" eq $finish->content
$finish->isa("PPI::Statement::UnmatchedBrace") "}" eq $finish->content
Truth table0%0/3
A and B
A= $finish->isa("PPI::Statement::UnmatchedBrace")B= "}" eq $finish->content
ABresult
0X0
100
111
226 and RIGHT_CURLY eq $finish->content()
227 or next;
228 0 $var{ $sigil . $next->content() } = 1;
229 }
230 }
231 }
232  
233 # Yes, we might have nested string literals, like
234 # "... @{[ qq<$foo> ]} ..."
235 65 foreach my $class ( @relevant_ppi_classes ) {
236 325 foreach my $elem ( _find( $ppi, $class ) ) {
237  
238 8 ▶ my $ql = PPIx::QuoteLike->new( $elem )
Branch: 1 branch
BranchTrueFalseTotal
my $ql = "PPIx::QuoteLike"->new($elem) or next 8 0 8
239 or next;
240 8 ▶ $ql->interpolates()
Branch: 1 branch
BranchTrueFalseTotal
$ql->interpolates or next 6 2 8
241 or next;
242 6 foreach my $sym ( $ql->variables() ) {
243 7 $var{ $sym } = 1;
244 }
245 }
246 }
247  
248 # By the same token we might have a regexp
249 # TODO for consistency's sake, give PPIx::Regexp a variables()
250 # method.
251 65 ▶ if ( HAVE_PPIX_REGEXP ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&HAVE_PPIX_REGEXP()) 0 65 65
252 0 foreach my $class ( qw{
253 PPI::Token::QuoteLike::Regexp
254 PPI::Token::Regexp::Match
255 PPI::Token::Regexp::Substitute
256 } ) {
257 0 foreach my $elem ( _find( $ppi, $class ) ) {
258 0 ▶ my $re = PPIx::Regexp->new( $elem )
Branch: 1 branch
BranchTrueFalseTotal
my $re = "PPIx::Regexp"->new($elem) or next 0 0 0
259 or next;
260 0 ▶ foreach my $code ( @{ $re->find(
Condition0%0/2
$re->find('PPIx::Regexp::Token::Code') || []
l!l
00
MC/DC0%0/1
$re->find('PPIx::Regexp::Token::Code') || []
$re->find('PPIx::Regexp::Token::Code')
Truth table0%0/2
A
A= $re->find('PPIx::Regexp::Token::Code')
Aresult
11
00
261 'PPIx::Regexp::Token::Code' ) || [] } ) {
262 0 foreach my $name ( __variables( $code->ppi() ) ) {
263 0 $var{ $name } = 1;
264 }
265 }
266 }
267 }
268 }
269  
270 65 return ( keys %var );
271 }
272 }
273  
274 # We want __variables to work when passed a single token. So we go
275 # through this to do what we wish PPI did -- return an array for a
276 # PPI::Node, or return either the element itself or nothing otherwise.
277 8 ▶sub _find {
Subroutine _find: called CC=4 SCAR=13.9
278 520 my ( $elem, $class ) = @_;
279 $elem->isa( 'PPI::Node' )
280 520 ▶ and return @{ $elem->find( $class ) || [] };
Branch: 1 branch
BranchTrueFalseTotal
$elem->isa("PPI::Node") and return @{$elem->find($class) or [];} 496 24 520
Condition100%2/2
$elem->find($class) || []
l!l
65431
MC/DC100%1/1
$elem->find($class) || []
$elem->find($class)
Truth table100%2/2
A
A= $elem->find($class)
Aresult
11
00
281 24 ▶ $elem->isa( $class )
Branch: 1 branch
BranchTrueFalseTotal
$elem->isa($class) and return $elem 2 22 24
282 and return $elem;
283 22 return;
284 }
285  
286 191 ▶sub __instance {
Subroutine __instance: called CC=2 SCAR=6.9
287 191 my ( $object, $class ) = @_;
288 191 ▶ Scalar::Util::blessed( $object ) or return;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($object) or return 144 47 191
289 144 return $object->isa( $class );
290 }
291  
292 # The problem this solves is that PPI can parse '{_}' as containing a
293 # PPI::Token::Magic (which is a PPI::Token::Symbol), not a
294 # PPI::Token::Word. This code also returns true for '${_}', which is not
295 # a subscript but has the same basic problem. The latter gets caught
296 # later.
297 57 ▶sub _is_bareword_subscript {
Subroutine _is_bareword_subscript: called CC=12 SCAR=48.2
298 57 my ( $elem ) = @_;
299 57 ▶ $elem->content() =~ m/ \A \w+ \z /smx
Branch: 1 branch
BranchTrueFalseTotal
$elem->content =~ / \A \w+ \z /msx or return 0 57 57
300 or return;
301 0 my $parent;
302 0 ▶ $parent = $elem->parent()
Branch: 1 branch
BranchTrueFalseTotal
$parent = $elem->parent and $parent->isa("PPI::Statement") and 1 == $parent->children or return 0 0 0
Condition0%0/6
$parent = $elem->parent and $parent->isa("PPI::Statement") and 1 == $parent->children
!ll&&!rl&&r
000
$parent = $elem->parent and $parent->isa("PPI::Statement")
!ll&&!rl&&r
000
MC/DC0%0/3
$parent = $elem->parent and $parent->isa("PPI::Statement") and 1 == $parent->children
$parent = $elem->parent $parent->isa("PPI::Statement") 1 == $parent->children
Truth table0%0/4
A and B and C
A= $parent = $elem->parentB= $parent->isa("PPI::Statement")C= 1 == $parent->children
ABCresult
0XX0
10X0
1100
1111
303 and $parent->isa( 'PPI::Statement' )
304 and 1 == $parent->children()
305 or return;
306 0 ▶ $parent = $parent->parent()
Branch: 1 branch
BranchTrueFalseTotal
$parent = $parent->parent and $parent->isa("PPI::Structure::Subscript") || $parent->isa("PPI::Structure::Block") and 1 == $parent->children or return 0 0 0
Condition0%0/9
$parent = $parent->parent and $parent->isa("PPI::Structure::Subscript") || $parent->isa("PPI::Structure::Block") and 1 == $parent->children
!ll&&!rl&&r
000
$parent = $parent->parent and $parent->isa("PPI::Structure::Subscript") || $parent->isa("PPI::Structure::Block")
!ll&&!rl&&r
000
$parent->isa("PPI::Structure::Subscript") or $parent->isa("PPI::Structure::Block")
l!l&&r!l&&!r
000
MC/DC0%0/4
$parent = $parent->parent and $parent->isa("PPI::Structure::Subscript") || $parent->isa("PPI::Structure::Block") and 1 == $parent->children
$parent = $parent->parent $parent->isa("PPI::Structure::Subscript") $parent->isa("PPI::Structure::Block") 1 == $parent->children
Truth table0%0/6
A and B or C and D
A= $parent = $parent->parentB= $parent->isa("PPI::Structure::Subscript")C= $parent->isa("PPI::Structure::Block")D= 1 == $parent->children
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
307 and ( $parent->isa( 'PPI::Structure::Subscript' )
308 or $parent->isa( 'PPI::Structure::Block' ) )
309 and 1 == $parent->children()
310 or return;
311 0 my $start;
312 0 ▶ $start = $parent->start()
Branch: 1 branch
BranchTrueFalseTotal
$start = $parent->start and $start->isa("PPI::Token::Structure") and "{" eq $start->content or return 0 0 0
Condition0%0/6
$start = $parent->start and $start->isa("PPI::Token::Structure") and "{" eq $start->content
!ll&&!rl&&r
000
$start = $parent->start and $start->isa("PPI::Token::Structure")
!ll&&!rl&&r
000
MC/DC0%0/3
$start = $parent->start and $start->isa("PPI::Token::Structure") and "{" eq $start->content
$start = $parent->start $start->isa("PPI::Token::Structure") "{" eq $start->content
Truth table0%0/4
A and B and C
A= $start = $parent->startB= $start->isa("PPI::Token::Structure")C= "{" eq $start->content
ABCresult
0XX0
10X0
1100
1111
313 and $start->isa( 'PPI::Token::Structure' )
314 and q<{> eq $start->content()
315 or return;
316 0 return 1;
317 }
318  
319 2 ▶sub line_number {
Subroutine line_number: called CC=2 SCAR=7.2
Pod documented
320 2 my ( $self ) = @_;
321 2 ▶ return ( $self->location() || [] )->[LOCATION_LINE];
Condition50%1/2
$self->location || []
l!l
20
MC/DC0%0/1
$self->location || []
$self->location
Truth table50%1/2
A
A= $self->location
Aresult
11
00
322 }
323  
324 2 ▶sub logical_filename {
Subroutine logical_filename: called CC=2 SCAR=7.2
Pod documented
325 2 my ( $self ) = @_;
326 2 ▶ return ( $self->location() || [] )->[LOCATION_LOGICAL_FILE];
Condition50%1/2
$self->location || []
l!l
20
MC/DC0%0/1
$self->location || []
$self->location
Truth table50%1/2
A
A= $self->location
Aresult
11
00
327 }
328  
329 2 ▶sub logical_line_number {
Subroutine logical_line_number: called CC=2 SCAR=7.2
Pod documented
330 2 my ( $self ) = @_;
331 2 ▶ return ( $self->location() || [] )->[LOCATION_LOGICAL_LINE];
Condition50%1/2
$self->location || []
l!l
20
MC/DC0%0/1
$self->location || []
$self->location
Truth table50%1/2
A
A= $self->location
Aresult
11
00
332 }
333  
334 {
335 8 our %REGEXP_CACHE;
336  
337 8 my %matching_bracket;
338  
339 8 ▶ BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
340 8 %matching_bracket = qw/ ( ) [ ] { } < > /;
341 }
342  
343 54 ▶ sub __match_enclosed {
Subroutine __match_enclosed: called CC=4 SCAR=13.9
344 54 my ( $left ) = @_;
345 54 my $ql = quotemeta $left;
346  
347 $REGEXP_CACHE{$ql}
348 54 ▶ and return $REGEXP_CACHE{$ql};
Branch: 1 branch
BranchTrueFalseTotal
$PPIx::QuoteLike::Utils::REGEXP_CACHE{$ql} and return $PPIx::QuoteLike::Utils::REGEXP_CACHE{$ql} 13 41 54
349  
350 41 ▶ if ( my $right = $matching_bracket{$left} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $right = $matching_bracket{$left}) { } 20 21 41
351  
352 # Based on Regexp::Common $RE{balanced} 2.113 (because I
353 # can't use (?-1)
354  
355 20 my $ql = quotemeta $left;
356 20 my $qr = quotemeta $right;
357 20 my $pkg = __PACKAGE__;
358 20 my $r = "(??{ \$${pkg}::REGEXP_CACHE{'$ql'} })";
359  
360 20 my @parts = (
361 "(?>[^\\\\$ql$qr]+)",
362 "(?>\\\$[$ql$qr])",
363 '(?>\\\\.)',
364 $r,
365 );
366  
367 {
368 8 ▶ use re qw{ eval };
Subroutine BEGIN: called CC=1 SCAR=0.0
369 20 local $" = '|';
370 20 $REGEXP_CACHE{$ql} = qr/($ql(?:@parts)*$qr)/sm;
371 }
372  
373 20 return $REGEXP_CACHE{$ql};
374  
375 } else {
376  
377 # Based on Regexp::Common $RE{delimited}{-delim=>'`'}
378 21 ▶ return ( $REGEXP_CACHE{$ql} ||=
Condition50%1/2
$PPIx::QuoteLike::Utils::REGEXP_CACHE{$ql} ||= qr/ ( (?: \Q$left\E ) (?: [^\\\Q$left\E]* (?: \\ . [^\\\Q$left\E]* )* ) (?: \Q$left\E ) ) /msx
l!l
021
MC/DC0%0/1
$PPIx::QuoteLike::Utils::REGEXP_CACHE{$ql} ||= qr/ ( (?: \Q$left\E ) (?: [^\\\Q$left\E]* (?: \\ . [^\\\Q$left\E]* )* ) (?: \Q$left\E ) ) /msx
$PPIx::QuoteLike::Utils::REGEXP_CACHE{$ql}
Truth table50%1/2
A
A= $PPIx::QuoteLike::Utils::REGEXP_CACHE{$ql}
Aresult
11
00
379 qr< (
380 (?: \Q$left\E )
381 (?: [^\\\Q$left\E]* (?: \\ . [^\\\Q$left\E]* )* )
382 (?: \Q$left\E )
383 ) >smx
384 );
385 }
386 }
387  
388 55 ▶ sub __matching_delimiter {
Subroutine __matching_delimiter: called CC=2 SCAR=6.9
389 55 my ( $left ) = @_;
390 55 ▶ my $right = $matching_bracket{$left}
Branch: 1 branch
BranchTrueFalseTotal
my $right = $matching_bracket{$left} or return $left 8 47 55
391 or return $left;
392 8 return $right;
393 }
394 }
395  
396 8 ▶sub __normalize_interpolation_for_ppi {
Subroutine __normalize_interpolation_for_ppi: called CC=5 SCAR=16.1
397 44 ( local $_ ) = @_;
398  
399 # "@{[ foo() ]}" => 'foo()'
400 44 ▶ if ( m/ \A \@ [{] \s* ( $BRACKETED_RE ) \s* [}] \z /smxo ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/ \A \@ [{] \s* ( $BRACKETED_RE ) \s* [}] \z /mosx) 3 41 44
401 3 $_ = $1;
402 3 s/ \A [[] \s* //smx;
403 3 s/ \s* []] \z //smx;
404 3 return "$_";
405 }
406  
407 # "${\( foo() )}" => 'foo()'
408 41 ▶ if ( m/ \A \$ [{] \s* \\ \s* ( $PARENTHESIZED_RE ) \s* [}] \z /smox ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/ \A \$ [{] \s* \\ \s* ( $PARENTHESIZED_RE ) \s* [}] \z /mosx) 1 40 41
409 1 $_ = $1;
410 1 s/ \A [(] \s* //smx;
411 1 s/ \s* [)] \z //smx;
412 1 return "$_";
413 }
414  
415 # "${foo}" => '$foo'
416 m/ \A ( $SIGIL_AND_CAST_RE ) \s*
417 [{] \s* ( $NORMAL_SYMBOL_NAME_RE ) \s* [}] \z /smxo
418 40 ▶ and return "$1$2";
Branch: 1 branch
BranchTrueFalseTotal
/ \A ( $SIGIL_AND_CAST_RE ) \s* [{] \s* ( $NORMAL_SYMBOL_NAME_RE ) \s* [}] \z /mosx and return "$1$2" 8 32 40
419  
420 # "${foo{bar}}" => '$foo{bar}'
421 # NOTE that this is a warning, and so not done.
422 # if ( m/ \A ( $SIGIL_AND_CAST_RE ) (?= [{] ) ( $BRACED_RE ) /smx ) {
423 # ( my $sigil, local $_ ) = ( $1, $2 );
424 # s/ \A [{] \s* //smx;
425 # s/ \s* [}] \z //smx;
426 # return "$sigil$_";
427 # }
428  
429 # "$ foo->{bar}" => '$foo->{bar}'
430 32 ▶ if ( m/ \A ( $SIGIL_AND_CAST_RE ) \s+ ( $SYMBOL_NAME_RE ) ( .* ) /smxo ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/ \A ( $SIGIL_AND_CAST_RE ) \s+ ( $SYMBOL_NAME_RE ) ( .* ) /mosx) 1 31 32
431 1 return "$1$2$3";
432 }
433  
434 # Everything else
435 31 return "$_";
436 }
437  
438 3 ▶sub statement {
Subroutine statement: called CC=5 SCAR=17.2
Pod documented
439 3 my ( $self ) = @_;
440 3 ▶ my $top = $self->top()
Branch: 1 branch
BranchTrueFalseTotal
my $top = $self->top or return 3 0 3
441 or return;
442 3 ▶ $top->can( 'source' )
Branch: 1 branch
BranchTrueFalseTotal
$top->can("source") or return 3 0 3
443 or return;
444 3 ▶ my $source = $top->source()
Branch: 1 branch
BranchTrueFalseTotal
my $source = $top->source or return 3 0 3
445 or return;
446 3 ▶ $source->can( 'statement' )
Branch: 1 branch
BranchTrueFalseTotal
$source->can("statement") or return 3 0 3
447 or return;
448 3 return $source->statement();
449 }
450  
451 2 ▶sub visual_column_number {
Subroutine visual_column_number: called CC=2 SCAR=7.2
Pod documented
452 2 my ( $self ) = @_;
453 2 ▶ return ( $self->location() || [] )->[LOCATION_COLUMN];
Condition50%1/2
$self->location || []
l!l
20
MC/DC0%0/1
$self->location || []
$self->location
Truth table50%1/2
A
A= $self->location
Aresult
11
00
454 }
455  
456 # This handles two known cases where PPI misparses bracketed variable
457 # names.
458 # * $${foo} is parsed as '$$' when it is really a dereference of $foo.
459 # The argument is the '$$'
460 # * ${$} is parsed as an unterminated block followed by '$}'. The
461 # argument is the '$}'.
462  
463 {
464 my $special = {
465 2 ▶ '$$' => sub { # $${foo},$${$_[0]}
Subroutine __ANON__: called CC=8 SCAR=37.7
466 2 my ( $elem ) = @_;
467 2 my $next;
468 2 ▶ $next = $elem->snext_sibling()
Branch: 1 branch
BranchTrueFalseTotal
$next = $elem->snext_sibling and $next->isa("PPI::Structure::Subscript") or return 0 2 2
Condition33%1/3
$next = $elem->snext_sibling and $next->isa("PPI::Structure::Subscript")
!ll&&!rl&&r
200
MC/DC0%0/2
$next = $elem->snext_sibling and $next->isa("PPI::Structure::Subscript")
$next = $elem->snext_sibling $next->isa("PPI::Structure::Subscript")
Truth table33%1/3
A and B
A= $next = $elem->snext_siblingB= $next->isa("PPI::Structure::Subscript")
ABresult
0X0
100
111
469 and $next->isa( 'PPI::Structure::Subscript' )
470 or return;
471 0 my $start;
472 0 ▶ $start = $next->start()
Branch: 1 branch
BranchTrueFalseTotal
$start = $next->start and "{" eq $start->content or return 0 0 0
Condition0%0/3
$start = $next->start and "{" eq $start->content
!ll&&!rl&&r
000
MC/DC0%0/2
$start = $next->start and "{" eq $start->content
$start = $next->start "{" eq $start->content
Truth table0%0/3
A and B
A= $start = $next->startB= "{" eq $start->content
ABresult
0X0
100
111
473 and LEFT_CURLY eq $start->content()
474 or return;
475 0 my @kids = $next->schildren();
476 0 ▶ 1 == @kids
Branch: 1 branch
BranchTrueFalseTotal
1 == @kids and $kids[0]->isa("PPI::Statement") and @kids = $kids[0]->schildren 0 0 0
Condition0%0/3
1 == @kids and $kids[0]->isa("PPI::Statement")
!ll&&!rl&&r
000
MC/DC0%0/2
1 == @kids and $kids[0]->isa("PPI::Statement")
1 == @kids $kids[0]->isa("PPI::Statement")
Truth table0%0/3
A and B
A= 1 == @kidsB= $kids[0]->isa("PPI::Statement")
ABresult
0X0
100
111
477 and $kids[0]->isa( 'PPI::Statement' )
478 and @kids = $kids[0]->schildren();
479 0 ▶ if ( 1 == @kids ) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 == @kids) { } 0 0 0
480 # The $${foo} case
481 0 return join '', '$', map { $_->content() } @kids;
482 } else {
483 # The $${$_[0]} case. In this case the curly brackets
484 # are really a block, as
485 # $ perl -MO=Deparse -e '$${$_[0]}' makes clear. So we
486 # just return the '$$', since the '$_' will turn up in
487 # the course of things.
488 0 return $elem->content();
489 }
490 },
491 # {
492 0 ▶ '$}' => sub { # ${$}
Subroutine __ANON__: not called CC=8 SCAR=42.8
493 0 my ( $elem ) = @_;
494 0 my $stmt;
495 0 ▶ $stmt = $elem->parent()
Branch: 1 branch
BranchTrueFalseTotal
$stmt = $elem->parent and $stmt->isa("PPI::Statement") or return 0 0 0
Condition0%0/3
$stmt = $elem->parent and $stmt->isa("PPI::Statement")
!ll&&!rl&&r
000
MC/DC0%0/2
$stmt = $elem->parent and $stmt->isa("PPI::Statement")
$stmt = $elem->parent $stmt->isa("PPI::Statement")
Truth table0%0/3
A and B
A= $stmt = $elem->parentB= $stmt->isa("PPI::Statement")
ABresult
0X0
100
111
496 and $stmt->isa( 'PPI::Statement' )
497 or return;
498 0 my $block;
499 0 ▶ $block = $stmt->parent()
Branch: 1 branch
BranchTrueFalseTotal
$block = $stmt->parent and $block->isa("PPI::Structure::Block") and not $block->finish or return 0 0 0
Condition0%0/6
$block = $stmt->parent and $block->isa("PPI::Structure::Block") and not $block->finish
!ll&&!rl&&r
000
$block = $stmt->parent and $block->isa("PPI::Structure::Block")
!ll&&!rl&&r
000
MC/DC0%0/3
$block = $stmt->parent and $block->isa("PPI::Structure::Block") and not $block->finish
$block = $stmt->parent $block->isa("PPI::Structure::Block") not $block->finish
Truth table0%0/4
A and B and not(C)
A= $block = $stmt->parentB= $block->isa("PPI::Structure::Block")C= not $block->finish
ABCresult
0XX0
10X0
1100
1111
500 and $block->isa( 'PPI::Structure::Block' )
501 and not $block->finish()
502 or return;
503 0 my $sigil;
504 0 ▶ $sigil = $block->sprevious_sibling()
Branch: 1 branch
BranchTrueFalseTotal
$sigil = $block->sprevious_sibling and $sigil->isa("PPI::Token::Cast") or return 0 0 0
Condition0%0/3
$sigil = $block->sprevious_sibling and $sigil->isa("PPI::Token::Cast")
!ll&&!rl&&r
000
MC/DC0%0/2
$sigil = $block->sprevious_sibling and $sigil->isa("PPI::Token::Cast")
$sigil = $block->sprevious_sibling $sigil->isa("PPI::Token::Cast")
Truth table0%0/3
A and B
A= $sigil = $block->sprevious_siblingB= $sigil->isa("PPI::Token::Cast")
ABresult
0X0
100
111
505 and $sigil->isa( 'PPI::Token::Cast' )
506 or return;
507 0 my $name = join '', map { $_->content() } $sigil,
508 $stmt->children();
509 0 chop $name;
510 0 return $name;
511 },
512 8 };
513  
514 57 ▶ sub _name_from_misparsed_magic {
Subroutine _name_from_misparsed_magic: called CC=3 SCAR=11.0
515 57 my ( $elem ) = @_;
516 57 ▶ $elem->isa( 'PPI::Token::Magic' )
Branch: 1 branch
BranchTrueFalseTotal
$elem->isa("PPI::Token::Magic") or return 8 49 57
517 or return;
518 8 ▶ my $code = $special->{ $elem->content() }
Branch: 1 branch
BranchTrueFalseTotal
my $code = $$special{$elem->content} or return 2 6 8
519 or return;
520 2 return $code->( $elem );
521 }
522 }
523  
524 8 1;
525  
526 __END__
↑ Summary QuoteLike/Dumper.pm »