my $comm_tree = Tree::DAG_Node->new; #create the root node $comm_tree->name('CommentRoot'); my %noderef; #hash of node references, with PID as keys $noderef{0} = \$comm_tree; #replies to the story have PID==0 #### while ( my $hashref = $query->fetchrow_hashref() ) { my $tmpref = $noderef{ $$hashref{pid} }; #reference to the parent node my $cid = $$hashref{cid}; #CID will be used several times my $nodeone = Tree::DAG_Node->new( { name => $cid, #CID as node name mother => $$tmpref, #mother is the parent } ); $noderef{ $cid } = \$nodeone; #add this node ref to the hash $comm_hashref{ $cid } = $hashref; } #### $comm_tree->walk_down( { callback => sub { my $node = shift; my $tmp = $comm_hashref{ $node->name }; $$tmp{level} = $_[0]->{_depth}; #add a level key for html indentation push @comm_loop, $tmp unless ($node->name eq 'CommentRoot'); #for the HTML::Template loop }, _depth => 0, treename => 'CommentRoot' } ); $template->param(comment_loop => \@comm_loop);