1 .\" Automatically generated by Pod::Man 2.22 (Pod::Simple 3.10)
4 .\" ========================================================================
5 .de Sp \" Vertical space (when we can't use .PP)
9 .de Vb \" Begin verbatim text
14 .de Ve \" End verbatim text
18 .\" Set up some character translations and predefined strings. \*(-- will
19 .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
20 .\" double quote, and \*(R" will give a right double quote. \*(C+ will
21 .\" give a nicer C++. Capital omega is used to do unbreakable dashes and
22 .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,
23 .\" nothing in troff, for use with C<>.
25 .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
29 . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
30 . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
43 .\" Escape single quotes in literal strings from groff's Unicode transform.
47 .\" If the F register is turned on, we'll generate index entries on stderr for
48 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
49 .\" entries marked with X<> in POD. Of course, you'll have to process the
50 .\" output yourself in some meaningful fashion.
53 . tm Index:\\$1\t\\n%\t"\\$2"
63 .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
64 .\" Fear. Run. Save yourself. No user-serviceable parts.
65 . \" fudge factors for nroff and troff
74 . ds #H ((1u-(\\\\n(.fu%2u))*.13m)
80 . \" simple accents for nroff and troff
90 . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
91 . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
92 . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
93 . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
94 . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
95 . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
97 . \" troff and (daisy-wheel) nroff accents
98 .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
99 .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
100 .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
101 .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
102 .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
103 .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
104 .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
105 .ds ae a\h'-(\w'a'u*4/10)'e
106 .ds Ae A\h'-(\w'A'u*4/10)'E
107 . \" corrections for vroff
108 .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
109 .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
110 . \" for low resolution devices (crt and lpr)
111 .if \n(.H>23 .if \n(.V>19 \
124 .\" ========================================================================
126 .IX Title "PPI::Node 3"
127 .TH PPI::Node 3 "2009-08-08" "perl v5.8.7" "User Contributed Perl Documentation"
128 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
129 .\" way too many mistakes in technical documents.
133 PPI::Node \- Abstract PPI Node class, an Element that can contain other Elements
135 .IX Header "INHERITANCE"
141 .IX Header "SYNOPSIS"
143 \& # Create a typical node (a Document in this case)
144 \& my $Node = PPI::Document\->new;
146 \& # Add an element to the node( in this case, a token )
147 \& my $Token = PPI::Token::Word\->new(\*(Aqmy\*(Aq);
148 \& $Node\->add_element( $Token );
150 \& # Get the elements for the Node
151 \& my @elements = $Node\->children;
153 \& # Find all the barewords within a Node
154 \& my $barewords = $Node\->find( \*(AqPPI::Token::Word\*(Aq );
156 \& # Find by more complex criteria
157 \& my $my_tokens = $Node\->find( sub { $_[1]\->content eq \*(Aqmy\*(Aq } );
159 \& # Remove all the whitespace
160 \& $Node\->prune( \*(AqPPI::Token::Whitespace\*(Aq );
162 \& # Remove by more complex criteria
163 \& $Node\->prune( sub { $_[1]\->content eq \*(Aqmy\*(Aq } );
166 .IX Header "DESCRIPTION"
167 The \f(CW\*(C`PPI::Node\*(C'\fR class provides an abstract base class for the Element
168 classes that are able to contain other elements PPI::Document,
169 PPI::Statement, and PPI::Structure.
171 As well as those listed below, all of the methods that apply to
172 PPI::Element objects also apply to \f(CW\*(C`PPI::Node\*(C'\fR objects.
176 .IX Subsection "scope"
177 The \f(CW\*(C`scope\*(C'\fR method returns true if the node represents a lexical scope
178 boundary, or false if it does not.
179 .ie n .SS "add_element $Element"
180 .el .SS "add_element \f(CW$Element\fP"
181 .IX Subsection "add_element $Element"
182 The \f(CW\*(C`add_element\*(C'\fR method adds a PPI::Element object to the end of a
183 \&\f(CW\*(C`PPI::Node\*(C'\fR. Because Elements maintain links to their parent, an
184 Element can only be added to a single Node.
186 Returns true if the PPI::Element was added. Returns \f(CW\*(C`undef\*(C'\fR if the
187 Element was already within another Node, or the method is not passed
188 a PPI::Element object.
190 .IX Subsection "elements"
191 The \f(CW\*(C`elements\*(C'\fR method accesses all child elements \fBstructurally\fR within
192 the \f(CW\*(C`PPI::Node\*(C'\fR object. Note that in the base of the PPI::Structure
193 classes, this \f(CW\*(C`DOES\*(C'\fR include the brace tokens at either end of the
196 Returns a list of zero or more PPI::Element objects.
198 Alternatively, if called in the scalar context, the \f(CW\*(C`elements\*(C'\fR method
199 returns a count of the number of elements.
201 .IX Subsection "first_element"
202 The \f(CW\*(C`first_element\*(C'\fR method accesses the first element structurally within
203 the \f(CW\*(C`PPI::Node\*(C'\fR object. As for the \f(CW\*(C`elements\*(C'\fR method, this does include
204 the brace tokens for PPI::Structure objects.
206 Returns a PPI::Element object, or \f(CW\*(C`undef\*(C'\fR if for some reason the
207 \&\f(CW\*(C`PPI::Node\*(C'\fR object does not contain any elements.
209 .IX Subsection "last_element"
210 The \f(CW\*(C`last_element\*(C'\fR method accesses the last element structurally within
211 the \f(CW\*(C`PPI::Node\*(C'\fR object. As for the \f(CW\*(C`elements\*(C'\fR method, this does include
212 the brace tokens for PPI::Structure objects.
214 Returns a PPI::Element object, or \f(CW\*(C`undef\*(C'\fR if for some reason the
215 \&\f(CW\*(C`PPI::Node\*(C'\fR object does not contain any elements.
217 .IX Subsection "children"
218 The \f(CW\*(C`children\*(C'\fR method accesses all child elements lexically within the
219 \&\f(CW\*(C`PPI::Node\*(C'\fR object. Note that in the case of the PPI::Structure
220 classes, this does \fB\s-1NOT\s0\fR include the brace tokens at either end of the
223 Returns a list of zero of more PPI::Element objects.
225 Alternatively, if called in the scalar context, the \f(CW\*(C`children\*(C'\fR method
226 returns a count of the number of lexical children.
228 .IX Subsection "schildren"
229 The \f(CW\*(C`schildren\*(C'\fR method is really just a convenience, the significant-only
230 variation of the normal \f(CW\*(C`children\*(C'\fR method.
232 In list context, returns a list of significant children. In scalar context,
233 returns the number of significant children.
234 .ie n .SS "child $index"
235 .el .SS "child \f(CW$index\fP"
236 .IX Subsection "child $index"
237 The \f(CW\*(C`child\*(C'\fR method accesses a child PPI::Element object by its
238 position within the Node.
240 Returns a PPI::Element object, or \f(CW\*(C`undef\*(C'\fR if there is no child
241 element at that node.
242 .ie n .SS "schild $index"
243 .el .SS "schild \f(CW$index\fP"
244 .IX Subsection "schild $index"
245 The lexical structure of the Perl language ignores 'insignificant' items,
246 such as whitespace and comments, while \s-1PPI\s0 treats these items as valid
247 tokens so that it can reassemble the file at any time. Because of this,
248 in many situations there is a need to find an Element within a Node by
249 index, only counting lexically significant Elements.
251 The \f(CW\*(C`schild\*(C'\fR method returns a child Element by index, ignoring
252 insignificant Elements. The index of a child Element is specified in the
253 same way as for a normal array, with the first Element at index 0, and
254 negative indexes used to identify a \*(L"from the end\*(R" position.
255 .ie n .SS "contains $Element"
256 .el .SS "contains \f(CW$Element\fP"
257 .IX Subsection "contains $Element"
258 The \f(CW\*(C`contains\*(C'\fR method is used to determine if another PPI::Element
259 object is logically \*(L"within\*(R" a \f(CW\*(C`PPI::Node\*(C'\fR. For the special case of the
260 brace tokens at either side of a PPI::Structure object, they are
261 generally considered \*(L"within\*(R" a PPI::Structure object, even if they are
262 not actually in the elements for the PPI::Structure.
264 Returns true if the PPI::Element is within us, false if not, or \f(CW\*(C`undef\*(C'\fR
266 .ie n .SS "find $class | \e&wanted"
267 .el .SS "find \f(CW$class\fP | \e&wanted"
268 .IX Subsection "find $class | &wanted"
269 The \f(CW\*(C`find\*(C'\fR method is used to search within a code tree for
270 PPI::Element objects that meet a particular condition.
272 To specify the condition, the method can be provided with either a simple
273 class name (full or shortened), or a \f(CW\*(C`CODE\*(C'\fR/function reference.
276 \& # Find all single quotes in a Document (which is a Node)
277 \& $Document\->find(\*(AqPPI::Quote::Single\*(Aq);
279 \& # The same thing with a shortened class name
280 \& $Document\->find(\*(AqQuote::Single\*(Aq);
282 \& # Anything more elaborate, we so with the sub
283 \& $Document\->find( sub {
284 \& # At the top level of the file...
285 \& $_[1]\->parent == $_[0]
287 \& # ...find all comments and POD
288 \& $_[1]\->isa(\*(AqPPI::Token::Pod\*(Aq)
290 \& $_[1]\->isa(\*(AqPPI::Token::Comment\*(Aq)
295 The function will be passed two arguments, the top-level \f(CW\*(C`PPI::Node\*(C'\fR
296 you are searching in and the current PPI::Element that the condition
299 The anonymous function should return one of three values. Returning true
300 indicates a condition match, defined-false (\f(CW0\fR or \f(CW\*(Aq\*(Aq\fR) indicates
301 no-match, and \f(CW\*(C`undef\*(C'\fR indicates no-match and no-descend.
303 In the last case, the tree walker will skip over anything below the
304 \&\f(CW\*(C`undef\*(C'\fR\-returning element and move on to the next element at the same
307 To halt the entire search and return \f(CW\*(C`undef\*(C'\fR immediately, a condition
308 function should throw an exception (i.e. \f(CW\*(C`die\*(C'\fR).
310 Note that this same wanted logic is used for all methods documented to
311 have a \f(CW\*(C`\e&wanted\*(C'\fR parameter, as this one does.
313 The \f(CW\*(C`find\*(C'\fR method returns a reference to an array of PPI::Element
314 objects that match the condition, false (but defined) if no Elements match
315 the condition, or \f(CW\*(C`undef\*(C'\fR if you provide a bad condition, or an error
316 occurs during the search process.
318 In the case of a bad condition, a warning will be emitted as well.
319 .ie n .SS "find_first $class | \e&wanted"
320 .el .SS "find_first \f(CW$class\fP | \e&wanted"
321 .IX Subsection "find_first $class | &wanted"
322 If the normal \f(CW\*(C`find\*(C'\fR method is like a grep, then \f(CW\*(C`find_first\*(C'\fR is
323 equivalent to the Scalar::Util \f(CW\*(C`first\*(C'\fR function.
325 Given an element class or a wanted function, it will search depth-first
326 through a tree until it finds something that matches the condition,
327 returning the first Element that it encounters.
329 See the \f(CW\*(C`find\*(C'\fR method for details on the format of the search condition.
331 Returns the first PPI::Element object that matches the condition, false
332 if nothing matches the condition, or \f(CW\*(C`undef\*(C'\fR if given an invalid condition,
334 .ie n .SS "find_any $class | \e&wanted"
335 .el .SS "find_any \f(CW$class\fP | \e&wanted"
336 .IX Subsection "find_any $class | &wanted"
337 The \f(CW\*(C`find_any\*(C'\fR method is a short-circuiting true/false method that behaves
338 like the normal \f(CW\*(C`find\*(C'\fR method, but returns true as soon as it finds any
339 Elements that match the search condition.
341 See the \f(CW\*(C`find\*(C'\fR method for details on the format of the search condition.
343 Returns true if any Elements that match the condition can be found, false if
344 not, or \f(CW\*(C`undef\*(C'\fR if given an invalid condition, or an error occurs.
345 .ie n .SS "remove_child $Element"
346 .el .SS "remove_child \f(CW$Element\fP"
347 .IX Subsection "remove_child $Element"
348 If passed a PPI::Element object that is a direct child of the Node,
349 the \f(CW\*(C`remove_element\*(C'\fR method will remove the \f(CW\*(C`Element\*(C'\fR intact, along
350 with any of its children. As such, this method acts essentially as a
353 If successful, returns the removed element. Otherwise, returns \f(CW\*(C`undef\*(C'\fR.
354 .ie n .SS "prune $class | \e&wanted"
355 .el .SS "prune \f(CW$class\fP | \e&wanted"
356 .IX Subsection "prune $class | &wanted"
357 The \f(CW\*(C`prune\*(C'\fR method is used to strip PPI::Element objects out of a code
358 tree. The argument is the same as for the \f(CW\*(C`find\*(C'\fR method, either a class
359 name, or an anonymous subroutine which returns true/false. Any Element
360 that matches the class|wanted will be deleted from the code tree, along
361 with any of its children.
363 The \f(CW\*(C`prune\*(C'\fR method returns the number of \f(CW\*(C`Element\*(C'\fR objects that matched
364 and were removed, \fBnon-recursively\fR. This might also be zero, so avoid a
365 simple true/false test on the return false of the \f(CW\*(C`prune\*(C'\fR method. It
366 returns \f(CW\*(C`undef\*(C'\fR on error, which you probably \fBshould\fR test for.
369 \&\- Move as much as possible to \s-1PPI::XS\s0
372 See the support section in the main module.
375 Adam Kennedy <adamk@cpan.org>
377 .IX Header "COPYRIGHT"
378 Copyright 2001 \- 2009 Adam Kennedy.
380 This program is free software; you can redistribute
381 it and/or modify it under the same terms as Perl itself.
383 The full text of the license can be found in the
384 \&\s-1LICENSE\s0 file included with this module.