PPIx::R...:Element(3)

πŸ’– PPIx::Regexp::Element πŸ’–

okay bestie, welcome to PPIx::Regexp::Element(3) β€” the user-contributed Perl documentation page for this absolute foundational little class. she's giving very much "I was here first, everything else came from me" energy and honestly? slay. πŸ‘‘


✨ NAME ✨

PPIx::Regexp::Element β€” the Base of the PPIx::Regexp hierarchy. the origin story. the mother of the whole family tree, fr fr. 🌳


πŸ¦‹ SYNOPSIS πŸ¦‹

No user-serviceable parts inside.

that's it. that's the synopsis. no cap, there are literally no user-serviceable parts inside. she's not here to be poked at directly, she's here to be inherited from. πŸ’…


🧚 INHERITANCE 🧚

so <pink>PPIx::Regexp::Element</pink> is not descended from any other class. she's self-made, she's the blueprint, she's the one who started it all. periodt.

and she is the parent of two iconic children:

  • PPIx::Regexp::Node
  • PPIx::Regexp::Token

those two are the ones actually doing the work out there in the wild, iykyk. 🫢


🌸 DESCRIPTION 🌸

so this class is the base of the entire PPIx::Regexp object hierarchy. literally everything else is a descendant of her. and what she brings to the table is the same kind of navigational functionality that PPI::Element provides β€” like, the whole "walk around the tree, find your siblings, find your parent" vibe. it's giving very much "I taught your favorite class everything she knows." 😌


πŸ’œ METHODS πŸ’œ

this class provides the following public methods, bestie. the methods that are not documented here are private, and unsupported in the sense that the author reserves the right to change or remove them without notice. so like, don't get attached to the undocumented ones, they could ghost you at any moment. 😭


⭐ accepts_perl ⭐

$token->accepts_perl( '5.020' )
    and say 'This works under Perl 5.20';

this method returns a true value if the token is acceptable under the specified version of Perl, and a false value otherwise. πŸ’–

so unless the token (or its contents) have been equivocated on, the result is simply what you would expect based on testing the results of perl_version_introduced() and perl_version_removed() versus the given Perl version number. like, it's just doing the math for you. iconic.

this method was added in version 0.051_01. πŸŽ€

⭐ ancestor_of ⭐

this method returns true if the object is an ancestor of the argument, and false otherwise. and here's the cute part: by the definition of this method, $self is its own ancestor. yes queen, self-love. πŸ’…


⭐ can_be_quantified ⭐

$token->can_be_quantified()
    and print "This element can be quantified.\n";

this method returns true if the element can be quantified. simple as that. ✨


⭐ class ⭐

this method returns the class name of the element. and ngl, it is the same as ref $self. no surprises here, bestie. πŸ¦‹


⭐ column_number ⭐

this method returns the column number of the first character in the element, or undef if that can not be determined. sometimes she just doesn't know, and that's valid. πŸ™ˆ


⭐ comment ⭐

this method returns true if the element is a comment and false otherwise. is she a comment? vibe check says yes or no. πŸ’¬


⭐ content ⭐

this method returns the content of the element. the substance. the material. the stuff. πŸ“


⭐ descendant_of ⭐

this method returns true if the object is a descendant of the argument, and false otherwise. and by the definition of this method, $self is its own descendant β€” again with the self-love, we adore to see it. πŸ‘‘


⭐ explain ⭐

this method returns a brief explanation of what the element does. the return will be either a string or undef in scalar context, but may be multiple values or an empty array in list context. versatile queen. 🌈

this method should be considered experimental. what it returns may change without notice as the author's understanding of what all the pieces/parts of a Perl regular expression evolves. the worst case is that it will prove entirely infeasible to implement satisfactorily, in which case it will be put through a deprecation cycle and retracted. so like, enjoy it while it lasts, bestie, no promises. 😭

⭐ error ⭐

say $token->error();

if an element is one of the classes that represents a parse error, this method may return a brief message saying why. otherwise it will return undef. so if you get a message back, something went wrong and she's telling you about it. πŸ’€


