PPI/Element.pm

filter: Statementstmt 89.3% 176 / 197 Branchbran 69.0% 76 / 110 Conditioncond 60.0% 9 / 15 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 91.3% 53 / 58 Podpod 100% 26 / 26 totaltotal 82.6% 344 / 416 CC 62 SCAR 14.9
CC
62
Cov
81%
_flush_locations
19.0
statement
12.0
replace
12.0
CRAP 4.4
« PPI/Cache.pm ↑ Summary PPI/Token/Structure.pm »
1 package PPI::Element;
2  
3 =pod
4  
5 =head1 NAME
6  
7 PPI::Element - The abstract Element class, a base for all source objects
8  
9 =head1 INHERITANCE
10  
11 PPI::Element is the root of the PDOM tree
12  
13 =head1 DESCRIPTION
14  
15 The abstract C<PPI::Element> serves as a base class for all source-related
16 objects, from a single whitespace token to an entire document. It provides
17 a basic set of methods to provide a common interface and basic
18 implementations.
19  
20 =head1 METHODS
21  
22 =cut
23  
24 63 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 63 ▶use Clone 0.30 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
26 63 ▶use Scalar::Util qw{refaddr};
Subroutine BEGIN: called CC=1 SCAR=0.0
27 63 ▶use Params::Util qw{_INSTANCE _ARRAY};
Subroutine BEGIN: called CC=1 SCAR=0.0
28 63 ▶use List::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29 63 ▶use PPI::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
30 63 ▶use PPI::Node ();
Subroutine BEGIN: called CC=1 SCAR=0.0
31 63 ▶use PPI::Singletons '%_PARENT';
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 63 our $VERSION = '1.260'; # VERSION
34  
35 63 our $errstr = "";
36  
37 63 ▶use overload 'bool' => \&PPI::Util::TRUE;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 63 ▶use overload '""' => 'content';
Subroutine BEGIN: called CC=1 SCAR=0.0
39 63 ▶use overload '==' => '__equals';
Subroutine BEGIN: called CC=1 SCAR=0.0
40 63 ▶use overload '!=' => '__nequals';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 63 ▶use overload 'eq' => '__eq';
Subroutine BEGIN: called CC=1 SCAR=0.0
42 63 ▶use overload 'ne' => '__ne';
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44  
45  
46  
47  
48 #####################################################################
49 # General Properties
50  
51 =pod
52  
53 =head2 significant
54  
55 Because we treat whitespace and other non-code items as Tokens (in order to
56 be able to "round trip" the L<PPI::Document> back to a file) the
57 C<significant> method allows us to distinguish between tokens that form a
58 part of the code, and tokens that aren't significant, such as whitespace,
59 POD, or the portion of a file after (and including) the C<__END__> token.
60  
61 Returns true if the Element is significant, or false it not.
62  
63 =cut
64  
65 ### XS -> PPI/XS.xs:_PPI_Element__significant 0.845+
66 sub significant() { 1 }
67  
68 =pod
69  
70 =head2 class
71  
72 The C<class> method is provided as a convenience, and really does nothing
73 more than returning C<ref($self)>. However, some people have found that
74 they appreciate the laziness of C<$Foo-E<gt>class eq 'whatever'>, so I
75 have caved to popular demand and included it.
76  
77 Returns the class of the Element as a string
78  
79 =cut
80  
81 0 ▶sub class { ref($_[0]) }
Subroutine class: not called CC=1 SCAR=6.9
Pod documented
82  
83 =pod
84  
85 =head2 tokens
86  
87 The C<tokens> method returns a list of L<PPI::Token> objects for the
88 Element, essentially getting back that part of the document as if it had
89 not been lexed.
90  
91 This also means there are no Statements and no Structures in the list,
92 just the Token classes.
93  
94 =cut
95  
96 274811 ▶sub tokens { $_[0] }
Subroutine tokens: called CC=1 SCAR=0.0
Pod documented
97  
98 =pod
99  
100 =head2 content
101  
102 For B<any> C<PPI::Element>, the C<content> method will reconstitute the
103 base code for it as a single string. This method is also the method used
104 for overloading stringification. When an Element is used in a double-quoted
105 string for example, this is the method that is called.
106  
107 B<WARNING:>
108  
109 You should be aware that because of the way that here-docs are handled, any
110 here-doc content is not included in C<content>, and as such you should
111 B<not> eval or execute the result if it contains any L<PPI::Token::HereDoc>.
112  
113 The L<PPI::Document> method C<serialize> should be used to stringify a PDOM
114 document into something that can be executed as expected.
115  
116 Returns the basic code as a string (excluding here-doc content).
117  
118 =cut
119  
120 ### XS -> PPI/XS.xs:_PPI_Element__content 0.900+
121 sub content() { '' }
122  
123  
124  
125  
126  
127 #####################################################################
128 # Navigation Methods
129  
130 =pod
131  
132 =head2 parent
133  
134 Elements themselves are not intended to contain other Elements, that is
135 left to the L<PPI::Node> abstract class, a subclass of C<PPI::Element>.
136 However, all Elements can be contained B<within> a parent Node.
137  
138 If an Element is within a parent Node, the C<parent> method returns the
139 Node.
140  
141 =cut
142  
143 2450 ▶sub parent { $_PARENT{refaddr $_[0]} }
Subroutine parent: called CC=1 SCAR=0.0
Pod documented
144  
145 =pod
146  
147 =head2 descendant_of $element
148  
149 Answers whether a C<PPI::Element> is contained within another one.
150  
151 C<PPI::Element>s are considered to be descendants of themselves.
152  
153 =cut
154  
155 sub descendant_of {
156 6 ▶ my $cursor = shift;
Subroutine descendant_of: called CC=4 SCAR=13.9
Pod documented
157 6 ▶ my $parent = shift or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $parent = shift() or return undef 0 6 6
158 6 ▶ while ( refaddr $cursor != refaddr $parent ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::refaddr($cursor) != builtin::refaddr($parent)) 17 3 20
159 17 ▶ $cursor = $_PARENT{refaddr $cursor} or return '';
Branch: 1 branch
BranchTrueFalseTotal
$cursor = $PPI::Element::_PARENT{builtin::refaddr($cursor)} or return "" 3 14 17
160 }
161 3 return 1;
162 }
163  
164 =pod
165  
166 =head2 ancestor_of $element
167  
168 Answers whether a C<PPI::Element> is contains another one.
169  
170 C<PPI::Element>s are considered to be ancestors of themselves.
171  
172 =cut
173  
174 sub ancestor_of {
175 6 ▶ my $self = shift;
Subroutine ancestor_of: called CC=4 SCAR=13.9
Pod documented
176 6 ▶ my $cursor = shift or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $cursor = shift() or return undef 0 6 6
177 6 ▶ while ( refaddr $cursor != refaddr $self ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::refaddr($cursor) != builtin::refaddr($self)) 17 3 20
178 17 ▶ $cursor = $_PARENT{refaddr $cursor} or return '';
Branch: 1 branch
BranchTrueFalseTotal
$cursor = $PPI::Element::_PARENT{builtin::refaddr($cursor)} or return "" 3 14 17
179 }
180 3 return 1;
181 }
182  
183 =pod
184  
185 =head2 statement
186  
187 For a C<PPI::Element> that is contained (at some depth) within a
188 L<PPI::Statement>, the C<statement> method will return the first parent
189 Statement object lexically 'above' the Element.
190  
191 Returns a L<PPI::Statement> object, which may be the same Element if the
192 Element is itself a L<PPI::Statement> object.
193  
194 Returns false if the Element is not within a Statement and is not itself
195 a Statement.
196  
197 =cut
198  
199 sub statement {
200 0 ▶ my $cursor = shift;
Subroutine statement: not called CC=3 SCAR=24.8
Pod documented
201 0 ▶ while ( ! _INSTANCE($cursor, 'PPI::Statement') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&_INSTANCE($cursor, "PPI::Statement")) 0 0 0
202 0 ▶ $cursor = $_PARENT{refaddr $cursor} or return '';
Branch: 1 branch
BranchTrueFalseTotal
$cursor = $PPI::Element::_PARENT{builtin::refaddr($cursor)} or return "" 0 0 0
203 }
204 0 $cursor;
205 }
206  
207 =pod
208  
209 =head2 top
210  
211 For a C<PPI::Element> that is contained within a PDOM tree, the C<top> method
212 will return the top-level Node in the tree. Most of the time this should be
213 a L<PPI::Document> object, however this will not always be so. For example,
214 if a subroutine has been removed from its Document, to be moved to another
215 Document.
216  
217 Returns the top-most PDOM object, which may be the same Element, if it is
218 not within any parent PDOM object.
219  
220 =cut
221  
222 sub top {
223 189 ▶ my $cursor = shift;
Subroutine top: called CC=2 SCAR=6.9
Pod documented
224 189 ▶ while ( my $parent = $_PARENT{refaddr $cursor} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $parent = $PPI::Element::_PARENT{builtin::refaddr($cursor)}) 384 189 573
225 384 $cursor = $parent;
226 }
227 189 $cursor;
228 }
229  
230 =pod
231  
232 =head2 document
233  
234 For an Element that is contained within a L<PPI::Document> object,
235 the C<document> method will return the top-level Document for the Element.
236  
237 Returns the L<PPI::Document> for this Element, or false if the Element is not
238 contained within a Document.
239  
240 =cut
241  
242 sub document {
243 177 ▶ my $top = shift->top;
Subroutine document: called CC=2 SCAR=7.2
Pod documented
244 177 ▶ _INSTANCE($top, 'PPI::Document') and $top;
Branch: 1 branch
BranchTrueFalseTotal
&_INSTANCE($top, "PPI::Document") and $top 177 0 177
245 }
246  
247 =pod
248  
249 =head2 next_sibling
250  
251 All L<PPI::Node> objects (specifically, our parent Node) contain a number of
252 C<PPI::Element> objects. The C<next_sibling> method returns the C<PPI::Element>
253 immediately after the current one, or false if there is no next sibling.
254  
255 =cut
256  
257 sub next_sibling {
258 83 ▶ my $self = shift;
Subroutine next_sibling: called CC=3 SCAR=11.0
Pod documented
259 83 ▶ my $parent = $_PARENT{refaddr $self} or return '';
Branch: 1 branch
BranchTrueFalseTotal
my $parent = $PPI::Element::_PARENT{builtin::refaddr($self)} or return "" 5 78 83
260 78 my $key = refaddr $self;
261 78 my $elements = $parent->{children};
262 my $position = List::Util::first {
263 270 ▶ refaddr $elements->[$_] == $key
Subroutine __ANON__: called CC=1 SCAR=0.0
264 78 } 0..$#$elements;
265 78 ▶ $elements->[$position + 1] || '';
Branch: 1 branch
BranchTrueFalseTotal
$$elements[$position + 1] or "" 23 55 78
266 }
267  
268 =pod
269  
270 =head2 snext_sibling
271  
272 As per the other 's' methods, the C<snext_sibling> method returns the next
273 B<significant> sibling of the C<PPI::Element> object.
274  
275 Returns a C<PPI::Element> object, or false if there is no 'next' significant
276 sibling.
277  
278 =cut
279  
280 sub snext_sibling {
281 2208 ▶ my $self = shift;
Subroutine snext_sibling: called CC=4 SCAR=13.9
Pod documented
282 2208 ▶ my $parent = $_PARENT{refaddr $self} or return '';
Branch: 1 branch
BranchTrueFalseTotal
my $parent = $PPI::Element::_PARENT{builtin::refaddr($self)} or return "" 7 2201 2208
283 2201 my $key = refaddr $self;
284 2201 my $elements = $parent->{children};
285 my $position = List::Util::first {
286 11877 ▶ refaddr $elements->[$_] == $key
Subroutine __ANON__: called CC=1 SCAR=0.0
287 2201 } 0..$#$elements;
288 2201 ▶ while ( defined(my $it = $elements->[++$position]) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $it = $$elements[++$position])) 2184 25 2209
289 2184 ▶ return $it if $it->significant;
Branch: 1 branch
BranchTrueFalseTotal
if $it->significant 2176 8 2184
290 }
291 25 '';
292 }
293  
294 =pod
295  
296 =head2 previous_sibling
297  
298 All L<PPI::Node> objects (specifically, our parent Node) contain a number of
299 C<PPI::Element> objects. The C<previous_sibling> method returns the Element
300 immediately before the current one, or false if there is no 'previous'
301 C<PPI::Element> object.
302  
303 =cut
304  
305 sub previous_sibling {
306 76 ▶ my $self = shift;
Subroutine previous_sibling: called CC=4 SCAR=13.9
Pod documented
307 76 ▶ my $parent = $_PARENT{refaddr $self} or return '';
Branch: 1 branch
BranchTrueFalseTotal
my $parent = $PPI::Element::_PARENT{builtin::refaddr($self)} or return "" 4 72 76
308 72 my $key = refaddr $self;
309 72 my $elements = $parent->{children};
310 my $position = List::Util::first {
311 248 ▶ refaddr $elements->[$_] == $key
Subroutine __ANON__: called CC=1 SCAR=0.0
312 72 } 0..$#$elements;
313 72 ▶ $position and $elements->[$position - 1] or '';
Branch: 1 branch
BranchTrueFalseTotal
$position and $$elements[$position - 1] or "" 18 54 72
Condition66%2/3
$position and $$elements[$position - 1]
!ll&&!rl&&r
18054
MC/DC50%1/2
$position and $$elements[$position - 1]
$position $$elements[$position - 1]
Truth table66%2/3
A and B
A= $positionB= $$elements[$position - 1]
ABresult
0X0
100
111
314 }
315  
316 =pod
317  
318 =head2 sprevious_sibling
319  
320 As per the other 's' methods, the C<sprevious_sibling> method returns
321 the previous B<significant> sibling of the C<PPI::Element> object.
322  
323 Returns a C<PPI::Element> object, or false if there is no 'previous' significant
324 sibling.
325  
326 =cut
327  
328 sub sprevious_sibling {
329 1489 ▶ my $self = shift;
Subroutine sprevious_sibling: called CC=5 SCAR=16.1
Pod documented
330 1489 ▶ my $parent = $_PARENT{refaddr $self} or return '';
Branch: 1 branch
BranchTrueFalseTotal
my $parent = $PPI::Element::_PARENT{builtin::refaddr($self)} or return "" 1 1488 1489
331 1488 my $key = refaddr $self;
332 1488 my $elements = $parent->{children};
333 my $position = List::Util::first {
334 9650 ▶ refaddr $elements->[$_] == $key
Subroutine __ANON__: called CC=1 SCAR=0.0
335 1488 } 0..$#$elements;
336 1488 ▶ while ( $position-- and defined(my $it = $elements->[$position]) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($position-- and defined(my $it = $elements->[$position])) 1481 21 1502
Condition66%2/3
$position-- and defined(my $it = $elements->[$position])
!ll&&!rl&&r
2101481
MC/DC50%1/2
$position-- and defined(my $it = $elements->[$position])
$position-- defined(my $it = $elements->[$position])
Truth table66%2/3
A and B
A= $position--B= defined(my $it = $elements->[$position])
ABresult
0X0
100
111
337 1481 ▶ return $it if $it->significant;
Branch: 1 branch
BranchTrueFalseTotal
if $it->significant 1467 14 1481
338 }
339 21 '';
340 }
341  
342 =pod
343  
344 =head2 first_token
345  
346 As a support method for higher-order algorithms that deal specifically with
347 tokens and actual Perl content, the C<first_token> method finds the first
348 PPI::Token object within or equal to this one.
349  
350 That is, if called on a L<PPI::Node> subclass, it will descend until it
351 finds a L<PPI::Token>. If called on a L<PPI::Token> object, it will return
352 the same object.
353  
354 Returns a L<PPI::Token> object, or dies on error (which should be extremely
355 rare and only occur if an illegal empty L<PPI::Statement> exists below the
356 current Element somewhere.)
357  
358 =cut
359  
360 sub first_token {
361 15 ▶ my $cursor = shift;
Subroutine first_token: called CC=3 SCAR=11.0
Pod documented
362 15 ▶ while ( $cursor->isa('PPI::Node') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cursor->isa("PPI::Node")) 18 15 33
363 18 ▶ $cursor = $cursor->first_element
Branch: 1 branch
BranchTrueFalseTotal
$cursor = $cursor->first_element or die "Found empty PPI::Node while getting first token" 0 18 18
364 or die "Found empty PPI::Node while getting first token";
365 }
366 15 $cursor;
367 }
368  
369  
370 =pod
371  
372 =head2 last_token
373  
374 As a support method for higher-order algorithms that deal specifically with
375 tokens and actual Perl content, the C<last_token> method finds the last
376 PPI::Token object within or equal to this one.
377  
378 That is, if called on a L<PPI::Node> subclass, it will descend until it
379 finds a L<PPI::Token>. If called on a L<PPI::Token> object, it will return
380 the itself.
381  
382 Returns a L<PPI::Token> object, or dies on error (which should be extremely
383 rare and only occur if an illegal empty L<PPI::Statement> exists below the
384 current Element somewhere.)
385  
386 =cut
387  
388 sub last_token {
389 14 ▶ my $cursor = shift;
Subroutine last_token: called CC=3 SCAR=11.0
Pod documented
390 14 ▶ while ( $cursor->isa('PPI::Node') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cursor->isa("PPI::Node")) 18 14 32
391 18 ▶ $cursor = $cursor->last_element
Branch: 1 branch
BranchTrueFalseTotal
$cursor = $cursor->last_element or die "Found empty PPI::Node while getting first token" 0 18 18
392 or die "Found empty PPI::Node while getting first token";
393 }
394 14 $cursor;
395 }
396  
397 =pod
398  
399 =head2 next_token
400  
401 As a support method for higher-order algorithms that deal specifically with
402 tokens and actual Perl content, the C<next_token> method finds the
403 L<PPI::Token> object that is immediately after the current Element, even if
404 it is not within the same parent L<PPI::Node> as the one for which the
405 method is being called.
406  
407 Note that this is B<not> defined as a L<PPI::Token>-specific method,
408 because it can be useful to find the next token that is after, say, a
409 L<PPI::Statement>, although obviously it would be useless to want the
410 next token after a L<PPI::Document>.
411  
412 Returns a L<PPI::Token> object, or false if there are no more tokens after
413 the Element.
414  
415 =cut
416  
417 sub next_token {
418 61 ▶ my $cursor = shift;
Subroutine next_token: called CC=6 SCAR=17.9
Pod documented
419  
420 # Find the next element, going upwards as needed
421 61 while ( 1 ) {
422 74 my $element = $cursor->next_sibling;
423 74 ▶ if ( $element ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($element) 52 22 74
424 52 ▶ return $element if $element->isa('PPI::Token');
Branch: 1 branch
BranchTrueFalseTotal
if $element->isa("PPI::Token") 42 10 52
425 10 return $element->first_token;
426 }
427 22 ▶ $cursor = $cursor->parent or return '';
Branch: 1 branch
BranchTrueFalseTotal
$cursor = $cursor->parent or return "" 4 18 22
428 18 ▶ if ( $cursor->isa('PPI::Structure') and $cursor->finish ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cursor->isa("PPI::Structure") and $cursor->finish) 5 13 18
Condition66%2/3
$cursor->isa("PPI::Structure") and $cursor->finish
!ll&&!rl&&r
1305
MC/DC50%1/2
$cursor->isa("PPI::Structure") and $cursor->finish
$cursor->isa("PPI::Structure") $cursor->finish
Truth table66%2/3
A and B
A= $cursor->isa("PPI::Structure")B= $cursor->finish
ABresult
0X0
100
111
429 5 return $cursor->finish;
430 }
431 }
432 }
433  
434 =pod
435  
436 =head2 previous_token
437  
438 As a support method for higher-order algorithms that deal specifically with
439 tokens and actual Perl content, the C<previous_token> method finds the
440 L<PPI::Token> object that is immediately before the current Element, even
441 if it is not within the same parent L<PPI::Node> as this one.
442  
443 Note that this is not defined as a L<PPI::Token>-only method, because it can
444 be useful to find the token is before, say, a L<PPI::Statement>, although
445 obviously it would be useless to want the next token before a
446 L<PPI::Document>.
447  
448 Returns a L<PPI::Token> object, or false if there are no more tokens before
449 the C<Element>.
450  
451 =cut
452  
453 sub previous_token {
454 59 ▶ my $cursor = shift;
Subroutine previous_token: called CC=6 SCAR=17.9
Pod documented
455  
456 # Find the previous element, going upwards as needed
457 59 while ( 1 ) {
458 70 my $element = $cursor->previous_sibling;
459 70 ▶ if ( $element ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($element) 51 19 70
460 51 ▶ return $element if $element->isa('PPI::Token');
Branch: 1 branch
BranchTrueFalseTotal
if $element->isa("PPI::Token") 42 9 51
461 9 return $element->last_token;
462 }
463 19 ▶ $cursor = $cursor->parent or return '';
Branch: 1 branch
BranchTrueFalseTotal
$cursor = $cursor->parent or return "" 3 16 19
464 16 ▶ if ( $cursor->isa('PPI::Structure') and $cursor->start ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cursor->isa("PPI::Structure") and $cursor->start) 5 11 16
Condition66%2/3
$cursor->isa("PPI::Structure") and $cursor->start
!ll&&!rl&&r
1105
MC/DC50%1/2
$cursor->isa("PPI::Structure") and $cursor->start
$cursor->isa("PPI::Structure") $cursor->start
Truth table66%2/3
A and B
A= $cursor->isa("PPI::Structure")B= $cursor->start
ABresult
0X0
100
111
465 5 return $cursor->start;
466 }
467 }
468 }
469  
470  
471  
472  
473  
474 #####################################################################
475 # Manipulation
476  
477 =pod
478  
479 =head2 clone
480  
481 As per the L<Clone> module, the C<clone> method makes a perfect copy of
482 an Element object. In the generic case, the implementation is done using
483 the L<Clone> module's mechanism itself. In higher-order cases, such as for
484 Nodes, there is more work involved to keep the parent-child links intact.
485  
486 =cut
487  
488 sub clone {
489 6 ▶ Clone::clone(shift);
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
490 }
491  
492 =pod
493  
494 =head2 insert_before @Elements
495  
496 The C<insert_before> method allows you to insert lexical perl content, in
497 the form of C<PPI::Element> objects, before the calling C<Element>. You
498 need to be very careful when modifying perl code, as it's easy to break
499 things.
500  
501 In its initial incarnation, this method allows you to insert a single
502 Element, and will perform some basic checking to prevent you inserting
503 something that would be structurally wrong (in PDOM terms).
504  
505 In future, this method may be enhanced to allow the insertion of multiple
506 Elements, inline-parsed code strings or L<PPI::Document::Fragment> objects.
507  
508 Returns true if the Element was inserted, false if it can not be inserted,
509 or C<undef> if you do not provide a C<PPI::Element> object as a parameter.
510  
511 =cut
512  
513 sub __insert_before {
514 2 ▶ my $self = shift;
Subroutine __insert_before: called CC=1 SCAR=0.0
515 2 $self->parent->__insert_before_child( $self, @_ );
516 }
517  
518 =pod
519  
520 =head2 insert_after @Elements
521  
522 The C<insert_after> method allows you to insert lexical perl content, in
523 the form of C<PPI::Element> objects, after the calling C<Element>. You need
524 to be very careful when modifying perl code, as it's easy to break things.
525  
526 In its initial incarnation, this method allows you to insert a single
527 Element, and will perform some basic checking to prevent you inserting
528 something that would be structurally wrong (in PDOM terms).
529  
530 In future, this method may be enhanced to allow the insertion of multiple
531 Elements, inline-parsed code strings or L<PPI::Document::Fragment> objects.
532  
533 Returns true if the Element was inserted, false if it can not be inserted,
534 or C<undef> if you do not provide a C<PPI::Element> object as a parameter.
535  
536 =cut
537  
538 sub __insert_after {
539 2 ▶ my $self = shift;
Subroutine __insert_after: called CC=1 SCAR=0.0
540 2 $self->parent->__insert_after_child( $self, @_ );
541 }
542  
543 =pod
544  
545 =head2 remove
546  
547 For a given C<PPI::Element>, the C<remove> method will remove it from its
548 parent B<intact>, along with all of its children.
549  
550 Returns the C<Element> itself as a convenience, or C<undef> if an error
551 occurs while trying to remove the C<Element>.
552  
553 =cut
554  
555 sub remove {
556 64 ▶ my $self = shift;
Subroutine remove: called CC=2 SCAR=7.1
Pod documented
557 64 ▶ my $parent = $self->parent or return $self;
Branch: 1 branch
BranchTrueFalseTotal
my $parent = $self->parent or return $self 0 64 64
558 64 $parent->remove_child( $self );
559 }
560  
561 =pod
562  
563 =head2 delete
564  
565 For a given C<PPI::Element>, the C<delete> method will remove it from its
566 parent, immediately deleting the C<Element> and all of its children (if it
567 has any).
568  
569 Returns true if the C<Element> was successfully deleted, or C<undef> if
570 an error occurs while trying to remove the C<Element>.
571  
572 =cut
573  
574 sub delete {
575 64 ▶ $_[0]->remove or return undef;
Subroutine delete: called CC=2 SCAR=7.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->remove or return undef 0 64 64
576 64 $_[0]->DESTROY;
577 64 1;
578 }
579  
580 =pod
581  
582 =head2 replace $Element
583  
584 Although some higher level class support more exotic forms of replace,
585 at the basic level the C<replace> method takes a single C<Element> as
586 an argument and replaces the current C<Element> with it.
587  
588 To prevent accidental damage to code, in this initial implementation the
589 replacement element B<must> be of the same class (or a subclass) as the
590 one being replaced.
591  
592 =cut
593  
594 sub replace {
595 0 ▶ my $self = ref $_[0] ? shift : return undef;
Subroutine replace: not called CC=3 SCAR=24.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
596 0 ▶ _INSTANCE(shift, ref $self) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
&_INSTANCE(shift(), ref $self) or return undef 0 0 0
597 0 die "The ->replace method has not yet been implemented";
598 }
599  
600 =pod
601  
602 =head2 location
603  
604 If the Element exists within a L<PPI::Document> that has
605 indexed the Element locations using C<PPI::Document::index_locations>, the
606 C<location> method will return the location of the first character of the
607 Element within the Document.
608  
609 Returns the location as a reference to a five-element array in the form C<[
610 $line, $rowchar, $col, $logical_line, $logical_file_name ]>. The values are in
611 a human format, with the first character of the file located at C<[ 1, 1, 1, ?,
612 'something' ]>.
613  
614 The second and third numbers are similar, except that the second is the
615 literal horizontal character, and the third is the visual column, taking
616 into account tabbing (see L<PPI::Document/"tab_width [ $width ]">).
617  
618 The fourth number is the line number, taking into account any C<#line>
619 directives. The fifth element is the name of the file that the element was
620 found in, if available, taking into account any C<#line> directives.
621  
622 Returns C<undef> on error, or if the L<PPI::Document> object has not been
623 indexed.
624  
625 =cut
626  
627 sub location {
628 78160 ▶ my $self = shift;
Subroutine location: called CC=2 SCAR=7.0
Pod documented
629  
630 78160 ▶ $self->_ensure_location_present or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->_ensure_location_present or return undef 0 78160 78160
631  
632 # Return a copy, not the original
633 78160 return [ @{$self->{_location}} ];
634 }
635  
636 =pod
637  
638 =head2 line_number
639  
640 If the Element exists within a L<PPI::Document> that has indexed the Element
641 locations using C<PPI::Document::index_locations>, the C<line_number> method
642 will return the line number of the first character of the Element within the
643 Document.
644  
645 Returns C<undef> on error, or if the L<PPI::Document> object has not been
646 indexed.
647  
648 =cut
649  
650 sub line_number {
651 90 ▶ my $self = shift;
Subroutine line_number: called CC=2 SCAR=7.1
Pod documented
652  
653 90 ▶ my $location = $self->location() or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $location = $self->location or return undef 0 90 90
654 90 return $location->[0];
655 }
656  
657 =pod
658  
659 =head2 column_number
660  
661 If the Element exists within a L<PPI::Document> that has indexed the Element
662 locations using C<PPI::Document::index_locations>, the C<column_number> method
663 will return the column number of the first character of the Element within the
664 Document.
665  
666 Returns C<undef> on error, or if the L<PPI::Document> object has not been
667 indexed.
668  
669 =cut
670  
671 sub column_number {
672 1 ▶ my $self = shift;
Subroutine column_number: called CC=2 SCAR=7.1
Pod documented
673  
674 1 ▶ my $location = $self->location() or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $location = $self->location or return undef 0 1 1
675 1 return $location->[1];
676 }
677  
678 =pod
679  
680 =head2 visual_column_number
681  
682 If the Element exists within a L<PPI::Document> that has indexed the Element
683 locations using C<PPI::Document::index_locations>, the C<visual_column_number>
684 method will return the visual column number of the first character of the
685 Element within the Document, according to the value of
686 L<PPI::Document/"tab_width [ $width ]">.
687  
688 Returns C<undef> on error, or if the L<PPI::Document> object has not been
689 indexed.
690  
691 =cut
692  
693 sub visual_column_number {
694 1 ▶ my $self = shift;
Subroutine visual_column_number: called CC=2 SCAR=7.1
Pod documented
695  
696 1 ▶ my $location = $self->location() or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $location = $self->location or return undef 0 1 1
697 1 return $location->[2];
698 }
699  
700 =pod
701  
702 =head2 logical_line_number
703  
704 If the Element exists within a L<PPI::Document> that has indexed the Element
705 locations using C<PPI::Document::index_locations>, the C<logical_line_number>
706 method will return the line number of the first character of the Element within
707 the Document, taking into account any C<#line> directives.
708  
709 Returns C<undef> on error, or if the L<PPI::Document> object has not been
710 indexed.
711  
712 =cut
713  
714 sub logical_line_number {
715 1 ▶ my $self = shift;
Subroutine logical_line_number: called CC=1 SCAR=0.0
Pod documented
716  
717 1 return $self->location()->[3];
718 }
719  
720 =pod
721  
722 =head2 logical_filename
723  
724 If the Element exists within a L<PPI::Document> that has indexed the Element
725 locations using C<PPI::Document::index_locations>, the C<logical_filename>
726 method will return the logical file name containing the first character of the
727 Element within the Document, taking into account any C<#line> directives.
728  
729 Returns C<undef> on error, or if the L<PPI::Document> object has not been
730 indexed.
731  
732 =cut
733  
734 sub logical_filename {
735 11 ▶ my $self = shift;
Subroutine logical_filename: called CC=2 SCAR=7.1
Pod documented
736  
737 11 ▶ my $location = $self->location() or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $location = $self->location or return undef 0 11 11
738 11 return $location->[4];
739 }
740  
741 sub _ensure_location_present {
742 78160 ▶ my $self = shift;
Subroutine _ensure_location_present: called CC=6 SCAR=19.6
743  
744 78160 ▶ unless ( exists $self->{_location} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_location"}) 169 77991 78160
745 # Are we inside a normal document?
746 169 ▶ my $Document = $self->document or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $Document = $self->document or return undef 0 169 169
747 169 ▶ if ( $Document->isa('PPI::Document::Fragment') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Document->isa("PPI::Document::Fragment")) 0 169 169
748 # Because they can't be serialized, document fragments
749 # do not support the concept of location.
750 0 return undef;
751 }
752  
753 # Generate the locations. If they need one location, then
754 # the chances are they'll want more, and it's better that
755 # everything is already pre-generated.
756 169 ▶ $Document->index_locations or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$Document->index_locations or return undef 0 169 169
757 169 ▶ unless ( exists $self->{_location} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_location"}) 0 169 169
758 # erm... something went very wrong here
759 0 return undef;
760 }
761 }
762  
763 78160 return 1;
764 }
765  
766 # Although flush_locations is only publically a Document-level method,
767 # we are able to implement it at an Element level, allowing us to
768 # selectively flush only the part of the document that occurs after the
769 # element for which the flush is called.
770 sub _flush_locations {
771 1 ▶ my $self = shift;
Subroutine _flush_locations: called CC=7 SCAR=29.4
772 1 ▶ unless ( $self == $self->top ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self == $self->top) 0 1 1
773 0 return $self->top->_flush_locations( $self );
774 }
775  
776 # Get the full list of all Tokens
777 1 my @Tokens = $self->tokens;
778  
779 # Optionally allow starting from an arbitrary element (or rather,
780 # the first Token equal-to-or-within an arbitrary element)
781 1 ▶ if ( _INSTANCE($_[0], 'PPI::Element') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_INSTANCE($_[0], "PPI::Element")) 0 1 1
782 0 my $start = shift->first_token;
783 0 ▶ while ( my $Token = shift @Tokens ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $Token = shift @Tokens) 0 0 0
784 0 ▶ return 1 unless $Token->{_location};
Branch: 1 branch
BranchTrueFalseTotal
unless $Token->{"_location"} 0 0 0
785 0 ▶ next unless refaddr($Token) == refaddr($start);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::refaddr($Token) == builtin::refaddr($start) 0 0 0
786  
787 # Found the start. Flush it's location
788 0 delete $$Token->{_location};
789 0 last;
790 }
791 }
792  
793 # Iterate over any remaining Tokens and flush their location
794 1 foreach my $Token ( @Tokens ) {
795 169 delete $Token->{_location};
796 }
797  
798 1 1;
799 }
800  
801  
802  
803  
804  
805 #####################################################################
806 # XML Compatibility Methods
807  
808 sub _xml_name {
809 5 ▶ my $class = ref $_[0] || $_[0];
Subroutine _xml_name: called CC=2 SCAR=7.6
Condition33%1/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
500
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table33%1/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
810 5 my $name = lc join( '_', split /::/, $class );
811 5 substr($name, 4);
812 }
813  
814 sub _xml_attr {
815 5 ▶ return {};
Subroutine _xml_attr: called CC=1 SCAR=0.0
816 }
817  
818 sub _xml_content {
819 5 ▶ defined $_[0]->{content} ? $_[0]->{content} : '';
Subroutine _xml_content: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]{'content'} ? : 4 1 5
820 }
821  
822  
823  
824  
825  
826 #####################################################################
827 # Internals
828  
829 # Set the error string
830 sub _error {
831 0 ▶ $errstr = $_[1];
Subroutine _error: not called CC=1 SCAR=6.9
832 0 undef;
833 }
834  
835 # Clear the error string
836 sub _clear {
837 0 ▶ $errstr = '';
Subroutine _clear: not called CC=1 SCAR=6.9
838 0 $_[0];
839 }
840  
841 # Being DESTROYed in this manner, rather than by an explicit
842 # ->delete means our reference count has probably fallen to zero.
843 # Therefore we don't need to remove ourselves from our parent,
844 # just the index ( just in case ).
845 ### XS -> PPI/XS.xs:_PPI_Element__DESTROY 0.900+
846 349307 ▶sub DESTROY { delete $_PARENT{refaddr $_[0]} }
Subroutine DESTROY: called CC=1 SCAR=0.0
847  
848 # Operator overloads
849 725 ▶sub __equals { ref $_[1] and refaddr($_[0]) == refaddr($_[1]) }
Subroutine __equals: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] and builtin::refaddr($_[0]) == builtin::refaddr($_[1]) 725 0 725
850 1 ▶sub __nequals { !__equals(@_) }
Subroutine __nequals: called CC=1 SCAR=0.0
851 sub __eq {
852 6298 ▶ my $self = _INSTANCE($_[0], 'PPI::Element') ? $_[0]->content : $_[0];
Subroutine __eq: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
&_INSTANCE($_[0], 'PPI::Element') ? : 6298 0 6298
853 6298 ▶ my $other = _INSTANCE($_[1], 'PPI::Element') ? $_[1]->content : $_[1];
Branch: 1 branch
BranchTrueFalseTotal
&_INSTANCE($_[1], 'PPI::Element') ? : 1 6297 6298
854 6298 $self eq $other;
855 }
856 1 ▶sub __ne { !__eq(@_) }
Subroutine __ne: called CC=1 SCAR=0.0
857  
858 63 1;
859  
860 =pod
861  
862 =head1 TO DO
863  
864 It would be nice if C<location> could be used in an ad-hoc manner. That is,
865 if called on an Element within a Document that has not been indexed, it will
866 do a one-off calculation to find the location. It might be very painful if
867 someone started using it a lot, without remembering to index the document,
868 but it would be handy for things that are only likely to use it once, such
869 as error handlers.
870  
871 =head1 SUPPORT
872  
873 See the L<support section|PPI/SUPPORT> in the main module.
874  
875 =head1 AUTHOR
876  
877 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
878  
879 =head1 COPYRIGHT
880  
881 Copyright 2001 - 2011 Adam Kennedy.
882  
883 This program is free software; you can redistribute
884 it and/or modify it under the same terms as Perl itself.
885  
886 The full text of the license can be found in the
887 LICENSE file included with this module.
888  
889 =cut
« PPI/Cache.pm ↑ Summary PPI/Token/Structure.pm »