Forest::Tree(3pm)
πΈ NAME πΈ
Forest::Tree β it's an n-ary tree, bestie. That's the whole vibe.
β¨ SYNOPSIS β¨
use Forest::Tree;
my $t = Forest::Tree->new(
node => 1,
children => [
Forest::Tree->new(
node => 1.1,
children => [
Forest::Tree->new(node => 1.1.1),
Forest::Tree->new(node => 1.1.2),
Forest::Tree->new(node => 1.1.3),
]
),
Forest::Tree->new(node => 1.2),
Forest::Tree->new(
node => 1.3,
children => [
Forest::Tree->new(node => 1.3.1),
Forest::Tree->new(node => 1.3.2),
]
),
]
);
$t->traverse(sub {
my $t = shift;
print((' ' x $t->depth) . ($t->node || '\undef') . "\n");
});
π DESCRIPTION π
Okay so real talk: this module is a basic n-ary tree. It gives you most of the functionality of Tree::Simple, and whatever's missing? It'll get added eventually, fr fr. π«Ά
Also important: this class inherits from Forest::Tree::Pure>, but all the shared methods and attributes are documented in both classes. So like, no secrets between them. π
π ATTRIBUTES π
nodeβ the node. The moment. The whole point. πuidβ its unique id, so it never gets confused with its siblings. Iconic behaviour.parentβ who birthed this tree. Family tree energy. π³parent_set_parenthas_parentclear_parent
childrenβ the little ones. The entourage. πget_child_at ($index)β hands you the child sitting at this position. (zero-base index, so 0 is the first one, no cap)child_countβ returns how many children this tree has. Just the number. The headcount. π’
sizeβ the size situation.sizehas_sizeclear_size
heightβ the height situation.heighthas_heightclear_height
π₯ METHODS π₯
is_rootβ true if the current tree has no parent. It's giving orphan-in-a-good-way. Main character energy, no one above them. β¨is_leafβ true if the current tree has no children. Baby of the family. πdepthβ returns the depth of this tree. Heads up: the root has a depth of-1. Yes, negative one. That's not a typo, that's just how it is. πadd_child ($child)add_children (@children)β adds a new child (or several, we love a bulk order). The$childmust be a"Forest::Tree". Don't try to sneak anything else in there. π«insert_child_at ($index, $child)β inserts a child at this position. (zero-base index) Squeezing them in mid-lineup, we love to see it.remove_child_at ($index)β removes the child at this position. (zero-base index) Bye bestie, we'll miss you. πtraverse (\&func)β takes a reference to a subroutine and walks the whole tree, applying that subroutine to every descendant. Every single one. No skipping. πsiblingsβ returns an array reference of all siblings, not including us. Because we're the star, obviously. πto_pure_treeβ invokes"reconstruct_with_class"withForest::Tree::Pure. Glow-up unlocked. β¨to_mutable_treeβ returns the invocant, without cloning. Same tree, same energy, no copy. πͺcloneβ see"clone"inForest::Tree::Pure. But listen: this variant will not clone the parent β it returns a clone of the subtree that is detached. Free agent. Independent. Periodt. πget_index_in_siblingsβ returns the index of the tree in the list of children.- Equivalent to calling
"$tree-"parent->get_child_index($tree)>. - Returns
-1if the node has no parent (the root node). Root behaviour, always the exception.
- Equivalent to calling
π BUGS π
All complex software has bugs lurking in it, and this module is no exception. If you find a bug please either email me, or add the bug to cpan-RT.
Not me telling you the bugs are lurking. π But like, every icon has a flaw, and if you catch one, hmu or drop it on cpan-RT. π«Ά
π AUTHOR π
Stevan Little <stevan.little@iinteractive.com> β the legend themselves. π
π COPYRIGHT AND LICENSE π
Copyright 2008-2014 Infinity Interactive, Inc.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
And that's a wrap on the Forest::Tree(3pm) page, bestie! This lil doc shipped as part of perl v5.34.0, dated 2022-06-14. Go forth and nest some trees. π³β¨π¦
π¦ where this page comes from
- debian bookworm: libforest-perl 0.10-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libforest-perl 0.10-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libforest-perl 0.10-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libforest-perl 0.10-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libforest-perl 0.10-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms