The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

TreePath - Simple Tree Path!

This module is at EXPERIMENTAL stage, so use with caution.

VERSION

SYNOPSIS

 use TreePath;

 my $tp = TreePath->new(  conf  => $conf  );

 # load datas from backend
 $tp->load;

 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};

 # or children from another source (if Source has a parent key)
 my $children = $root->{children_Comment};

 # also can has_many relationship. it's the same but without parent key
 my files = $root->{files};

SUBROUTINES/METHODS

new($method => $value)

 # for now there are two backends : DBIX and File
 $tp = TreePath->new( conf => 't/conf/treefromdbix.yml')
 # $tp = TreePath->new( conf => 't/conf/treefromfile.yml')

 # see t/conf/treepath.yml for hash structure
 $tp = TreePath->new( datas => $datas);

 also see t/01-tpath.t

load

 # The data load is not automatic
 $tp->load;

tree

 $tree = $tp->tree;

nodes

 $root = $tp->root;

 This is the root node ( a simple hashref )
 it has no parent.

               {
                'id' => '1',
                'name' => '/',
                'parent' => '0' # or ''
                'source' => 'Page'
              }

  All keys can be a relation to another source, as belong_to with DBIx like this :
               {
                'id' => '15',
                'name' => '/',
                'parent' => { 'Page_10' }
                'source' => 'Comment'
              }

  where 'Page' is the source of relationship and '10' the id. So Comment_15 is linked with Page_10.

  See the dump of a node example :


          {
            'id' => 2,
            'name' => 'A',
            'source' => 'Page'
            'parent' => {
                          'children' => [
                                          $VAR1
                                        ],
                          'children_Comment' => [
                                                  'HASH(0x52b6df8)',
                                                  'HASH(0x52aab28)',
                                                  'HASH(0x527daf8)'
                                                ],
                          'files' => [
                                       'HASH(0x43de208)',
                                       'HASH(0x52d8c80)'
                                     ],
                          'id' => 1,
                          'name' => '/',
                          'parent' => undef,
                          'source' => 'Page'
                        },
            'children' => [
                            {
                              'children' => 'ARRAY(0x52cbd70)',
                              'files' => 'ARRAY(0x52dbc30)',
                              'id' => 3,
                              'name' => 'B',
                              'parent' => $VAR1,
                              'source' => 'Page'
                            },
                            {
                              'children' => 'ARRAY(0x52dbb58)',
                              'children_Comment' => 'ARRAY(0x528ba38)',
                              'files' => 'ARRAY(0x52e45d8)',
                              'id' => 7,
                              'name' => "\x{2665}",
                              'parent' => $VAR1,
                              'source' => 'Page'
                            }
                          ],
            'children_Comment' => [
                                    {
                                      'id' => 3,
                                      'page' => $VAR1,
                                      'source' => 'Comment'
                                    },
                                    {
                                      'id' => 4,
                                      'page' => $VAR1,
                                      'source' => 'Comment'
                                    }
                                  ],
            'files' => [],
          }


    => 'parent' is a reference to root node and 'children' is an array containing 2 Pages, children_Comment has 2 Comments

search (hashref)

 # in scalar context return the first result
 my $E = $tp->search( { name => 'E', source => 'Page' } );

 # return all result in array context
 my @allE = $tp->search( { name => 'E', source => 'Page' } );

 # It is also possible to specify a particular field of a hash
 my $B = $tp->search( { name => 'B', source => 'Page', 'parent.name' => 'A'} );

search_path (SOURCE, PATH)

 # Search a path in a tree
 # in scalar context return last node (last page in this example)
 my $c = $tp->search_path('Page', '/A/B/C');

 # in array context return found and not_found nodes
 my ($found, $not_found) = $tp->search_path('Page', '/A/B/X/D/E');

dump

 # dump whole tree
 print $tp->dump;

 # dump a node
 print $tp->dump($c);;

count

 # return the number of nodes
 print $tp->count;

traverse ($node, [\&function], [$args])

 # return an arrayref of nodes
 my $nodes = $tp->traverse($node);

 # or use a function on each nodes
 $tp->traverse($node, \&function, $args);

del ($node)

 # delete recursively all children and node
 $deleted = $tp->del($node);

 # delete several nodes at once
 @del = $tp->del($n1, $n2, ...);

add ($node, [ $parent ])

 # add the root
 $root = $tp->add({ name => '/', source => 'Node', id => 123 });

 # add a node beneath the parent.
 $Z = $tp->add({ name => 'Z', source => 'Node' }, $parent);

update ($node, $datas)

 # update node with somes datas
 $Z = $tp->update($node, { name => 'new_name' });

move ($node, $parent)

 # move a node as child of given parent
 $Z = $tp->move($Z, $X);

AUTHOR

Daniel Brosseau, <dab at catapulse.org>

BUGS

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.

SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc TreePath

You can also look for information at:

ACKNOWLEDGEMENTS

LICENSE AND COPYRIGHT

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.