http://www.perlmonks.org?node_id=354049

Hero Zzyzzx has asked for the wisdom of the Perl Monks concerning the following question:

I'm looking to implement a DBI-backed parent-child tree in perl, and I'm NOT looking to reinvent the wheel, AGAIN.

Do any of you have any favorites on the CPAN? I'm looking to do child/ancestor/sibling queries and manipulations, and have some kind of help on output (like figuring out what "level" an item is at), though I'll be using HTML::Template for display and CGI::Application for "flow".

A few I've been looking at:

I'm sure I'm missing some that are worthy of consideration. What have you used? Likes? Hates?

Thanks!

-Any sufficiently advanced technology is
indistinguishable from doubletalk.

My Biz

  • Comment on Recommended modules for Parent-Child trees?

Replies are listed 'Best First'.
Re: Recommended modules for Parent-Child trees?
by tilly (Archbishop) on May 17, 2004 at 20:06 UTC
    If you wish the nodes to be stored in a database, then I'd suggest reading (and understanding) Nested Set Trees. In the common case of insert occasionally, query often, they are far more efficient than the usual adjacency model that people think of.

    Unfortunately I don't know of a module that automates generating them in Perl, and I'm not entirely sure what such a module should look like. (Which may be why I don't know of one...) So you'd have to do some reinvention, but at least you'd reinvent a good version.

      Got it. Nested set trees are very, very cool. Wow! Thanks for that. The efficiency with which you could select siblings/parents/ancestors and subtrees is crazy, recursive queries always bugged me (I've implemented parent-child relationships before).

      It's a bit hard to wrap my head around how to get it into code, but I can probably come up with something given the SQL in the article.

      I need to figure out now if speed of execution outweighs speed of development, and figure out where I'm going to spend my time.

      -Any sufficiently advanced technology is
      indistinguishable from doubletalk.

      My Biz

Re: Recommended modules for Parent-Child trees?
by stvn (Monsignor) on May 17, 2004 at 20:19 UTC
    Hero Zzyzzx,

    As lachoy has already said, you can use Tree::Simple for this. Of course, I wrote it so I favor it heavily. As for a persistence layer, I have actually implemented several different ones for Tree::Simple over the years, only one of which has been packaged for on CPAN right now, it is called Tree::Parser. Tree::Parser is more appropriate for serializing Tree::Simple heirarchies to files, but could be made to use a DBI backend by writing a custom filter.

    If you would like to tell me a little more about your application, I can possibly help you out by giving you some persistence code I have already written (if it is appropriate to your needs). At the very least, I might be able to point you in the right direction.

    -stvn

      And you can use DBD::Mock to test it! :-) (Somewhat of an inside joke: stvn recently submitted some patches to DBD::Mock...)

      Chris
      M-x auto-bs-mode

Re: Recommended modules for Parent-Child trees?
by perrin (Chancellor) on May 17, 2004 at 19:19 UTC
    Tree::DAG_Node worked great for me, but it doesn't handle the DBI stuff automatically. I wrote a routine that walked the tree (easy to do with the provided API) and performed the SQL statements required to save each node in my database.
Re: Recommended modules for Parent-Child trees?
by lachoy (Parson) on May 17, 2004 at 18:52 UTC

    While not DBI-backed Tree::Simple looks pretty useful. Even better: its docs have a brief comments on a number of other tree modules on CPAN.

    Chris
    M-x auto-bs-mode

      Very interesting module, it does pretty much everything I'd like EXCEPT give me a persistence layer. Got any ideas? Ever heard of SPOPS? :)

      -Any sufficiently advanced technology is
      indistinguishable from doubletalk.

      My Biz

        I bet it wouldn't be that tough to use the included visitor interface to serialize the tree. But you can ask stvn to see if he's already done something like this.

        Chris
        M-x auto-bs-mode

        Ever heard of SPOPS?

        lachoy actually wrote SPOPS :)

        -stvn