TreePath - Simple Tree Path!
use TreePath; my $tp = TreePath->new( conf => $conf ); my $tree = $tp->tree; # All nodes are hash # The first is called 'root' my $root = $tp->root; # a node can have children my $children = $root->{children};
# for now there are two backends : DBIX and File $tp = TreePath->new( conf => 't/conf/treefromdbix.yml') # see t/conf/treepath.yml for hash structure $tp = TreePath->new( datas => $datas); also see t/01-tpath.t
$tree = $tp->tree;
# reload tree from backend $tree = $tp->reload;
$root = $tp->root; # $root and $tree->{1} are the same node This is the root node ( a simple hashref ) it has no parent. { '1' => { 'id' => '1', 'name' => '/', 'parent' => '0' } } $A = $tp->search( { name => 'A' } ); See the dump : { 'children' => [ { 'children' => 'ARRAY(0x293ce00)', 'id' => '3', 'name' => 'B', 'parent' => $VAR1 }, { 'children' => 'ARRAY(0x2fd69b0)', 'id' => '7', 'name' => 'F', 'parent' => $VAR1 } ], 'id' => '2', 'name' => 'A', 'parent' => { 'children' => [ $VAR1 ], 'id' => '1', 'name' => '/', 'parent' => '0' } } => 'parent' is a reference on root node and 'children' is an array containing 2 nodes => if nodes have 'type', it's possible to 'link' some 'type' of node on some others types For example, A:(type1), B:(type1, parent:A), C:(type2, parent:A) becomes : A:(type1, children:[B], children_type2[C]) Each children can be ordered : { name => A, position => 2}; It's also possible to block link with 'rules'. 'link' must be follow these rules to be applied.
# in scalar context return the first result my $E = $tp->search( { name => 'E' } ); # return all result in array context my @allE = $tp->search( { name => 'E' } ); # It is also possible to specify a particular field of a hash my $B = $tp->search( { name => 'B', 'parent.name' => 'A'} );
# Search a path in a tree # in scalar context return last node my $c = $tp->search_path('/A/B/C'); # in array context return found and not_found nodes my ($found, $not_found) = $tp->search_path('/A/B/X/D/E');
# dump whole tree print $tp->dump; # dump a node print $tp->dump($c);;
# return the number of nodes print $tp->count;
# return an arrayref of nodes my $nodes = $tp->traverse($node); # or use a function on each nodes $tp->traverse($node, \&function, $args);
# delete recursively all children and node $deleted = $tp->del($node); # delete several nodes at once @del = $tp->del($n1, $n2, ...);
# add the root $root = $tp->add(0, { name => '/'}); # add a node beneath the parent at the last position. $Z = $tp->add($parent, { name => 'Z' }); # or at given position $Z = $tp->add($parent, { name => 'Z', position => 2 });
# update node with somes datas $Z = $tp->update($node, { name => 'new_name' }); # it's possible to update position $X = $tp->update($node, { position => 2 });
# move a node as child of given parent $Z = $tp->move($Z, $X);
# Inserts a node beneath the parent before the given sibling. $Y = $tp->insert_before($Z, { name => 'Y' });
Daniel Brosseau, <dab at catapulse.org>
<dab at catapulse.org>
Please report any bugs or feature requests to bug-tpath at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=TreePath. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-tpath at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc TreePath
You can also look for information at:
RT: CPAN's request tracker (report bugs here)
http://rt.cpan.org/NoAuth/Bugs.html?Dist=TreePath
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/TreePath
CPAN Ratings
http://cpanratings.perl.org/d/TreePath
Search CPAN
http://search.cpan.org/dist/TreePath/
Copyright 2014 Daniel Brosseau.
This program is free software; you can redistribute it and/or modify it under the terms of the the Artistic License (2.0). You may obtain a copy of the full license at:
http://www.perlfoundation.org/artistic_license_2_0
Any use, modification, and distribution of the Standard or Modified Versions is governed by this Artistic License. By using, modifying or distributing the Package, you accept this license. Do not use, modify, or distribute the Package, if you do not accept this license.
If your Modified Version has been derived from a Modified Version made by someone other than you, you are nevertheless required to ensure that your Modified Version complies with the requirements of this license.
This license does not grant you the right to use any trademark, service mark, tradename, or logo of the Copyright Holder.
This license includes the non-exclusive, worldwide, free-of-charge patent license to make, have made, use, offer to sell, sell, import and otherwise transfer the Package with respect to any patent claims licensable by the Copyright Holder that are necessarily infringed by the Package. If you institute patent litigation (including a cross-claim or counterclaim) against any party alleging that the Package constitutes direct or contributory patent infringement, then this Artistic License to you shall terminate on the date that such litigation is filed.
Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
To install TreePath, copy and paste the appropriate command in to your terminal.
cpanm
cpanm TreePath
CPAN shell
perl -MCPAN -e shell install TreePath
For more information on module installation, please visit the detailed CPAN module installation guide.