Tree::...tedHash(3pm)

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

πŸ’– NAME πŸ’–

okay so bestie, meet Tree::Simple::Visitor::FromNestedHash β€” it's a Visitor, and its entire personality is: take a tree made of nested hashes and build the equivalent Tree::Simple object out of it. No cap. That's the job. ✨

(she lives in the perl docs, section 3pm, in case you were wondering 🫢)


🌸 SYNOPSIS 🌸

use Tree::Simple::Visitor::FromNestedHash;

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

# given this nested hash tree
my $hash_tree = {
              Root => {
                      Child1 => {
                              GrandChild1 => {},
                              GrandChild2 => {}
                              },
                      Child2 => {}
                      }
              };

# set the array tree we
# are going to convert
$visitor->setHashTree($hash_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"),
#     );

like… she literally eats a hash and gives you back a whole family tree πŸ’€βœ¨


πŸ¦‹ DESCRIPTION πŸ¦‹

so here's the vibe: you've got a tree that was built out of nested hashes, right? This Visitor takes that and creates the equivalent Tree::Simple hierarchy. it's giving "translation service but make it trees". 🌳➑️🌳


✨ METHODS ✨

new

there are no arguments to the constructor, bestie β€” the object just spawns in its default state, main character energy from day one. πŸ’… You can use the setNodeFilter methods to customize its behavior.

setNodeFilter ($filter_function)

This method accepts a CODE reference as its $filter_function argument and throws an exception if it is not a code reference. πŸ™ˆ

This code reference is used to filter the tree nodes as they are created. The $filter_function is passed the node value extracted from the hash prior to it being inserted into the tree being built, and it's expected to return the value you actually want included in the tree. glow-up for your node values, fr fr ✨

setHashTree ($hash_tree)

This method is used to set the $hash_tree that our Tree::Simple hierarchy will be constructed from. It must be in the following form:

{
  Root => {
        Child1 => {
                GrandChild1 => {},
                GrandChild2 => {}
                },
        Child2 => {}
        }
}

Basically: each key in the hash is considered a node. Values are ignored β€” unless it's a hash reference with at least one key in it, in which case it's interpreted as containing the children of the node created from the key. πŸ‘‘

The tree is validated prior to being accepted. If it fails validation, an exception will be thrown. The rules are as follows:

The hash tree must not be empty.

It makes no sense to create a tree out of nothing, so it is assumed that this is a sign of something wrong. 🚩

The hash tree must be a single rooted tree.

The hash tree should have only one key in the first level. If it has more than one, then it is not a single rooted tree. one root only, bestie β€” this is not a polycule πŸ’€

NOTE: Hash keys are sorted ascii-betically before being added to the tree β€” this results in a somewhat more predictable hierarchy. πŸ“šβœ¨

visit ($tree)

This is the method that is used by the Tree::Simple accept method. It can also be used on its own. It requires the $tree argument to be a Tree::Simple object (or derived from a Tree::Simple object), and will throw an exception otherwise. no sneaking in random objects, she will clock you πŸ’…


🧚 Repository 🧚

<https://github.com/ronsavage/Tree-Simple-VisitorFactory>


🌈 SUPPORT 🌈

Bugs should be reported via the CPAN bug tracker at

<https://github.com/ronsavage/Tree-Simple-VisitorFactory/issues>

if you find a bug, tell the tracker, don't suffer in silence bestie 🫢


πŸ’œ CODE COVERAGE πŸ’œ

See the CODE COVERAGE section in Tree::Simple::VisitorFactory for more information.


πŸŽ€ 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. iykyk, go read the parent πŸ’…


πŸ‘‘ 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.


and that's the whole page, bestie!! πŸ₯³ this one came from perl v5.36.0, dated 2022-11-19 β€” iconic, timeless, ate. πŸŒΈπŸ¦‹πŸ’– periodt.

πŸ“¦ where this page comes from