I built a yahoo style link managing system.
I need to be able to print a dropdown box with the (sorted) directory tree. I am using BerkeleyDB.pm as the database, i came up with some code that works but its ptretty ugly -- hopefully somebody could help me make it more elegant.
sub BuildCatList { my @cat_list = (); my $cats = &main::GetDB('cats'); # a ref to a tied BerkeleyDB hash local *get_next_lvl = sub { my ($cat_id, $depth) = @_; my %cache = (); my $cat_info = Category->new($cat_id); defined $cat_info or warn "no such cat '$cat_id'" && return undef; foreach my $id (@{$cat_info->field('sub_cats')}) { my $subcat_info = Category->new($id); defined $subcat_info or warn "no such cat '$id'" && next; $cache{$id} = $subcat_info->field('name'); } foreach my $id (sort {$cache{$a} cmp $cache{$b}} keys %cache) { push (@cat_list, { depth => $depth , id => $id , name => '. . ' x $depth . $cache{$id} }); defined get_next_lvl($id, $depth+1) or warn "undef ret" && return undef; } return 1; }; push(@cat_list, {id => 'top', name => 'Top', depth => 0} ); defined get_next_lvl('top', 1) or warn "undef ret" && return undef; $cats->{'~cat_list'} = &main::nfreeze( \@cat_list ); return 1; }
$cats->{'~cat_list'} is then passed into HTML::Template.

----------------------------------
Here's some of Google's Wizdom (Google Glossary):
Stupid: Losing $25 on the game and $25 on the instant replay.


In reply to Printing a dir tree by ClickToViewInfo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.