⭐ is_matcher ⭐

this method reports on whether the element potentially matches something. possible returns are:

  • a true value if it does
  • a false (but defined) value if it does not
  • undef if this can not be determined

the idea is to classify elements based on whether they potentially match something in the target string. 🎯

now here's the tea on who overrides what:

  • this method is overridden to return undef in PPIx::Regexp::Token::Code, PPIx::Regexp::Token::Interpolation, and PPIx::Regexp::Token::Unknown.
  • this method is overridden to return a true value in PPIx::Regexp::Token::Assertion, PPIx::Regexp::Token::CharClass, PPIx::Regexp::Token::Literal, and PPIx::Regexp::Token::Reference.
  • for PPIx::Regexp::Node, this method is overridden to return a value computed from the node's children.
  • for anything else this method returns a false (but defined) value.

⭐ in_regex_set ⭐

this method returns a true value if the invocant is contained in an extended bracketed character class (also known as a regex set), and a false value otherwise. and specifically, this method returns true if the invocant is a PPIx::Regexp::Structure::RegexSet. ✨


⭐ is_quantifier ⭐

$token->is_quantifier()
    and print "This element is a quantifier.\n";

this method returns true if the element is a quantifier. and here's the sneaky part β€” you can not tell this from the element's class, because a right curly bracket may represent a quantifier for the purposes of figuring out whether a greediness token is possible. so like, don't try to guess from the class name, bestie, just ask her directly. πŸŽ€


⭐ line_number ⭐

this method returns the line number of the first character in the element, or undef if that can not be determined. πŸ¦‹


⭐ location ⭐

this method returns a reference to an array describing the position of the element in the regular expression, or undef if locations were not indexed.

the array is compatible with the corresponding PPI::Element method. so she plays nice with others, we love a compatible icon. πŸ’…


⭐ logical_filename ⭐

