MultiNode/Node.pm

filter: Statementstmt 91.8% 102 / 111 Branchbran 72.2% 13 / 18 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 95.4% 21 / 22 Podpod 100% 13 / 13 totaltotal 88.7% 150 / 169 CC 17 SCAR 14.0
CC
17
Cov
88%
_clearrefs
20.0
new
5.0
_clone
3.0
CRAP 4.1
↑ Summary MultiNode/Handle.pm »
1 package Tree::MultiNode::Node;
2 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 17 our $VERSION = '2.02';
8  
9 367 ▶sub _debug {
Subroutine _debug: called CC=2 SCAR=7.4
10 367 ▶ return unless $Tree::MultiNode::debug;
Branch: 1 branch
BranchTrueFalseTotal
unless $Tree::MultiNode::debug 367 0 367
11 17 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 0 print @_;
13 }
14  
15 =head1 NAME
16  
17 Tree::MultiNode::Node -- a node in a Tree::MultiNode tree.
18  
19 =head1 DESCRIPTION
20  
21 Please note that the Node object is used internally by the MultiNode object.
22 Though you have the ability to interact with the nodes, it is unlikely that
23 you should need to. That being said, the interface is documented here anyway.
24  
25 =head2 Tree::MultiNode::Node::new
26  
27 new($)
28 @param package name or node object to clone [scalar]
29 @returns new node object
30  
31 new($$)
32 @param key [scalar]
33 @param value [scalar]
34 @returns new node object
35  
36 Creates a new Node. There are three behaviors for new. A constructor with no
37 arguments creates a new, empty node. A single argument of another node object
38 will create a clone of the node object. If two arguments are passed, the first
39 is stored as the key, and the second is stored as the value.
40  
41 # clone an existing node
42 my $node = Tree::MultiNode::Node->new($oldNode);
43 # or
44 my $node = $oldNode->new();
45  
46 # create a new node
47 my $node = Tree::MultiNode::Node->new;
48 my $node = Tree::MultiNode::Node->new("fname");
49 my $node = Tree::MultiNode::Node->new("fname","Larry");
50  
51 =cut
52  
53 301 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
54 301 my $this = shift;
55 301 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
03010
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
56 301 my $self = {};
57 301 bless $self, $class;
58  
59 301 my $node = shift;
60 301 ▶ if ( ref($node) eq "Tree::MultiNode::Node" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node eq 'Tree::MultiNode::Node') { } 14 287 301
61  
62 # become a copy of that node...
63 14 $self->_clone($node);
64 }
65 else {
66 287 my ( $key, $value );
67 287 $key = $node;
68 287 $value = shift;
69 287 _debug(__PACKAGE__, "::new() key,val = ", $key, ",", $value, "\n");
70 287 $self->{'children'} = [];
71 287 $self->{'parent'} = undef;
72 287 ▶ $self->{'key'} = defined $key ? $key : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $key ? : 195 92 287
73 287 ▶ $self->{'value'} = defined $value ? $value : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 195 92 287
74 }
75  
76 301 return $self;
77 }
78  
79 #
80 # internal method for making the current node a clone of another
81 # node...
82 #
83 14 ▶sub _clone {
Subroutine _clone: called CC=3 SCAR=11.0
84 14 my $self = shift;
85 14 my $them = shift;
86 14 $self->{'parent'} = $them->parent;
87 14 ▶ weaken($self->{'parent'}) if defined $self->{'parent'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"parent"} 8 6 14
88 14 $self->{'key'} = $them->key;
89 14 $self->{'value'} = $them->value;
90 # Deep clone children so the copy is fully independent
91 14 $self->{'children'} = [];
92 14 foreach my $child ( @{$them->children} ) {
93 8 my $copy = Tree::MultiNode::Node->new($child);
94 8 $copy->{'parent'} = $self;
95 8 weaken($copy->{'parent'});
96 8 push @{$self->{'children'}}, $copy;
97 }
98 }
99  
100 =head2 Tree::MultiNode::Node::key
101  
102 @param key [scalar]
103 @returns the key [scalar]
104  
105 Used to set, or retrieve the key for a node. If a parameter is passed,
106 it sets the key for the node. The value of the key member is always
107 returned.
108  
109 print $node3->key(), "\n"; # 'fname'
110  
111 =cut
112  
113 243 ▶sub key {
Subroutine key: called CC=2 SCAR=6.9
Pod documented
114 243 my ( $self, $key ) = @_;
115  
116 243 ▶ if ( @_ > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 67 176 243
117 67 _debug(__PACKAGE__, "::key() setting key: ", $key, " on ", $self, "\n");
118 67 $self->{'key'} = $key;
119 }
120  
121 243 return $self->{'key'};
122 }
123  
124 =head2 Tree::MultiNode::Node::value
125  
126 @param the value to set [scalar]
127 @returns the value [scalar]
128  
129 Used to set, or retrieve the value for a node. If a parameter is passed,
130 it sets the value for the node (including undef and other falsy values like
131 0 or ""). The value of the value member is always returned.
132  
133 print $node3->value(), "\n"; # 'Larry'
134 $node3->value(0); # sets value to 0
135 $node3->value(undef); # sets value to undef
136  
137 =cut
138  
139 90 ▶sub value {
Subroutine value: called CC=2 SCAR=6.9
Pod documented
140 90 my ( $self, $value ) = @_;
141  
142 90 ▶ if ( @_ > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 13 77 90
143 13 _debug(__PACKAGE__, "::value() setting value: ", $value, " on ", $self, "\n");
144 13 $self->{'value'} = $value;
145 }
146  
147 90 return $self->{'value'};
148 }
149  
150 =head2 Tree::MultiNode::Node::clear_key
151  
152 @returns the deleted key
153  
154 Clears the key member by deleting it.
155  
156 $node3->clear_key();
157  
158 =cut
159  
160 1 ▶sub clear_key {
Subroutine clear_key: called CC=1 SCAR=0.0
Pod documented
161 1 my $self = shift;
162 1 return delete $self->{'key'};
163 }
164  
165 =head2 Tree::MultiNode::Node::clear_value
166  
167 @returns the deleted value
168  
169 Clears the value member by deleting it.
170  
171 $node3->clear_value();
172  
173 =cut
174  
175 1 ▶sub clear_value {
Subroutine clear_value: called CC=1 SCAR=0.0
Pod documented
176 1 my $self = shift;
177 1 return delete $self->{'value'};
178 }
179  
180 =head2 Tree::MultiNode::Node::children
181  
182 @returns reference to children [array reference]
183  
184 Returns a reference to the array that contains the children of the
185 node object.
186  
187 $array_ref = $node3->children();
188  
189 =cut
190  
191 690 ▶sub children {
Subroutine children: called CC=1 SCAR=0.0
Pod documented
192 690 my $self = shift;
193 690 return $self->{'children'};
194 }
195  
196 =head2 Tree::MultiNode::Node::child_keys
197 Tree::MultiNode::Node::child_values
198 Tree::MultiNode::Node::child_kv_pairs
199  
200 These functions return arrays consisting of the appropriate data
201 from the child nodes.
202  
203 my @keys = $node->child_keys();
204 my @vals = $node->child_values();
205 my %kv_pairs = $node->child_kv_pairs();
206  
207 =cut
208  
209 6 ▶sub child_keys {
Subroutine child_keys: called CC=2 SCAR=6.9
Pod documented
210 6 my $self = shift;
211 6 my $children = $self->{'children'};
212 6 my @keys;
213 my $node;
214  
215 6 foreach $node (@$children) {
216 13 push @keys, $node->key();
217 }
218  
219 6 return @keys;
220 }
221  
222 1 ▶sub child_values {
Subroutine child_values: called CC=2 SCAR=6.9
Pod documented
223 1 my $self = shift;
224 1 my $children = $self->{'children'};
225 1 my @values;
226 my $node;
227  
228 1 foreach $node (@$children) {
229 3 push @values, $node->value();
230 }
231  
232 1 return @values;
233 }
234  
235 6 ▶sub child_kv_pairs {
Subroutine child_kv_pairs: called CC=2 SCAR=6.9
Pod documented
236 6 my $self = shift;
237 6 my $children = $self->{'children'};
238 6 my %h;
239 my $node;
240  
241 6 foreach $node (@$children) {
242 15 $h{ $node->key() } = $node->value();
243 }
244  
245 6 return %h;
246 }
247  
248 =head2 Tree::MultiNode::Node::child_key_positions
249  
250 This function returns a hash table that consists of the
251 child keys as the hash keys, and the position in the child
252 array as the value. This allows for a quick and dirty way
253 of looking up the position of a given key in the child list.
254  
255 my %h = $node->child_key_positions();
256  
257 =cut
258  
259 3 ▶sub child_key_positions {
Subroutine child_key_positions: called CC=2 SCAR=6.9
Pod documented
260 3 my $self = shift;
261 3 my $children = $self->{'children'};
262 3 my ( %h, $i, $node );
263  
264 3 $i = 0;
265 3 foreach $node (@$children) {
266 8 $h{ $node->key() } = $i++;
267 }
268  
269 3 return %h;
270 }
271  
272 =head2 Tree::MultiNode::Node::num_children
273  
274 Returns the number of children for this node.
275  
276 my $count = $node->num_children();
277  
278 =cut
279  
280 36 ▶sub num_children {
Subroutine num_children: called CC=1 SCAR=0.0
Pod documented
281 36 my $self = shift;
282 36 return scalar @{$self->{'children'}};
283 }
284  
285 =head2 Tree::MultiNode::Node::parent
286  
287 Returns a reference to the parent node of the current node.
288  
289 $node_parent = $node3->parent();
290  
291 =cut
292  
293 51 ▶sub parent {
Subroutine parent: called CC=1 SCAR=0.0
Pod documented
294 51 my $self = shift;
295 51 return $self->{'parent'};
296 }
297  
298 =head2 Tree::MultiNode::Node::dump
299  
300 Used for diagnostics, it prints out the members of the node.
301  
302 $node3->dump();
303  
304 =cut
305  
306 2 ▶sub dump {
Subroutine dump: called CC=1 SCAR=0.0
Pod documented
307 2 my $self = shift;
308  
309 17 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
310 2 print "[dump] key: ", $self->{'key'}, "\n";
311 2 print "[dump] val: ", $self->{'value'}, "\n";
312 2 print "[dump] parent: ", $self->{'parent'}, "\n";
313 2 print "[dump] children: ", $self->{'children'}, "\n";
314 }
315  
316 0 ▶sub _clearrefs {
Subroutine _clearrefs: not called CC=4 SCAR=30.0
317 0 my $self = shift;
318 0 delete $self->{'parent'};
319 0 my $children = $self->{'children'};
320 0 ▶ if ( defined $children ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $children) 0 0 0
321 0 foreach my $child ( @{$children} ) {
322 0 ▶ $child->_clearrefs() if defined $child;
Branch: 1 branch
BranchTrueFalseTotal
if defined $child 0 0 0
323 }
324 }
325 0 delete $self->{'children'};
326 }
327  
328 17 1;
↑ Summary MultiNode/Handle.pm »