line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Tree::From::Struct; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY |
4
|
|
|
|
|
|
|
our $DATE = '2020-02-04'; # DATE |
5
|
|
|
|
|
|
|
our $DIST = 'Tree-From-Struct'; # DIST |
6
|
|
|
|
|
|
|
our $VERSION = '0.040'; # VERSION |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
require Code::Includable::Tree::FromStruct; |
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
70200
|
use Exporter qw(import); |
|
1
|
|
|
|
|
12
|
|
|
1
|
|
|
|
|
163
|
|
11
|
|
|
|
|
|
|
our @EXPORT_OK = qw(build_tree_from_struct); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub build_tree_from_struct { |
14
|
1
|
|
|
1
|
1
|
90
|
my $struct = shift; |
15
|
|
|
|
|
|
|
|
16
|
1
|
50
|
|
|
|
4
|
my $class = $struct->{_class} or die "Please specify _class in struct"; |
17
|
1
|
|
|
|
|
6
|
Code::Includable::Tree::FromStruct::new_from_struct($class, $struct); |
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
1; |
21
|
|
|
|
|
|
|
# ABSTRACT: Build a tree object from hash structure |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
__END__ |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=pod |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=encoding UTF-8 |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head1 NAME |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
Tree::From::Struct - Build a tree object from hash structure |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=head1 VERSION |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
This document describes version 0.040 of Tree::From::Struct (from Perl distribution Tree-From-Struct), released on 2020-02-04. |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=head1 SYNOPSIS |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
In your tree node class F<My/Person.pm>: |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
package My::Person; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub new { |
44
|
|
|
|
|
|
|
my $class = shift; |
45
|
|
|
|
|
|
|
my %args = @_; |
46
|
|
|
|
|
|
|
bless \%args, $class; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub parent { |
50
|
|
|
|
|
|
|
my $self = shift; |
51
|
|
|
|
|
|
|
$self->{_parent} = $_[0] if $@; |
52
|
|
|
|
|
|
|
$self->{_parent}; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub children { |
56
|
|
|
|
|
|
|
my $self = shift; |
57
|
|
|
|
|
|
|
$self->{_children} = $_[0] if $@; |
58
|
|
|
|
|
|
|
$self->{_children}; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
In your code to build a tree: |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
use Tree::From::Struct qw(build_tree_from_struct); |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# require all the used classes |
66
|
|
|
|
|
|
|
use My::Person; |
67
|
|
|
|
|
|
|
use My::MarriedPerson; |
68
|
|
|
|
|
|
|
use My::KidPerson; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
my $family_tree = build_tree_from_struct({ |
71
|
|
|
|
|
|
|
_class => 'My::Person', name => 'Andi', age => 60, _children => [ |
72
|
|
|
|
|
|
|
{name => 'Budi', age => 30}, |
73
|
|
|
|
|
|
|
{_class => 'My::MarriedPerson', name => 'Cinta', _children => [ |
74
|
|
|
|
|
|
|
{class => 'My::KidPerson', name => 'Deni'}, |
75
|
|
|
|
|
|
|
{class => 'My::KidPerson', name => 'Eno'}, |
76
|
|
|
|
|
|
|
]}, |
77
|
|
|
|
|
|
|
]}); |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=head1 DESCRIPTION |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
Building a tree manually can be tedious: you have to connect the parent and |
82
|
|
|
|
|
|
|
the children nodes together: |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
my $root = My::TreeNode->new(...); |
85
|
|
|
|
|
|
|
my $child1 = My::TreeNode->new(...); |
86
|
|
|
|
|
|
|
my $child2 = My::TreeNode->new(...); |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
$root->children([$child1, $child2]); |
89
|
|
|
|
|
|
|
$child1->parent($root); |
90
|
|
|
|
|
|
|
$child2->parent($root); |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
my $grandchild1 = My::TreeNode->new(...); |
93
|
|
|
|
|
|
|
... |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
This module provides a convenience function to build a tree of objects in a |
96
|
|
|
|
|
|
|
single command. It connect the parent and children nodes for you. |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
The class can be any class that provides C<parent> and C<children> methods. See |
99
|
|
|
|
|
|
|
L<Role::TinyCommons::Tree::Node> for more details. |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=head1 FUNCTIONS |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head2 build_tree_from_struct($struct) => obj |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
This is basically L<Role::TinyCommons::Tree::FromStruct>'s C<new_from_struct> |
106
|
|
|
|
|
|
|
presented as a function. See the role's documentation for more details on what |
107
|
|
|
|
|
|
|
you can put in C<$struct>. |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head1 HOMEPAGE |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
Please visit the project's homepage at L<https://metacpan.org/release/Tree-From-Struct>. |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head1 SOURCE |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
Source repository is at L<https://github.com/perlancar/perl-Tree-FromStruct>. |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head1 BUGS |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Tree-From-Struct> |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
When submitting a bug or request, please include a test-file or a |
122
|
|
|
|
|
|
|
patch to an existing test-file that illustrates the bug or desired |
123
|
|
|
|
|
|
|
feature. |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=head1 SEE ALSO |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
L<Role::TinyCommons::Tree::FromStruct> if you want to use this functionality via |
128
|
|
|
|
|
|
|
consuming a role. |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
Other ways to create tree: L<Tree::From::Text>, L<Tree::From::TextLines>, |
131
|
|
|
|
|
|
|
L<Tree::Create::Callback>, L<Tree::Create::Size>. |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head1 AUTHOR |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
perlancar <perlancar@cpan.org> |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
This software is copyright (c) 2020, 2016 by perlancar@cpan.org. |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
142
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=cut |