NAME
Data::DPath - DPath is not XPath!
SYNOPSIS
use Data::DPath 'dpath';
my $data = {
AAA => { BBB => { CCC => [ qw/ XXX YYY ZZZ / ] },
RRR => { CCC => [ qw/ RR1 RR2 RR3 / ] },
DDD => { EEE => [ qw/ uuu vvv www / ] },
},
};
# Perl 5.8 style
@resultlist = dpath('/AAA/*/CCC')->match($data); # ( ['XXX', 'YYY', 'ZZZ'], [ 'RR1', 'RR2', 'RR3' ] )
# Perl 5.10 style using overloaded smartmatch operator
$resultlist = $data ~~ dpath '/AAA/*/CCC'; # [ ['XXX', 'YYY', 'ZZZ'], [ 'RR1', 'RR2', 'RR3' ] ]
Not that the match()
function returns an array but the overloaded ~~
operator returns an array reference (that's a limitation of overloading).
Various other example paths from t/data_dpath.t
(not neccessarily fitting to above data structure):
$data ~~ dpath '/AAA/*/CCC'
$data ~~ dpath '/AAA/BBB/CCC/../..' # parents (..)
$data ~~ dpath '//AAA' # anywhere (//)
$data ~~ dpath '//AAA/*' # anywhere + anystep
$data ~~ dpath '//AAA/*[size == 3]' # filter by arrays/hash size
$data ~~ dpath '//AAA/*[size != 3]' # filter by arrays/hash size
$data ~~ dpath '/"EE/E"/CCC' # quote strange keys
$data ~~ dpath '/AAA/BBB/CCC/*[1]' # filter by array index
$data ~~ dpath '/AAA/BBB/CCC/*[ idx == 1 ]' # same, filter by array index
$data ~~ dpath '//AAA/BBB/*[key eq "CCC"]' # filter by exact keys
$data ~~ dpath '//AAA/*[ key =~ /CC/ ]' # filter by regex matching keys
$data ~~ dpath '//CCC/*[ value eq "RR2" ]' # filter by values of hashes
You can get references into the $data
data structure by using dpathr
:
$data ~~ dpathr '/AAA/*/CCC'
$data ~~ dpathr '/AAA/BBB/CCC/../..'
$data ~~ dpathr '//AAA'
# etc.
See full details t/data_dpath.t
.
ABOUT
With this module you can address points in a datastructure by describing a "path" to it using hash keys, array indexes or some wildcard-like steps. It is inspired by XPath but differs from it.
Why not XPath?
XPath is for XML. DPath is for data structures, with a stronger Perl focus.
Although XML documents are data structures, they are special.
Elements in XML always have an order which is in contrast to hash keys in Perl.
XML elements names on same level can be repeated, not so in hashes.
XML element names are more limited than arbitrary strange hash keys.
XML elements can have attributes and those can be addressed by XPath; Perl data structures do not need this. On the other side, data structures in Perl can contain blessed elements, DPath can address this.
XML has namespaces, data structures have not.
Arrays starting with index 1 as in XPath would be confusing to read for data structures.
DPath allows filter expressions that are in fact just Perl expressions not an own sub language as in XPath.
Comparison with Data::Path
There is a similar approach on CPAN, Data::Path. Here is a comparison matrix between Data::Path and Data::DPath.
(Warning: alpha grade comparison ahead, not yet fully verified, only evaluated by reading the source. Speed comparison not really benchmarked.)
---------------------------------------------------------------------
Criteria Data::Path Data::DPath
---------------------------------------------------------------------
real XPath syntax no no
---------------------------------------------------------------------
allow strange, YES YES
non-xml but
perl-like although
hash keys limited,
see next
---------------------------------------------------------------------
allows special no YES
chars of own
path syntax in you can quoting everything
hash keys
("/[]|*.")
---------------------------------------------------------------------
call subs in YES no
data structure,
like:
/method()
---------------------------------------------------------------------
callbacks on YES no
not found keys
---------------------------------------------------------------------
element "//" no YES
for "ANYWHERE"
(//foo/bar)
---------------------------------------------------------------------
element "." no YES
for "NOSTEP" or
"actual position"
(/.[filter expr])
---------------------------------------------------------------------
element ".." no YES
for "PARENT"
(//foo/..)
---------------------------------------------------------------------
element "*" no YES
for "ANYSTEP" or
"all subelements"
(/foo/*)
---------------------------------------------------------------------
array access YES YES
like /foo[4]
although including negative indexes
limited and whitespace awareness
---------------------------------------------------------------------
complex no YES
filter expressions
like full Perl expressions
/foo[size == 3] or plus sugar functions,
/.[isa("Foo::Bar")] some minor limitations
(no "/" allowed in expr)
---------------------------------------------------------------------
works with YES YES
blessed subelements
---------------------------------------------------------------------
arrays start YES YES
with index 0
(in contrast
to 1 as in XPath)
---------------------------------------------------------------------
handling of croak RETURN EMPTY
not matching
paths but can be
overwritten
as callback
---------------------------------------------------------------------
usage sugar none overloaded '~~' operator
---------------------------------------------------------------------
Speed FAST slow
- raw Perl - based on Moose
- considered fast - slow startup time
- probably comparable
speed with expression
that Data::Path handles
- slow on fuzzy paths,
eg. with many "//" in it
---------------------------------------------------------------------
Perl Versions 5.6 - 5.11 5.10 .. 5.11
---------------------------------------------------------------------
Install chance 100% 40%
(http://deps (for Perl 5.10+)
.cpantesters
.org)
---------------------------------------------------------------------
Summary
Generally Data::Path is for simpler use cases but does not suffer from surrounding meta problems: it has few dependencies, is fast and works on practically every Perl version.
Whereas Data::DPath provides more XPath like features but regarding speed and Perl compatibility suffers a bit from it's new-school dependency stack: Perl 5.10+, Moose and MooseX::Declare.
Security warning
Watch out! This module eval
s parts of provided dpaths (filter expressions). Don't use it if you don't trust your paths.
Maybe I will provide a switch-off-complex-filtering option in a later version. Tell me what you think or when you need it.
FUNCTIONS
dpath( $path )
Meant as the front end function for everyday use of Data::DPath. It takes a path string and returns a Data::DPath::Path
object on which the match method can be called with data structures and the operator ~~
is overloaded.
The function is prototyped to take exactly one argument so that you can omit the parens in many cases.
See SYNOPSIS.
dpathr( $dpath )
Same as dpath
but toggles that results are references to the matched points in the data structure.
METHODS
match( $data, $path )
Returns an array of all values in $data
that match the $path
.
get_context( $path )
Returns a Data::DPath::Context
object that matches the path and can be used to incrementally dig into it.
OPERATOR
~~
Does a match
of a dpath against a data structure.
Due to the matching nature of DPath the operator ~~
should make your code more readable.
THE DPATH LANGUAGE
Synopsis
/AAA/BBB/CCC
/AAA/*/CCC
//CCC/*
//CCC/*[2]
//CCC/*[size == 3]
//CCC/*[size != 3]
/"EE/E"/CCC
/AAA/BBB/CCC/*[1]
/AAA/BBB/CCC/*[ idx == 1 ]
//AAA/BBB/*[key eq "CCC"]
//AAA/*[ key =~ /CC/ ]
//CCC/*[value eq "RR2"]
//.[ size == 4 ]
/.[ isa("Funky::Stuff") ]/.[ size == 5 ]/.[ reftype eq "ARRAY" ]
Modeled on XPath
The basic idea is that of XPath: define a way through a datastructure and allow some funky ways to describe fuzzy ways. The syntax is roughly looking like XPath but in fact have not much more in common.
Some wording
I call the whole path a, well, path.
It consists of single (path) steps that are divided by the path separator /
.
Each step can have a filter appended in brackets []
that narrows down the matching set of results.
Additional functions provided inside the filters are called, well, filter functions.
Each step has a set of point
s relative to the set of points before this step, all starting at the root of the data structure.
Special elements
//
Anchors to any hash or array inside the data structure below the currently found points (or the root).
Typically used at the start of a path to anchor the path anywhere instead of only the root node:
//FOO/BAR
but can also happen inside paths to skip middle parts:
/AAA/BBB//FARAWAY
This allows any way between
BBB
andFARAWAY
.*
Matches one step of any value relative to the current points (or the root). This step might be any hash key or all values of an array in the step before.
..
Matches the parent element relative to the current points.
.
A "no step". This keeps passively at the current points, but allows incrementally attaching filters to points or to otherwise hard to reach steps, like the top root element
/
. So you can do:/.[ FILTER ]
or chain filters:
/AAA/BBB/.[ filter1 ]/.[ filter2 ]/.[ filter3 ]
This way you do not need to stuff many filters together into one huge killer expression and can more easily maintain them.
See Filters for more details on filters.
If you need those special elements to be not special but as key names, just quote them:
/"*"/ /"*"[ filter ]/ /".."/ /".."[ filter ]/ /"."/ /"."[ filter ]/ /"//"/ /"//"[ filter ]/
Difference between /step[filter]
vs. /step/.[filter]
vs. /step/*[filter]
The filter applies to the matched points of the step to which it is applied, therefore /part[filter]
is the normal form. The "no step" stays on the current step, therefore /part/.[filter]
should be the same as /part[filter]
.
It's different for /part/*[filter]
. That means, take all the sub elements ("*") after "step" and apply the filter to those. The most common use is to take "all" elements of an array and chose one element via index: /step/*[4]/
. This takes the fifth element of the array inside "step".
Filters
Filters are conditions in brackets. They apply to all elements that are directly found by the path part to which the filter is appended.
Internally the filter condition is part of a grep
construct (exception: single integers, they choose array elements). See below.
Examples:
/FOO/*[2]/
-
A single integer as filter means choose an element from an array. So the
*
finds all subelements that follow current stepFOO
and the[2]
reduces them to only the third element (index starts at 0). /FOO/*[ idx == 2 ]/
-
The
*
is a step that matches all elements afterFOO
, but with the filter only those elements are chosen that are of index 2. This is actually the same as just/FOO/*[2]
. /FOO/*[key eq "CCC"]
-
In all elements after
FOO
it matches only those elements whose key is "CCC". /FOO/*[key =~ /CCC/ ]
-
In all elements after step
FOO
it matches only those elements whose key matches the regex/CCC/
. It is actually just Perl code inside the filter which works in a grep{}-like context. //FOO/*[value eq "RR2"]
-
Find elements below
FOO
that have the valueRR2
.Combine this with the parent step
..
: //FOO/*[value eq "RR2"]/..
-
Find such an element below
FOO
where an element with valueRR2
is contained. //FOO[size >= 3]
-
Find
FOO
elements that are arrays or hashes of size 3 or bigger.
Filter functions
The filter condition is internally part of a grep
over the current subset of values. So you can write any condition like in a grep and also use the variable $_
.
Additional filter functions are available that are usually written to use $_ by default. See Data::DPath::Filters for complete list of available filter functions.
Here are some of them:
- idx
-
Returns the current index inside array elements.
Please note that the current matching elements might not be in a defined order if resulting from anything else than arrays.
- size
-
Returns the size of the current element. If it is a arrayref it returns number of elements, if it's a hashref it returns number of keys, if it's a scalar it returns 1, everything else returns -1.
- key
-
Returns the key of the current element if it is a hashref. Else it returns undef.
- value
-
Returns the value of the current element. If it is a hashref return the value. If a scalar return the scalar. Else return undef.
Special characters
There are 4 special characters: the slash /
, paired brackets []
, the double-quote "
and the backslash \
. They are needed and explained in a logical order.
Path parts are divided by the slash </>.
A path part can be extended by a filter with appending an expression in brackets []
.
To contain slashes in hash keys, they can be surrounded by double quotes "
.
To contain double-quotes in hash keys they can be escaped with backslash \
.
Backslashes in path parts don't need to be escaped, except before escaped quotes (but see below on Backslash handling).
Filters of parts are already sufficiently divided by the brackets []
. There is no need to handle special characters in them, not even double-quotes. The filter expression just needs to be balanced on the brackets.
So this is the order how to create paths:
- 1. backslash double-quotes that are part of the key
- 2. put double-quotes around the resulting key
- 3. append the filter expression after the key
- 4. separate several path parts with slashes
Backslash handling
If you know backslash in Perl strings, skip this paragraph, it should be the same.
It is somewhat difficult to create a backslash directly before a quoted double-quote.
Inside the DPath language the typical backslash rules of apply that you already know from Perl single quoted strings. The challenge is to specify such strings inside Perl programs where another layer of this backslashing applies.
Without quotes it's all easy. Both a single backslash \
and a double backslash \\
get evaluated to a single backslash \
.
Extreme edge case by example: To specify a plain hash key like this:
"EE\E5\"
where the quotes are part of the key, you need to escape the quotes and the backslash:
\"EE\E5\\\"
Now put quotes around that to use it as DPath hash key:
"\"EE\E5\\\""
and if you specify this in a Perl program you need to additionally escape the backslashes (i.e., double their count):
"\"EE\E5\\\\\\""
As you can see, strangely, this backslash escaping is only needed on backslashes that are not standing alone. The first backslash before the first escaped double-quote is ok to be a single backslash.
All strange, isn't it? At least it's (hopefully) consistent with something you know (Perl, Shell, etc.).
INTERNAL METHODS
To make pod coverage happy.
build_dpath
Prepares internal attributes for dpath.
build_dpathr
Prepares internal attributes for dpathr.
AUTHOR
Steffen Schwigon, <schwigon at cpan.org>
CONTRIBUTIONS
Florian Ragwitz (cleaner exports, $_ scoping, general perl consultant)
BUGS
Please report any bugs or feature requests to bug-data-dpath at rt.cpan.org
, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Data-DPath. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Data::DPath
You can also look for information at:
RT: CPAN's request tracker
AnnoCPAN: Annotated CPAN documentation
CPAN Ratings
Search CPAN
REPOSITORY
The public repository is hosted on github:
git clone git://github.com/renormalist/data-dpath.git
COPYRIGHT & LICENSE
Copyright 2008,2009 Steffen Schwigon.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
SEE ALSO
There are other modules on CPAN which are related to finding elements in data structures.