NAME
Struct::Path - Path for nested structures where path is also a structure
VERSION
Version 0.73
SYNOPSIS
use Struct::Path qw(slist spath spath_delta);
$s = [
0,
1,
{
'2a' => {
'2aa' => '2aav',
'2ab' => '2abv'
}
},
undef
];
@list = slist($s); # list paths and values
# @list == (
# [[0]], \0,
# [[1]], \1,
# [[2],{keys => ['2a']},{keys => ['2aa']}], \'2aav',
# [[2],{keys => ['2a']},{keys => ['2ab']}], \'2abv',
# [[3]], \undef
# )
@r = spath($s, [ [3,0,1] ]); # get refs to values
# @r == (\undef, \0, \1)
@r = spath($s, [ [2],{keys => ['2a']},{} ]); # another example
# @r == (\'2aav', \'2abv')
@r = spath($s, [ [2],{},{regs => [qr/^2a/]} ]); # using regular expressions
# @r == (\'2aav', \'2abv')
${$r[0]} =~ s/2a/blah-blah-/; # replace value
# $s->[2]{2a}{2aa} eq "blah-blah-av"
@d = spath_delta([[0],[4],[2]], [[0],[1],[3]]); # get steps delta
# @d == ([1],[3])
DESCRIPTION
Struct::Path provides functions to access/match/expand/list nested data structures.
Why existed Path modules is not enough? This module has no conflicts for paths like '/a/0/c', where 0
may be an array index or a key for hash (depends on passed structure). In some cases this is important, for example, when one need to define exact path in structure, but unable to validate it's schema or when structure itself doesn't yet exist (see "spath/Options/expand" for example).
EXPORT
Nothing is exported by default.
ADDRESSING SCHEME
Path is a list of 'steps', each represents nested level in structure.
Arrayref as a step stands for ARRAY in the structure and must contain desired indexes or be empty (means "all items"). Sequence for indexes is important and defines result sequence.
Hashref represent HASH in the structure and may contain keys keys
, regs
or be empty. keys
may contain list of desired keys, regs
must contain list of regular expressions. Empty hash or empty list for keys
means all keys. Sequence in keys
and regs
lists defines result sequence. keys
have higher priority than regs
.
Coderef step is a hook - subroutine which may filter and/or modify structure. Path as first argument and a stack (arrayref) of refs to traversed subsstructures as second passed to it when executed, $_ set to current substructure. Some true (match) value or false (doesn't match) value expected as output.
Sample:
$spath = [
[1,7], # first spep
{regs => qr/foo/} # second step
sub { exists $_->{bar} } # third step
];
See Struct::Path::PerlStyle if you're looking for human friendly path definition method.
SUBROUTINES
is_implicit_step
$implicit = is_implicit_step($step);
Returns true value if step contains hooks or specified 'all' items or regexp match.
slist
Returns list of paths and references to their values from structure.
@list = slist($struct, %opts)
Options
spath
Returns list of references from structure.
@list = spath($struct, $path, %opts)
Options
- assign
<value>
-
Assign provided value to substructures pointed by path.
- delete
<true|false>
-
Delete specified by path items from structure.
- deref
<true|false>
-
Dereference result items.
- expand
<"append"|true|false>
-
Expand structure if specified in path items doesn't exist. All newly created items initialized by
undef
. Arrays will be growed smoothly ifappend
as value used (experimental). - paths
<true|false>
-
Return path for each result.
- stack
<true|false>
-
Return stack of references to substructures.
- strict
<true|false>
-
Croak if at least one element, specified by path, absent in the structure.
All options are disabled (undef
) by default.
spath_delta
Returns delta for two passed paths. By delta means steps from the second path without beginning common steps for both.
@delta = spath_delta($path1, $path2)
LIMITATIONS
Struct::Path will fail on structures with loops in references.
No object oriented interface provided.
AUTHOR
Michael Samoglyadov, <mixas at cpan.org>
BUGS
Please report any bugs or feature requests to bug-struct-path at rt.cpan.org
, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Struct-Path. 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 Struct::Path
You can also look for information at:
RT: CPAN's request tracker (report bugs here)
AnnoCPAN: Annotated CPAN documentation
CPAN Ratings
Search CPAN
SEE ALSO
Data::Diver Data::DPath Data::DRef Data::Focus Data::Hierarchy Data::Nested Data::PathSimple Data::Reach Data::Spath JSON::Path MarpaX::xPathLike Sereal::Path Data::Find
Struct::Diff Struct::Path::PerlStyle
LICENSE AND COPYRIGHT
Copyright 2016,2017 Michael Samoglyadov.
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.