PPI::Element(3)
π PPI::Element(3) π
okay bestie, gather round, because this is a big one. we're looking at PPI::Element(3) from the User Contributed Perl Documentation collection, and it's the blueprint page for the whole PDOM situation. it's giving foundational. it's giving ancestor energy. let's get into it π¦
β¨ NAME β¨
PPI::Element β the abstract Element class, aka the base class that every single source object in PPI inherits from. everything. the whole tree. no cap.
πΈ INHERITANCE πΈ
PPI::Element is the root of the PDOM tree
that's right, she's the root. the original. the mother of the PDOM tree. everybody else is a descendant, and yes, we'll get to what "descendant" means in a sec π
π DESCRIPTION π
So the abstract PPI::Element is the base class for literally every source-related object. like, from the tiniest single whitespace token all the way up to an entire document. it hands down a basic set of methods so all those objects share a common interface plus some basic implementations. it's the group chat everyone's in π
π METHODS π
β significant
okay so here's the tea. because PPI treats whitespace and other non-code stuff as Tokens (so that we can "round trip" the PPI::Document back to a file), the significant method is how we tell the difference between tokens that are actually part of the code and tokens that are not significant β like whitespace, POD, or the chunk of a file after (and including) the __END__ token.
Returns true if the Element is significant, or false if it's not. vibe check passed or failed, basically.
β class
The class method is provided as a convenience, and honestly it really does nothing more than returning ref($self). but some people have found that they appreciate the laziness of $Foo->class eq 'whatever', so I have caved to popular demand and included it. iconic of the author fr.
Returns the class of the Element as a string.
β tokens
The tokens method returns a list of PPI::Token objects for the Element β essentially getting back that part of the document as if it had not been lexed.
This also means there are no Statements and no Structures in the list, just the Token classes. just tokens. nothing else. periodt.
β content
For any PPI::Element, the content method will reconstitute the base code for it as a single string. This method is also the method used for overloading stringification. So when an Element is used in a double-quoted string, for example, this is the method that is called. string magic β¨
WARNING: π You should be aware that because of the way that here-docs are handled, any here-doc content is not included incontent, and as such you should notevalor execute the result if it contains anyPPI::Token::HereDoc.
The PPI::Document method serialize should be used to stringify a PDOM document into something that can be executed as expected. use that one, not this one, bestie π
Returns the basic code as a string (excluding here-doc content).
β parent
Elements themselves are not intended to contain other Elements β that job is left to the PPI::Node abstract class, which is a subclass of PPI::Element. However, all Elements can be contained within a parent Node.
If an Element is within a parent Node, the parent method returns the Node. π«Ά
β descendant_of $element
Answers whether a PPI::Element is contained within another one.
PPI::Elements are considered to be descendants of themselves. yes, self-love counts here π
β ancestor_of $element
Answers whether a PPI::Element contains another one.
PPI::Elements are considered to be ancestors of themselves. same energy, other direction β¨
β statement
For a PPI::Element that is contained (at some depth) within a PPI::Statement, the statement method will return the first parent Statement object lexically 'above' the Element.
Returns a PPI::Statement object, which may be the same Element if the Element is itself a PPI::Statement object.
Returns false if the Element is not within a Statement and is not itself a Statement.
β top
For a PPI::Element that is contained within a PDOM tree, the top method will return the top-level Node in the tree. Most of the time this should be a PPI::Document object, however this will not always be so. For example, if a subroutine has been removed from its Document, to be moved to another Document. main character energy, just vibing outside the tree π
Returns the top-most PDOM object, which may be the same Element, if it is not within any parent PDOM object.
β document
For an Element that is contained within a PPI::Document object, the document method will return the top-level Document for the Element.
Returns the PPI::Document for this Element, or false if the Element is not contained within a Document.
β next_sibling
All PPI::Node objects (specifically, our parent Node) contain a number of PPI::Element objects. The next_sibling method returns the PPI::Element immediately after the current one, or false if there is no next sibling.
β snext_sibling
As per the other 's' methods, the snext_sibling method returns the next significant sibling of the PPI::Element object.
Returns a PPI::Element object, or false if there is no 'next' significant sibling.
β previous_sibling
All PPI::Node objects (specifically, our parent Node) contain a number of PPI::Element objects. The previous_sibling method returns the Element immediately before the current one, or false if there is no 'previous' PPI::Element object.
β sprevious_sibling
As per the other 's' methods, the sprevious_sibling method returns the previous significant sibling of the PPI::Element object.
Returns a PPI::Element object, or false if there is no 'previous' significant sibling.
β first_token
As a support method for higher-order algorithms that deal specifically with tokens and actual Perl content, the first_token method finds the first PPI::Token object within or equal to this one.
That is, if called on a PPI::Node subclass, it will descend until it finds a PPI::Token. If called on a PPI::Token object, it will return the same object.
Returns a PPI::Token object, or dies on error (which should be extremely rare and only occur if an illegal empty PPI::Statement exists below the current Element somewhere). dying is not the vibe but ok π
β last_token
As a support method for higher-order algorithms that deal specifically with tokens and actual Perl content, the last_token method finds the last PPI::Token object within or equal to this one.
That is, if called on a PPI::Node subclass, it will descend until it finds a PPI::Token. If called on a PPI::Token object, it will return the itself.
Returns a PPI::Token object, or dies on error (which should be extremely rare and only occur if an illegal empty PPI::Statement exists below the current Element somewhere).
β next_token
As a support method for higher-order algorithms that deal specifically with tokens and actual Perl content, the next_token method finds the PPI::Token object that is immediately after the current Element, even if it is not within the same parent PPI::Node as the one for which the method is being called.
Note that this is not defined as a PPI::Token-specific method, because it can be useful to find the next token that is after, say, a PPI::Statement, although obviously it would be useless to want the next token after a PPI::Document. lmao fair
Returns a PPI::Token object, or false if there are no more tokens after the Element.
β previous_token
As a support method for higher-order algorithms that deal specifically with tokens and actual Perl content, the previous_token method finds the PPI::Token object that is immediately before the current Element, even if it is not within the same parent PPI::Node as this one.
Note that this is not defined as a PPI::Token-only method, because it can be useful to find the token is before, say, a PPI::Statement, although obviously it would be useless to want the next token before a PPI::Document.
Returns a PPI::Token object, or false if there are no more tokens before the Element.
β presumed_features
Returns a hash that indicates which features appear to be active for the given element. π
β clone
As per the Clone module, the clone method makes a perfect copy of an Element object. In the generic case, the implementation is done using the Clone module's mechanism itself. In higher-order cases, such as for Nodes, there is more work involved to keep the parent-child links intact. copy paste but make it correct π
β insert_before @Elements
The insert_before method allows you to insert lexical perl content, in the form of PPI::Element objects, before the calling Element. You need to be very careful when modifying perl code, as it's easy to break things. π₯
In its initial incarnation, this method allows you to insert a single Element, and will perform some basic checking to prevent you inserting something that would be structurally wrong (in PDOM terms).
In future, this method may be enhanced to allow the insertion of multiple Elements, inline-parsed code strings or PPI::Document::Fragment objects.
Returns true if the Element was inserted, false if it can not be inserted, or undef if you do not provide a PPI::Element object as a parameter.
β insert_after @Elements
The insert_after method allows you to insert lexical perl content, in the form of PPI::Element objects, after the calling Element. You need to be very careful when modifying perl code, as it's easy to break things. π₯
In its initial incarnation, this method allows you to insert a single Element, and will perform some basic checking to prevent you inserting something that would be structurally wrong (in PDOM terms).
In future, this method may be enhanced to allow the insertion of multiple Elements, inline-parsed code strings or PPI::Document::Fragment objects.
Returns true if the Element was inserted, false if it can not be inserted, or undef if you do not provide a PPI::Element object as a parameter.
β remove
For a given PPI::Element, the remove method will remove it from its parent intact, along with all of its children. gentle removal, no destruction π
Returns the Element itself as a convenience, or undef if an error occurs while trying to remove the Element.
β delete
For a given PPI::Element, the delete method will remove it from its parent, immediately deleting the Element and all of its children (if it has any). no survivors. gone gone. π
Returns true if the Element was successfully deleted, or undef if an error occurs while trying to remove the Element.
β replace $Element
Although some higher level class support more exotic forms of replace, at the basic level the replace method takes a single Element as an argument and replaces the current Element with it.
To prevent accidental damage to code, in this initial implementation the replacement element must be of the same class (or a subclass) as the one being replaced. rules are rules β¨
If successful, returns the replace element. Otherwise, returns undef.
β location
If the Element exists within a PPI::Document that has indexed the Element locations using PPI::Document::index_locations, the location method will return the location of the first character of the Element within the Document.
Returns the location as a reference to a five-element array in the form [ $line, $rowchar, $col, $logical_line, $logical_file_name ]. The values are in a human format, with the first character of the file located at [ 1, 1, 1, ?, 'something' ].
The second and third numbers are similar, except that the second is the literal horizontal character, and the third is the visual column, taking into account tabbing (see tab_width [ $width ] in PPI::Document). π¦
and that's it for part one, bestie β more of this page is still coming, so don't go anywhere π
β¦the fourth number is the line number, and yes, it takes any #line directives into account. The fifth element is the name of the file that the element was found in β if it's available β again with any #line directives folded in. π¦
And whenever something goes wrong, you get undef. Same deal if the PPI::Document object hasn't been indexed yet. Sorry bestie, that's just how it is. π
π line_number
If your Element is living inside a PPI::Document that has indexed the Element locations using PPI::Document::index_locations, then line_number hands you the line number of the first character of the Element within the Document. Slay. β¨
Returns undef on error, or if the PPI::Document object has not been indexed.
πΈ column_number
Same energy, different number: if the Element exists within a PPI::Document that indexed its Element locations using PPI::Document::index_locations, column_number gives you the column number of the first character of the Element within the Document. π
Returns undef on error, or if the PPI::Document object has not been indexed.
π§ visual_column_number
Okay this one is lowkey the icon. If the Element exists within a PPI::Document that has indexed the Element locations using PPI::Document::index_locations, then visual_column_number returns the visual column number of the first character of the Element within the Document β and it figures that out according to the value of tab_width [ $width ] in PPI::Document. Because tabs are not one column, babe, and we respect that. π
Returns undef on error, or if the PPI::Document object has not been indexed.
π¦ logical_line_number
If the Element exists within a PPI::Document that has indexed the Element locations using PPI::Document::index_locations, logical_line_number returns the line number of the first character of the Element within the Document, taking into account any #line directives. So it's the line number your brain thinks it is, not the one the file physically has. π
Returns undef on error, or if the PPI::Document object has not been indexed.
π logical_filename
If the Element exists within a PPI::Document that has indexed the Element locations using PPI::Document::index_locations, logical_filename returns the logical file name containing the first character of the Element within the Document, taking into account any #line directives. Yep, the filename can lie, and this method knows the truth. π
Returns undef on error, or if the PPI::Document object has not been indexed.
π TO DO
Not me reading the TO DO list β but okay, it's giving wishlist, and honestly it's a mood. It would be nice if location could be used in an ad-hoc manner. Like, if you called it on an Element inside a Document that hasn't been indexed, it would just do a one-off calculation to find the location for you. π
Now, it might get very painful if someone started using it a lot, without remembering to index the document first. But it would be super handy for stuff that's only likely to use it once β think error handlers. β¨
π«Ά SUPPORT
See the support section in the main module. That's the whole thing, bestie, go check it out. π
π AUTHOR
Adam Kennedy <adamk@cpan.org>
β COPYRIGHT
Copyright 2001 - 2011 Adam Kennedy.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the LICENSE file included with this module.
And that's a wrap on PPI::Element(3) π β this version is from perl v5.42.0, dated 2026-01-17. Go forth and index your documents, bestie, your line numbers will thank you. π β¨
π¦ where this page comes from
- fedora 44: perl-PPI 1.284-2.fc44 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms