PPI/Element.pm

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