$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);