++ed by:
1 non-PAUSE user
Author image Murat √únalan


Tree::Nary::Extended - Tree::Nary with substantial load/save from/to sql/hash


 use Tree::Nary::Extended;

   Tree::Nary->to_hash( $ntree )

   Tree::Nary->from_dbi_to_hash( $dbh, $table_name )

   Tree::Nary->from_dbi_to_tree( $dbh, $table_name )

   Tree::Nary->to_dbi( $tree, $dbh, $table_name )
   Tree::Nary->bread_crumb_trail( $node )

   Tree::Nary->depth( $node )

   Tree::Nary->type( $node )

   my $href_nodes = Tree::Nary->from_dbi( $dbh, $table_name );

   my $nary = Tree::Nary->from_hash( $href_nodes );

   Tree::Nary->depth( $nary->{children} );

   my $found = Tree::Nary->find( $nary, $Tree::Nary::IN_ORDER, $Tree::Nary::TRAVERSE_ALL, 'foobar' );

   my $aref_trail = Tree::Nary->bread_crumb_trail( $found );

   Tree::Nary->append( $found, new Tree::Nary( 'Dummy' ) );

   Tree::Nary->traverse( $nary, $Tree::Nary::PRE_ORDER, $Tree::Nary::TRAVERSE_ALL, -1, \&Tree::Nary::Extended::_callback_textout );

   my $href_nodes = Tree::Nary->to_hash( $nary );

   Tree::Nary->to_dbi( $href_nodes, $dbh, $table_name );


This module is not inheriting from Tree::Nary, but adds service methods to the namespace. So it an be seen as an "extended", but a bit "fishy" replacement for Tree::Nary. It loads Tree::Nary in the background and relies on some private innerts, which risks future compability. But noteworthy it works very well and does a good job for its author so far.


You should understand Tree::Nary (from CPAN) concepts first, before you proceed with this module.


from_hash( $href_all_nodes [, $root_id (default: -1) ] )

Creates a Nary Tree from a hash. The keys must be id, parent_id and data.

to_hash( $nary )

Produces a hash from Nary Tree. Returns a reference to it.

from_dbi_to_hash( $dbh, $table_name )

Reads a table from a DBI $dbh and produces a hash and returns the hashref to it.

from_dbi_to_tree( $dbh, $table_name )

Produces a Tree::Nary::Extended tree out of a DBI table.

Note: Read the DESCRIPTION from DBIx::Tree for the format of the sql table. The columns names must be "id", "parent_id" and "data"

to_dbi( $tree, $dbh, $table_name )

Write a Tree::Nary::Extended tree to a DBI table. See Note of from_dbi_to_tree for the format. Only the three node attributes id, parent_id, data are saved.

Note: The written sql table can be read with from_dbi_to_tree method.

Traverses the anchestrol tree (partent->to->parent) upwards and collects all parents to an array. A reference to it is returned. This can be used for building a "bread crumb trail" in website navigation.

depth( $node )

Returns the depth of a node, which is the distance to the root parent as an integer value.

type( $node )

Returns the node "type". 'root' if it is the root node. 'leaf' if it is a leaf node.


Tree::Nary heavily uses callbacks for doing something. Tree::Nary::Extended ships with some preconfectioned callbacks usefull for various things.

_callback_find_highest( $sref_highest_id )

Fills the scalarref with the highest node id number of the tree.

_callback_give_id( $sref_unique )

Overwrite the node ids with primary key ids (linear (+1) unique id).

_callback_convert_with_idkeys( $ref_arg )

Internal use. Cannot remember what is was.

_callback_find_id( $aref_args = [ $id ] )

Returns the first node with a given id. $aref_args[1] will contain the resulting node.

_callback_textout( $ref_args )

Dumps a textual printout of the node structure. Helps debugging.


tables( $dbh )

Returns a hash of tablenames from a DBI $dbh. Values are 1.


Because Tree::Nary isnt that clean OO, i had to use some dirty tricks on the innerts of the private hash objects. Thats why this module relies somehow on Tree::Nary Version 1.21 and may be broken on future updates of Tree::Nary.


None by default.


M. Ünalan, muenalan@cpan.org


Tree::Nary, DBIx::Tree

2 POD Errors

The following errors were encountered while parsing the POD:

Around line 540:

You forgot a '=back' before '=head1'

Around line 551:

Non-ASCII character seen before =encoding in 'Ünalan,'. Assuming ISO8859-1