File Coverage

blib/lib/Tree/Create/Callback.pm
Criterion Covered Total %
statement 24 24 100.0
branch 1 2 50.0
condition n/a
subroutine 2 2 100.0
pod 1 1 100.0
total 28 29 96.5


line stmt bran cond sub pod time code
1              
2             our $DATE = '2016-04-01'; # DATE
3             our $VERSION = '0.03'; # VERSION
4              
5             use Exporter qw(import);
6 2     2   72012 our @EXPORT_OK = qw(create_tree_using_callback);
  2         10  
  2         354  
7              
8             my $callback = shift;
9              
10 2     2 1 97 # create the root node
11             my $level = 0;
12             my ($root, $num_children) = $callback->(undef, $level, 0);
13 2         3 my @parents = ($root);
14 2         6 my @nums_children = ($num_children);
15 2         25 while (@parents) {
16 2         3 $level++;
17 2         6 my @new_parents;
18 7         8 my @new_nums_children;
19 7         8 for my $i (0..$#parents) {
20             my $node;
21 7         17 my @children;
22 16         52 for my $j (0..$nums_children[$i]-1) {
23             ($node, $num_children) = $callback->($parents[$i], $level, $j);
24 16         32 if ($node) {
25 14         21 # connect child to parent
26 14 50       73 $node->parent($parents[$i]);
27              
28 14         25 push @children, $node;
29             push @new_parents, $node;
30 14         57 push @new_nums_children, $num_children;
31 14         14 }
32 14         18 }
33             # connect parent to its children
34             $parents[$i]->children(\@children);
35             }
36 16         26 @parents = @new_parents;
37             @nums_children = @new_nums_children;
38 7         34 }
39 7         14 $root;
40             }
41 2         5  
42             1;
43             # ABSTRACT: Create tree object by using a callback
44              
45              
46             =pod
47              
48             =encoding UTF-8
49              
50             =head1 NAME
51              
52             Tree::Create::Callback - Create tree object by using a callback
53              
54             =head1 VERSION
55              
56             This document describes version 0.03 of Tree::Create::Callback (from Perl distribution Tree-Create-Callback), released on 2016-04-01.
57              
58             =head1 SYNOPSIS
59              
60             use Tree::Create::Callback qw(create_tree_using_callback);
61             use Tree::Object::Hash; # for nodes
62              
63             # create a tree of height 4 containing 1 + 2 + 4 + 8 nodes
64             my $tree = create_tree_using_callback(
65             sub {
66             my ($parent, $level, $seniority) = @_;
67             # we should return ($node, $num_children)
68             return (Tree::Object::Hash->new, $level >= 3 ? 0:2);
69             }
70             );
71              
72             =head1 DESCRIPTION
73              
74             Building a tree manually can be tedious: you have to connect the parent and
75             the children nodes together:
76              
77             my $root = My::TreeNode->new(...);
78             my $child1 = My::TreeNode->new(...);
79             my $child2 = My::TreeNode->new(...);
80              
81             $root->children([$child1, $child2]);
82             $child1->parent($root);
83             $child2->parent($root);
84              
85             my $grandchild1 = My::Class->new(...);
86             ...
87              
88             This module provides a convenience function to build a tree of objects in a
89             single command. You supply a callback to create node and the function will
90             connect the parent and children nodes for you.
91              
92             The callback is called with these arguments:
93              
94             ($parent, $level, $seniority)
95              
96             where C<$parent> is the parent node object (or undef if creating the root node,
97             which is the first time the callback is called), C<$level> indicates the current
98             depth of the tree (starting from 0 for the root node, then 1 for the root's
99             children, then 2 for their children, and so on). You can use this argument to
100             know where to stop creating nodes. C<$seniority> indicates the position of the
101             node against its sibling (0 means the node is the first child of its parent, 1
102             means the second, and so on). You can use this argument to perhaps customize the
103             node according to its sibling order.
104              
105             The callback should return a list:
106              
107             ($node, $num_children)
108              
109             where C<$node> is the created node object (the object can be of any class but it
110             must respond to C<parent> and C<children>, see L<Role::TinyCommons::Tree::Node>
111             for more details on the requirement), C<$num_children> is an integer that
112             specifies the number of children that this node should have (0 means this node
113             is to be a leaf node). The children will be created when the function calls the
114             callback again later for each child node.
115              
116             =head1 FUNCTIONS
117              
118             =head2 create_tree_using_callback($cb) => obj
119              
120             =head1 HOMEPAGE
121              
122             Please visit the project's homepage at L<https://metacpan.org/release/Tree-Create-Callback>.
123              
124             =head1 SOURCE
125              
126             Source repository is at L<https://github.com/perlancar/perl-Tree-Create-Callback>.
127              
128             =head1 BUGS
129              
130             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Tree-Create-Callback>
131              
132             When submitting a bug or request, please include a test-file or a
133             patch to an existing test-file that illustrates the bug or desired
134             feature.
135              
136             =head1 SEE ALSO
137              
138             Other C<Tree::Create::*> modules, e.g. L<Tree::Create::Size>.
139              
140             Other ways to create tree: L<Tree::FromStruct>, L<Tree::FromText>,
141             L<Tree::FromTextLines>.
142              
143             =head1 AUTHOR
144              
145             perlancar <perlancar@cpan.org>
146              
147             =head1 COPYRIGHT AND LICENSE
148              
149             This software is copyright (c) 2016 by perlancar@cpan.org.
150              
151             This is free software; you can redistribute it and/or modify it under
152             the same terms as the Perl 5 programming language system itself.
153              
154             =cut