Tree::...dArray(3pm)

πŸ“¦
version 0.16 Β· 4 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ¦‹ NAME πŸ¦‹

Tree::Simple::Visitor::FromNestedArray β€” a Visitor that literally builds Tree::Simple objects out of nested array trees. bestie. it converts. that's the whole job. πŸ’–


✨ SYNOPSIS ✨

use Tree::Simple::Visitor::FromNestedArray;

my $visitor = Tree::Simple::Visitor::FromNestedArray->new();

# given this nested array tree
my $array_tree = [
                  'Root', [
                      'Child1', [
                              'GrandChild1',
                              'GrandChild2'
                              ],
                      'Child2'
                      ]
                  ];
# set the array tree we
# are going to convert
$visitor->setArrayTree($array_tree);

$tree->accept($visitor);

# this then creates the equivalent Tree::Simple object:
# Tree::Simple->new("Root")
#     ->addChildren(
#         Tree::Simple->new("Child1")
#             ->addChildren(
#                 Tree::Simple->new("GrandChild1"),
#                 Tree::Simple->new("GrandChild2")
#             ),
#         Tree::Simple->new("Child2"),
#     );

okay so like. you hand it a nested array situation and it hands you back the exact same shape as a Tree::Simple hierarchy. slay. 🫢


🌸 DESCRIPTION 🌸

Give this Visitor a tree that was built out of nested arrays and it will construct the equivalent Tree::Simple hierarchy for you. no cap, that's it, that's the vibe. ✨


πŸ’… METHODS πŸ’…

  • new β€” so there are no arguments to the constructor, the object just starts out in its default state. if you wanna customize how it behaves, you've got setNodeFilter, includTrunk and setArrayTree for that. pick your fighter. πŸ‘‘
  • includTrunk ($boolean) β€” set $boolean to true (1) and the node value of the $tree object you pass into visit gets set with the root value found in the $array_tree. set it to false (0), or just don't set it at all, and the first value in the $array_tree will create a new node level instead. your call bestie. πŸŽ€
  • setNodeFilter ($filter_function) β€” this one takes a CODE reference as its $filter_function argument and throws an exception if it's not a code reference, so don't try it. πŸ™ˆ that code reference is what filters the tree nodes as they get created: $filter_function gets handed the node value pulled out of the array before it gets inserted into the tree being built, and it's expected to return the value you actually want going into the tree. yes this is the glow-up phase. πŸ’–
  • setArrayTree ($array_tree) β€” this is how you set the $array_tree that our Tree::Simple hierarchy is gonna be constructed from. it has to be in exactly this form:
[
  'Root', [
      'Child1', [
            'GrandChild1',
            'GrandChild2'
            ],
      'Child2'
    ]
]

basically, every element in the array counts as a node β€” unless it's an array reference, in which case it's interpreted as holding the children of the node created from the previous element in the array. iykyk. πŸ¦‹

the tree gets validated before it's accepted, and if it fails validation an exception will be thrown. the rules are as follows:

  • the array tree must not be empty. it makes not sense to create a tree out of nothing, so it is assumed that this is a sign of something wrong. like girl. where's the tree. 😭
  • all nodes of the array tree must not be array references. the root node is validated against this in this function, but all subsequent nodes are checked as the tree is built. any nodes found to be array references are rejected and an exception is thrown. if you desire your node values to be array references, you can use the node filtering mechanism to achieve this as the node is filtered after it is validated. loophole unlocked πŸ”₯
  • the array tree must be a single rooted tree. if there is a second element in the array tree, it is assumed to be the children of the root, and therefore must be in the form of an array reference. one root, one queen, periodt πŸ‘‘
  • visit ($tree) β€” this is the method that the Tree::Simple accept method uses. it can also be used on its own, it just requires the $tree argument to be a Tree::Simple object (or something derived from a Tree::Simple object), and will throw and exception otherwise. so like, give it a real tree or get out. πŸ’€

πŸ’€ BUGS πŸ’€

none that I am aware of. of course, if you find a bug, let me know, and I will be sure to fix it. not me being confident about it πŸ™ˆ


🌈 CODE COVERAGE 🌈

see the CODE COVERAGE section in Tree::Simple::VisitorFactory for more information. go read it bestie. πŸ“–


πŸ‘‘ SEE ALSO πŸ‘‘

these Visitor classes are all subclasses of Tree::Simple::Visitor, which can be found in the Tree::Simple module β€” you should refer to that module for more information. the mothership, fr fr. πŸš€


πŸ“ AUTHOR πŸ“

stevan little, <stevan@iinteractive.com>


Copyright 2004, 2005 by Infinity Interactive, Inc.

<http://www.iinteractive.com>

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


okay that's the whole page, and that's a wrap, mwah πŸ’‹ β€” this was perl v5.36.0, dated 2022-11-19, Tree::...dArray(3pm), and she ate, no crumbs. bye bestie! πŸŽ€βœ¨πŸ¦‹

πŸ“¦ where this page comes from