this method returns the logical file name (taking #line directives into account) of the file containing first character in the element, or undef if that can not be determined. yes, #line directives get taken into account, she's thorough like that. 🌸


⭐ logical_line_number ⭐

this method returns the logical line number (taking #line directives into account) of the first character in the element, or undef if that can not be determined. again with the #line awareness. consistent queen. πŸ‘‘


⭐ main_structure ⭐

this method returns the PPIx::Regexp::Structure::Main that contains the element. in practice this will be a PPIx::Regexp::Structure::Regexp or a PPIx::Regexp::Structure::Replacement, (yes, the original really does end on that comma, she's a little quirky like that 😌).

if the element is not contained in any such structure, undef is returned. this will happen if the element is a PPIx::Regexp or one of its immediate children. so like, top-level girlies don't have a main structure, that's just how it is. 🌈


⭐ modifier_asserted ⭐

$token->modifier_asserted( 'i' )
    and print "Matched without regard to case.\n";

this method returns true if the given modifier is in effect for the element, and false otherwise. πŸ’–

what it does is to walk backwards from the element until it finds a modifier object that specifies the modifier, whether asserted or negated, and returns the specified value. if nobody specifies the modifier, it returns undef. (the original has a tiny typo β€” "negated. and returns" β€” but the meaning is exactly what I just said, bestie, don't @ me 🫢)

this method will not work reliably if called on tokenizer output. so like, heads up on that one. 😭

⭐ next_element ⭐

this method returns the next element, or nothing if there is none.

unlike next_sibling(), this will cross from the content of a structure into the elements that define the structure, or vice versa. she's brave, she crosses boundaries. ✨


⭐ next_sibling ⭐

this method returns the element's next sibling, or nothing if there is none. no boundary-crossing here, she stays in her lane. πŸ’…


⭐ next_token ⭐

this method returns the next token, or nothing if there is none.

unlike next_element(), this will walk the parse tree. different method, different journey. 🌳


⭐ parent ⭐

this method returns the parent of the element, or undef if there is none. every icon has a mother, and if she doesn't, you get undef. πŸ’œ


⭐ perl_version_introduced ⭐

this method returns the version of Perl in which the element was introduced. this will be at least 5.000. before 5.006 the author is relying on the perldelta, perlre, and perlop documentation, since he has been unable to build earlier Perls. and since he found no documentation before 5.003, he assumes that anything found in 5.003 is also in 5.000. fair enough, we respect the detective work. πŸ”

since this all depends on his ability to read and understand masses of documentation, the results of this method should be viewed with caution, if not downright skepticism. so like, trust but verify, bestie. πŸ‘€

there are also cases which are ambiguous in various ways. for those, see the PPIx::Regexp documentation, particularly Changes in Syntax.

and very occasionally, a construct will be removed and then added back. if this happens, this method will return the lowest version in which the construct appeared. for the known instances of this, see the PPIx::Regexp documentation, particularly Equivocation. πŸŽ€


⭐ perl_version_removed ⭐

this method returns the version of Perl in which the element was removed. if the element is still valid, the return is undef. πŸ¦‹

all the caveats to perl_version_introduced() apply here also, though perhaps less severely, since although many features have been introduced since 5.0, few have been removed. so like, less chaos, but still chaos. 🌸

and very occasionally, a construct will be removed and then added back. if this happens, this method will return the undef if the construct is present in the highest-numbered version of Perl (whether production or development), or the version after the highest-numbered version in which it appeared otherwise. for the known instances of this, see the PPIx::Regexp documentation, particularly Equivocation. ⭐


⭐ previous_element ⭐

this method returns the previous element, or nothing if there is none.

unlike previous_sibling(), this will cross from the content of a structure into the elements that define the structure, or vice versa. again with the boundary-crossing, she contains multitudes. πŸ’…


⭐ previous_sibling ⭐

this method returns the element's previous sibling, or nothing if there is none.

this method is analogous to the same-named PPI::Element method, in that it will not cross from the content of a structure into the elements that define the structure. staying in her lane, respectfully. ✨


⭐ previous_token ⭐

this method returns the previous token, or nothing if there is none.

unlike previous_element(), this will walk the parse tree. 🌳


⭐ raw_width ⭐

my ( $raw_min, $raw_max ) = $self->raw_width();

this public method returns the minimum and maximum width matched by the element before taking into account such details as what the element actually is and how it is quantified. so it's the raw, unbothered, unquantified truth. πŸ’–

either or both elements can be undef if the width can not be determined, and the maximum can be Inf. yes, Inf. infinity. she can go on forever, and honestly? same. 😭

this method was added in version 0.085_01. πŸŽ€

⭐ remove_insignificant ⭐

this method returns a new object manufactured from the invocant, but containing only elements for which $elem->significant() returns a true value. so like, she clones herself and throws out all the insignificant parts. a glow-up, essentially. ✨

here's the breakdown:

  • if you call this method on a PPIx::Regexp::Node you will get back a deep clone, but without the insignificant elements. πŸ‘‘
  • if you call this method on any other PPIx::Regexp class you will get back either the invocant or nothing. this may change to a clone of the invocant or nothing if unforeseen problems arise with returning the invocant, or if objects become mutable (unlikely, but not impossible.) so like, future her might change her mind, but for now that's the deal. πŸ™ˆ

⭐ requirements_for_perl ⭐

say $token->requirements_for_perl();

this method returns a string representing the Perl requirements for a given module. this should only be used for informational purposes, as the format of the string may be subject to change. so don't build anything on top of it, bestie, she warned you. πŸ’€

at the moment, the returns may be:

version <= $]
version <= $] < version
two or more of the above joined by '||'
! $]

the last one means that, although all the components of the regular expression can be compiled by some version of Perl, there is no version that will compile all of them. it's giving "each piece works somewhere, but nowhere works for all of them." tragic. 😭

and I reiterate: the returned string may be subject to change, maybe without warning. πŸŽ€

this method was added in version 0.051_01. ⭐

⭐ scontent ⭐

this method returns the significant content of the element. that is, if called on the parse of / f u b a r /x, it returns /fubar/x. the spaces? gone. the vibes? immaculate. 🌈

  • if the invocant contains no insignificant elements, it is the same as content().
  • if called on an insignificant element, it returns nothing β€” that is, undef in scalar context, and an empty list in list context.

so like, insignificant elements get nothing back, because she simply does not see them. πŸ’…


Okay bestie, this method? It was inspired by jb's question on Perl Monks about stripping comments and white space from a regular expression: https://perlmonks.org/?node_id=1207556

This method was added in version 0.053_01.


✨ significant ✨

This one is literally a vibe check. It returns true if the element is significant, and false otherwise. πŸ’…

✨ snext_element ✨

Returns the next significant element, or nothing if there is none. Unlike snext_sibling(), this will cross from the content of a structure into the elements that define the structure, or vice versa. No cap.

✨ snext_sibling ✨

Returns the element's next significant sibling, or nothing if there is none. This method is analogous to the same-named PPI::Element method, in that it will not cross from the content of a structure into the elements that define the structure.

✨ sprevious_element ✨

Returns the previous significant element, or nothing if there is none. Unlike sprevious_sibling(), this will cross from the content of a structure into the elements that define the structure, or vice versa.

✨ sprevious_sibling ✨

Returns the element's previous significant sibling, or nothing if there is none. This method is analogous to the same-named PPI::Element method, in that it will not cross from the content of a structure into the elements that define the structure.

✨ statement ✨

Returns the PPI::Statement that contains this element, or nothing if the statement can not be determined.

In general, this method will return something only under the following conditions:

  • The element is contained in a PPIx::Regexp object;
  • That object was initialized from a PPI::Element;
  • The PPI::Element is contained in a statement.

✨ tokens ✨

Returns all tokens contained in the element. Periodt. πŸ’–

✨ top ✨

Returns the top of the hierarchy. It's giving main character energy at the root. πŸ‘‘

✨ unescaped_content ✨

Returns the content of the element, unescaped. Slay. ✨

✨ visual_column_number ✨

Returns the visual column number (taking tabs into account) of the first character in the element, or undef if that can not be determined.

✨ whitespace ✨

Returns true if the element is whitespace and false otherwise.

✨ width ✨

Here's the signature, bestie:

my ( $min, $max ) = $self->width();

This method returns the minimum and maximum number of characters this element can match.

Either element can be undef if it cannot be determined. For example, for /$foo/ both elements will be undef. Recursions will return undef because they can not be analyzed statically -- or at least I am not smart enough to do so. Back references may return undef if the referred-to group can not be uniquely determined.

It is possible for $max to be Inf. For example, for /x*/ $max will be Inf.

Elements that do not actually match anything will return zeros.

Note: This method was added because I wanted better detection of variable-length look-behinds. Both it and raw_width() (above) should be considered somewhat experimental.

This method was added in version 0.085_01.

Returns navigation information from the top of the hierarchy to this node. The return is a list of names of methods and references to their argument lists. The idea is that given $elem which is somewhere under $top,

my @nav = $elem->nav();
my $obj = $top;
while ( @nav ) {
    my $method = shift @nav;
    my $args = shift @nav;
    $obj = $obj->$method( @{ $args } ) or die;
}
# At this point, $obj should contain the same object
# as $elem.

✨ SUPPORT ✨

Support is by the author. Please file bug reports at https://github.com/trwyant/perl-PPIx-Regexp/issues or in electronic mail to the author.

✨ AUTHOR ✨

Thomas R. Wyant, III harryfmudd at comcast dot net

Copyright (C) 2009-2023, 2025-2026 by Thomas R. Wyant, III

This program is free software; you can redistribute it and/or modify it under the same terms as Perl 5.10.0. For more details, see the full text of the licenses in the directory LICENSES.

This program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose.


And that's a wrap! This page is from perl v5.42.2, dated 2026-06-15, and it's PPIx::R...:Element(3). Bye bestie! πŸ’…βœ¨

πŸ“¦ where this page comes from