Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
There's more than one way to do things.
 
PerlMonks

Recommended modules for Parent-Child trees?

by Hero Zzyzzx (Curate)
 | Log in | Create a new user | The Monastery Gates | Super Search | 
 | Seekers of Perl Wisdom | Meditations | PerlMonks Discussion | 
 | Obfuscation | Reviews | Cool Uses For Perl | Perl News | Q&A | Tutorials | 
 | Poetry | Recent Threads | Newest Nodes | Donate | What's New | 

on May 17, 2004 at 18:33 UTC ( #354049=perlquestion: print w/ replies, xml ) Need Help??
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?
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

[reply]

      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

[reply]

        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

[reply]
        Ever heard of SPOPS?

        lachoy actually wrote SPOPS :)

        -stvn
[reply]
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.
[reply]
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.

[reply]

      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

[reply]
[reply]
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
[reply]

      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

[reply]

Back to Seekers of Perl Wisdom


Login:
Password
remember me
What's my password?
Create A New User

Node Status
node history
Node Type: perlquestion [id://354049]
Approved by kvale
help
Community Ads
Chatterbox
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users
Others imbibing at the Monastery: (7)
GrandFather
wfsp
herveus
Spooty
Eyck
gnosti
im2
As of 2009-11-21 09:12 GMT
Sections
The Monastery Gates
Seekers of Perl Wisdom
Meditations
PerlMonks Discussion
Categorized Q&A
Tutorials
Obfuscated Code
Perl Poetry
Cool Uses for Perl
Perl News
Information
PerlMonks FAQ
Guide to the Monastery
What's New at PerlMonks
Voting/Experience System
Tutorials
Reviews
Library
Perl FAQs
Other Info Sources
Find Nodes
Nodes You Wrote
Super Search
List Nodes By Users
Newest Nodes
Recently Active Threads
Selected Best Nodes
Best Nodes
Worst Nodes
Saints in our Book
Leftovers
The St. Larry Wall Shrine
Offering Plate
Awards
Craft
Snippets Section
Code Catacombs
Quests
Editor Requests
Buy PerlMonks Gear
PerlMonks Merchandise
Planet Perl
Perlsphere
Use Perl
Perl.com
Perl 5 Wiki
Perl Jobs
Perl Mongers
Perl Directory
Perl documentation
CPAN
Random Node
Voting Booth

Future historians will find that the material characteristic of the current era is...

Aluminium
Plastic
Oil
Water
Carbon dioxide
Copper
Iron
Silicon
Salt
Uranium
Hydrogen
Other

Results (729 votes), past polls