[San-Diego-pm] Recurse through trees

Randal L. Schwartz merlyn at stonehenge.com
Fri Mar 17 07:21:23 PST 2006


>>>>> "Emile" == Emile Aben <emileaben at gmail.com> writes:

Emile> ---code---
Emile> [% tree = filelocation.logical_tree('string') %]
Emile> [% PERL %]
Emile>  my $tree = $stash->get('tree');
Emile>  html_node( $tree, '&nbsp;&nbsp;&nbsp;');
Emile>  $tree->traverse(sub{
Emile>     my($node)=@_;
Emile>     html_node( $node, '&nbsp;&nbsp;&nbsp;');
Emile>  });

Emile>  sub html_node {
Emile>     my ($node,$spacer) = @_;
Emile>     if ($node->isLeaf) {
Emile>         print '<span><div class="filelocation_tree_leave">';
Emile>     } else {
Emile>         print "<span title='some text about leaf-nodes here (".
Emile> $node->getNodeValue() .")'><div class='filelocation_tree'>";
Emile>     }
Emile>     print $spacer x ($node->getDepth());
Emile>     print '<img src="raw?file=L.png" style="padding-right:1px"/>' if
Emile> ($node->getDepth > -1);
Emile>     print $node->getNodeValue();
Emile>     print '</div>';
Emile>     print '</span>';
Emile>  }

Emile> [% END %]

[% spacer = "&nbsp;&nbsp;" %]
[% MACRO html_node_visitor(node) BLOCK %]
[% IF node.isLeaf %]
... etc etc
[% ELSE %]
... etc etc
[% END %]
[% GET spacer FOR 1..node.getDepth %]
[% node.getNodeValue %] etc etc
[% END %]
[% tree.traverse(html_node_visitor) %]

A "MACRO" is a code block!

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn at stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!


More information about the San-Diego-pm mailing list