line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Code::Includable::Tree::FromObjArray; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
530
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
319
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY |
6
|
|
|
|
|
|
|
our $DATE = '2021-10-07'; # DATE |
7
|
|
|
|
|
|
|
our $DIST = 'Role-TinyCommons-Tree'; # DIST |
8
|
|
|
|
|
|
|
our $VERSION = '0.128'; # VERSION |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $GET_PARENT_METHOD = 'parent'; |
11
|
|
|
|
|
|
|
our $GET_CHILDREN_METHOD = 'children'; |
12
|
|
|
|
|
|
|
our $SET_PARENT_METHOD = 'parent'; |
13
|
|
|
|
|
|
|
our $SET_CHILDREN_METHOD = 'children'; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub __build_subtree { |
16
|
3
|
|
50
|
3
|
|
7
|
my ($parent_node, @obj_array) = ($_[0], @{$_[1] // []}); |
|
3
|
|
|
|
|
12
|
|
17
|
|
|
|
|
|
|
|
18
|
3
|
|
|
|
|
6
|
my @children; |
19
|
3
|
|
|
|
|
9
|
while (@obj_array) { |
20
|
4
|
|
|
|
|
8
|
my $child_node = shift @obj_array; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# connect child node to its parent |
23
|
4
|
|
|
|
|
15
|
$child_node->$SET_PARENT_METHOD($parent_node); |
24
|
4
|
|
|
|
|
21
|
push @children, $child_node; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# the child has its own children, recurse |
27
|
4
|
100
|
66
|
|
|
18
|
if (@obj_array && ref $obj_array[0] eq 'ARRAY') { |
28
|
2
|
|
|
|
|
8
|
__build_subtree($child_node, shift(@obj_array)); |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# connect parent node to its children |
33
|
3
|
|
|
|
|
12
|
$parent_node->$SET_CHILDREN_METHOD(\@children); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# return something useful |
36
|
3
|
|
|
|
|
17
|
$parent_node; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub new_from_obj_array { |
40
|
1
|
|
|
1
|
0
|
5024
|
my $class = shift; |
41
|
1
|
|
|
|
|
3
|
my $obj_array = shift; |
42
|
|
|
|
|
|
|
|
43
|
1
|
50
|
33
|
|
|
16
|
die "Object array must be a one- or two-element array" |
|
|
|
33
|
|
|
|
|
44
|
|
|
|
|
|
|
unless ref $obj_array eq 'ARRAY' && (@$obj_array == 1 || @$obj_array == 2); |
45
|
1
|
|
|
|
|
4
|
__build_subtree(@$obj_array); |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
1; |
49
|
|
|
|
|
|
|
# ABSTRACT: Routine to build a tree of objects from a nested array of objects |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
__END__ |