PPIx::XPath - an XPath implementation for the PDOM
version 2.02
use PPI; use PPIx::XPath; use Tree::XPathEngine; my $pdom = PPI::Document->new('some_code.pl'); my $xpath = Tree::XPathEngine->new(); my @subs = $xpath->findnodes('//Statement-Sub',$pdom); my @vars = $xpath->findnodes('//Token-Symbol',$pdom);
Deprecated interface, backward-compatible with PPIx::XPath version 1:
PPIx::XPath
use PPIx::XPath; my $pxp = PPIx::XPath->new("some_code.pl"); my @subs = $pxp->match("//Statement::Sub"); my $vars = $pxp->match("//Token::Symbol");
This module augments PPI's classes with the methods required by Tree::XPathEngine, allowing you to perform complex XPath matches against any PDOM tree.
See Tree::XPathEngine for details about its methods.
Each node in the PDOM is an element as seen by XPath
The name of the element is the class name of the node, minus the
initial PPI::, with :: replaced by -. That is:
PPI::
::
-
($xpath_name = substr($pdom_node->class,5)) =~ s/::/-/g;
Only "significant" nodes are seen by XPath
all scalar-valued accessors of PDOM nodes are visible as attributes
"here-docs" contents are not mapped
new
my $pxp = PPIx::XPath->new("some_code.pl"); my $pxp = PPIx::XPath->new($pdom);
Only useful for the backward-compatible, and deprecated, interface. Returns an instance of PPIx::XPath tied to the given document.
clean_xpath_expr
my $new_xpath_expr = $pxp->clean_xpath_expr($old_xpath_expr);
PPIx::XPath version 1.0.0 allowed the use of partial package names (like Token::Number) as element names: this collides with the axis specification of proper XPath. For this reason, in newer version of PPIx::XPath, the element name is the class name of the PDOM node, minus the initial PPI::, with :: replaced by - (like Token-Number).
Token::Number
Token-Number
This method replaces all occurrences of PPI package names in the given string with the new names.
match
my @subs = $pxp->match("//Statement::Sub"); my $vars = $pxp->match("//Token::Symbol");
Only useful for the backward-compatible, and deprecated, interface. From the document this instance was built against, returns the nodes that match the given XPath expression.
You should not use this method, you should call findnodes instead:
findnodes
my $xpath = Tree::XPathEngine->new(); my @subs = $xpath->findnodes('//Statement-Sub',$pdom); my @vars = $xpath->findnodes('//Token-Symbol',$pdom);
node ordering is slow, because I could not find a way in PPI to
compare two nodes for document order; suggestions are most welcome
PPI
Tree::XPathEngine
http://www.w3.org/TR/xpath (the XPath specification)
Dan Brook <cpan@broquaint.com> original author
Gianni Ceccarelli <dakkar@thenautilus.net> Tree::XPathEngine-based re-implementation
This software is copyright (c) 2016 by Gianni Ceccarelli <dakkar@thenautilus.net>.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install PPIx::XPath, copy and paste the appropriate command in to your terminal.
cpanm
cpanm PPIx::XPath
CPAN shell
perl -MCPAN -e shell install PPIx::XPath
For more information on module installation, please visit the detailed CPAN module installation guide.