MultiNode/Handle.pm

filter: Statementstmt 96.5% 311 / 322 Branchbran 80.4% 66 / 82 Conditioncond 58.3% 21 / 36 MC/DCmcdc 34.7% 8 / 23 Subroutinesub 97.5% 40 / 41 Podpod 100% 31 / 31 totaltotal 89.1% 477 / 535 CC 55 SCAR 12.4
CC
55
Cov
90%
remove_child
6.2
get_child
6.2
add_child_node
6.0
CRAP 3.5
« MultiNode/Node.pm ↑ Summary MultiNode.pm »
1 package Tree::MultiNode::Handle;
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 1529 ▶sub _debug {
Subroutine _debug: called CC=2 SCAR=7.4
10 1529 ▶ return unless $Tree::MultiNode::debug;
Branch: 1 branch
BranchTrueFalseTotal
unless $Tree::MultiNode::debug 1529 0 1529
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::Handle -- a cursor for navigating a Tree::MultiNode tree.
18  
19 =head1 DESCRIPTION
20  
21 Handle is used as a 'pointer' into the tree. It has a few attributes that it keeps
22 track of. These are:
23  
24 1. the top of the tree
25 2. the current node
26 3. the current child node
27 4. the depth of the current node
28  
29 The top of the tree never changes, and you can reset the handle to point back at
30 the top of the tree by calling the top() method.
31  
32 The current node is where the handle is 'pointing' in the tree. The current node
33 is changed with functions like top(), down(), and up().
34  
35 The current child node is used for traversing downward into the tree. The members
36 first(), next(), prev(), last(), and position() can be used to set the current child,
37 and then traverse down into it.
38  
39 The depth of the current node is a measure of the length of the path
40 from the top of the tree to the current node, i.e., the top of the node
41 has a depth of 0, each of its children has a depth of 1, etc.
42  
43 =cut
44  
45 =head2 Tree::MultiNode::Handle::new
46  
47 Constructs a new handle. You must pass a tree object to Handle::new.
48  
49 my $tree = Tree::MultiNode->new;
50 my $handle = Tree::MultiNode::Handle->new($tree);
51  
52 =cut
53  
54 101 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
55 101 my $this = shift;
56 101 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
01010
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
57  
58 101 my $self = {};
59 101 bless $self, $class;
60 101 my $data = shift;
61 101 _debug(__PACKAGE__, "::new() ref($data) is: ", ref($data), "\n");
62 101 ▶ if ( ref($data) eq "Tree::MultiNode::Handle" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq 'Tree::MultiNode::Handle') { } 10 91 101
63 10 $self->_clone($data);
64 }
65 else {
66 91 ▶ unless ( ref($data) eq "Tree::MultiNode" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $data eq "Tree::MultiNode") 1 90 91
67 1 confess "Error, invalid Tree::MultiNode reference: $data\n";
68 }
69  
70 90 $self->{'tree'} = $data;
71 90 $self->{'curr_pos'} = undef;
72 90 $self->{'curr_node'} = $data->{'top'};
73 90 $self->{'curr_child'} = undef;
74 90 $self->{'curr_depth'} = 0;
75 }
76 100 return $self;
77 }
78  
79 #
80 # internal method for making the current handle a copy of another
81 # handle...
82 #
83 10 ▶sub _clone {
Subroutine _clone: called CC=1 SCAR=0.0
84 10 my $self = shift;
85 10 my $them = shift;
86 10 _debug(__PACKAGE__, "::_clone() cloning: ", $them, "\n");
87 10 _debug(__PACKAGE__, "::_clone() depth: ", $them->{'curr_depth'}, "\n");
88 10 $self->{'tree'} = $them->{'tree'};
89 10 $self->{'curr_pos'} = $them->{'curr_pos'};
90 10 $self->{'curr_node'} = $them->{'curr_node'};
91 10 $self->{'curr_child'} = $them->{'curr_child'};
92 10 $self->{'curr_depth'} = $them->{'curr_depth'};
93 10 return 1;
94 }
95  
96 =head2 Tree::MultiNode::Handle::tree
97  
98 Returns the tree that was used to construct the node. Useful if you're
99 trying to create another node into the tree.
100  
101 my $handle2 = Tree::MultiNode::Handle->new($handle->tree());
102  
103 =cut
104  
105 1 ▶sub tree {
Subroutine tree: called CC=1 SCAR=0.0
Pod documented
106 1 my $self = shift;
107 1 return $self->{'tree'};
108 }
109  
110 =head2 Tree::MultiNode::Handle::get_data
111  
112 Retrieves both the key, and value (as an array) for the current node.
113  
114 my ($key,$val) = $handle->get_data();
115  
116 =cut
117  
118 8 ▶sub get_data {
Subroutine get_data: called CC=1 SCAR=0.0
Pod documented
119 8 my $self = shift;
120 8 my $node = $self->{'curr_node'};
121  
122 8 return ( $node->key, $node->value );
123 }
124  
125 =head2 Tree::MultiNode::Handle::get_key
126  
127 Retrieves the key for the current node.
128  
129 $key = $handle->get_key();
130  
131 =cut
132  
133 48 ▶sub get_key {
Subroutine get_key: called CC=1 SCAR=0.0
Pod documented
134 48 my $self = shift;
135 48 my $node = $self->{'curr_node'};
136  
137 48 my $key = $node->key();
138  
139 48 _debug(__PACKAGE__, "::get_key() getting from ", $node, " : ", $key, "\n");
140  
141 48 return $key;
142 }
143  
144 =head2 Tree::MultiNode::Handle::set_key
145  
146 Sets the key for the current node.
147  
148 $handle->set_key("lname");
149  
150 =cut
151  
152 63 ▶sub set_key {
Subroutine set_key: called CC=1 SCAR=0.0
Pod documented
153 63 my $self = shift;
154 63 my $key = shift;
155 63 my $node = $self->{'curr_node'};
156  
157 63 _debug(__PACKAGE__, "::set_key() setting key \"", $key, "\" on: ", $node, "\n");
158  
159 63 return $node->key($key);
160 }
161  
162 =head2 Tree::MultiNode::Handle::get_value
163  
164 Retrieves the value for the current node.
165  
166 $val = $handle->get_value();
167  
168 =cut
169  
170 7 ▶sub get_value {
Subroutine get_value: called CC=1 SCAR=0.0
Pod documented
171 7 my $self = shift;
172 7 my $node = $self->{'curr_node'};
173  
174 7 my $value = $node->value();
175  
176 7 _debug(__PACKAGE__, "::get_value() getting from ", $node, " : ", $value, "\n");
177  
178 7 return $value;
179 }
180  
181 =head2 Tree::MultiNode::Handle::set_value
182  
183 Sets the value for the current node.
184  
185 $handle->set_value("Wall");
186  
187 =cut
188  
189 9 ▶sub set_value {
Subroutine set_value: called CC=1 SCAR=0.0
Pod documented
190 9 my $self = shift;
191 9 my $value = shift;
192 9 my $node = $self->{'curr_node'};
193  
194 9 _debug(__PACKAGE__, "::set_value() setting value \"", $value, "\" on: ", $node, "\n");
195  
196 9 return $node->value($value);
197 }
198  
199 =head2 Tree::MultiNode::Handle::get_child
200  
201 get_child takes an optional parameter which is the position of the child
202 that is to be retrieved. If this position is not specified, get_child
203 attempts to return the current child. get_child returns a Node object.
204  
205 my $child_node = $handle->get_child();
206  
207 =cut
208  
209 118 ▶sub get_child {
Subroutine get_child: called CC=5 SCAR=18.2
Pod documented
210 118 my $self = shift;
211 118 my $children = $self->{'curr_node'}->children;
212 118 my $pos = shift;
213 118 ▶ $pos = defined $pos ? $pos : $self->{'curr_pos'};
Branch: 1 branch
BranchTrueFalseTotal
defined $pos ? : 118 0 118
214  
215 118 _debug(__PACKAGE__, "::get_child() children: ", $children, " ", $pos, "\n");
216  
217 118 ▶ unless ( defined $children ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $children) 0 118 118
218 0 return undef;
219 }
220  
221 118 ▶ unless ( defined $pos && $pos <= $#{$children} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pos and $pos <= $#{$children;}) 0 118 118
Condition33%1/3
defined $pos and $pos <= $#{$children;}
!ll&&!rl&&r
00118
MC/DC0%0/2
defined $pos and $pos <= $#{$children;}
defined $pos $pos <= $#{$children;}
Truth table33%1/3
A and B
A= defined $posB= $pos <= $#{$children;}
ABresult
0X0
100
111
222 0 my $num = $#{$children};
223 0 confess "Error, $pos is an invalid position [$num] $children.\n";
224 }
225  
226 _debug(__PACKAGE__, "::get_child() returning [$pos]: ",
227 118 ${$children}[$pos], "\n");
228 118 return ( ${$children}[$pos] );
229 }
230  
231 =head2 Tree::MultiNode::Handle::add_child
232  
233 This member adds a new child node to the end of the array of children for the
234 current node. There are three optional parameters:
235  
236 - a key
237 - a value
238 - a position
239  
240 If passed, the key and value will be set in the new child. If a position is
241 passed, the new child will be inserted into the current array of children at
242 the position specified.
243  
244 $handle->add_child(); # adds a blank child
245 $handle->add_child("language","perl"); # adds a child to the end
246 $handle->add_child("language","C++",0); # adds a child to the front
247  
248 =cut
249  
250 176 ▶sub add_child {
Subroutine add_child: called CC=4 SCAR=13.9
Pod documented
251 176 my $self = shift;
252 176 my ( $key, $value, $pos ) = @_;
253 176 my $children = $self->{'curr_node'}->children;
254 176 _debug(__PACKAGE__, "::add_child() children: ", $children, "\n");
255 176 my $child = Tree::MultiNode::Node->new( $key, $value );
256 176 $child->{'parent'} = $self->{'curr_node'};
257 176 weaken($child->{'parent'});
258  
259 176 _debug(__PACKAGE__, "::add_child() adding child ", $child, " (", $key, ",", $value, ") ",
260 "to: ", $children, "\n");
261  
262 176 ▶ if ( defined $pos ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pos) { } 2 174 176
263 2 _debug(__PACKAGE__, "::add_child() adding at ", $pos, " ", $child, "\n");
264 2 ▶ unless ( $pos >= 0 && $pos <= $#{$children} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pos >= 0 and $pos <= $#{$children;}) 1 1 2
Condition66%2/3
$pos >= 0 and $pos <= $#{$children;}
!ll&&!rl&&r
101
MC/DC50%1/2
$pos >= 0 and $pos <= $#{$children;}
$pos >= 0 $pos <= $#{$children;}
Truth table66%2/3
A and B
A= $pos >= 0B= $pos <= $#{$children;}
ABresult
0X0
100
111
265 1 my $num = $#{$children};
266 1 confess "Position $pos is invalid for child position [$num] $children.\n";
267 }
268 1 splice( @{$children}, $pos, 1, $child, ${$children}[$pos] );
269 }
270 else {
271 174 _debug(__PACKAGE__, "::add_child() adding at end ", $child, "\n");
272 174 push @{$children}, $child;
273 }
274  
275 _debug(__PACKAGE__, "::add_child() children:",
276 175 join( ',', @{ $self->{'curr_node'}->children } ), "\n");
277 }
278  
279 =head2 Tree::MultiNode::Handle::add_child_node
280  
281 Adds an existing node (or the top node of another tree) as a child of the
282 current node. Works like C<add_child()> but accepts a pre-built
283 L<Tree::MultiNode::Node> or L<Tree::MultiNode> object instead of a key/value
284 pair.
285  
286 When a Tree::MultiNode (tree) object is passed, its top node is extracted
287 and added as a child. The original tree remains valid but now shares
288 structure with this tree -- the caller should not modify the original tree
289 after this call.
290  
291 When a position is given, the new child is inserted before the existing
292 child at that position. Without a position, the child is appended to the
293 end.
294  
295 # append an existing node as the last child
296 my $node = Tree::MultiNode::Node->new("color", "red");
297 $handle->add_child_node($node);
298  
299 # insert at a specific position
300 $handle->add_child_node($node, 0); # insert as first child
301  
302 # graft another tree's root node
303 my $other = Tree::MultiNode->new();
304 $handle->add_child_node($other);
305  
306 =cut
307  
308 9 ▶sub add_child_node {
Subroutine add_child_node: called CC=6 SCAR=17.9
Pod documented
309 9 my $self = shift;
310 9 my ( $child, $pos ) = @_;
311 9 my $children = $self->{'curr_node'}->children;
312 9 _debug(__PACKAGE__, "::add_child_node() children: ", $children, "\n");
313 9 ▶ if ( ref($child) eq 'Tree::MultiNode' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $child eq "Tree::MultiNode") 2 7 9
314 2 $child = $child->{'top'};
315 }
316 9 ▶ confess "Invalid child argument.\n"
Branch: 1 branch
BranchTrueFalseTotal
if ref $child ne "Tree::MultiNode::Node" 3 6 9
317 if ( ref($child) ne 'Tree::MultiNode::Node' );
318  
319 6 $child->{'parent'} = $self->{'curr_node'};
320 6 weaken($child->{'parent'});
321  
322 6 _debug(__PACKAGE__, "::add_child_node() adding child ", $child,
323 " to: ", $children, "\n");
324  
325 6 ▶ if ( defined $pos ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pos) { } 2 4 6
326 2 _debug(__PACKAGE__, "::add_child_node() adding at ", $pos, " ", $child, "\n");
327 2 ▶ unless ( $pos >= 0 && $pos <= $#{$children} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pos >= 0 and $pos <= $#{$children;}) 1 1 2
Condition66%2/3
$pos >= 0 and $pos <= $#{$children;}
!ll&&!rl&&r
101
MC/DC50%1/2
$pos >= 0 and $pos <= $#{$children;}
$pos >= 0 $pos <= $#{$children;}
Truth table66%2/3
A and B
A= $pos >= 0B= $pos <= $#{$children;}
ABresult
0X0
100
111
328 1 my $num = $#{$children};
329 1 confess "Position $pos is invalid for child position [$num] $children.\n";
330 }
331 1 splice( @{$children}, $pos, 1, $child, ${$children}[$pos] );
332 }
333 else {
334 4 _debug(__PACKAGE__, "::add_child_node() adding at end ", $child, "\n");
335 4 push @{$children}, $child;
336 }
337  
338 _debug(__PACKAGE__, "::add_child_node() children:",
339 5 join( ',', @{ $self->{'curr_node'}->children } ), "\n");
340 }
341  
342 =head2 Tree::MultiNode::Handle::depth
343  
344 Gets the depth for the current node.
345  
346 my $depth = $handle->depth();
347  
348 =cut
349  
350 29 ▶sub depth {
Subroutine depth: called CC=1 SCAR=0.0
Pod documented
351 29 my $self = shift;
352 29 my $node = $self->{'curr_node'};
353  
354 29 _debug(__PACKAGE__, "::depth() getting depth \"", $self->{'curr_depth'}, "\" ",
355 "on: ", $node, "\n");
356  
357 29 return $self->{'curr_depth'};
358 }
359  
360 =head2 Tree::MultiNode::Handle::select
361  
362 Sets the current child via a specified value -- basically it iterates
363 through the array of children, looking for a match. You have to
364 supply the key to look for, and optionally a sub ref to find it. The
365 default for this sub is
366  
367 sub { return shift eq shift; }
368  
369 Which is sufficient for testing the equality of strings (the most common
370 thing that I think will get stored in the tree). If you're storing multiple
371 data types as keys, you'll have to write a sub that figures out how to
372 perform the comparisons in a sane manner.
373  
374 The code reference should take two arguments, and compare them -- return
375 false if they don't match, and true if they do.
376  
377 $handle->select('lname', sub { return shift eq shift; } );
378  
379 =cut
380  
381 8 ▶sub select {
Subroutine select: called CC=4 SCAR=13.9
Pod documented
382 8 my $self = shift;
383 8 my $key = shift;
384 8 ▶ my $code = shift || sub { return shift eq shift; };
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
shift() || sub { return shift() eq shift(); }
l!l&&r!l&&!r
170
MC/DC0%0/2
shift() || sub { return shift() eq shift(); }
shift() sub { return shift() eq shift(); }
Truth table66%2/3
A || B
A= shift()B= sub { return shift() eq shift(); }
ABresult
1X1
011
000
385 8 my ( $child, $pos );
386 8 my $found = undef;
387  
388 8 $pos = 0;
389 8 foreach $child ( $self->children() ) {
390 14 ▶ if ( $code->( $key, $child->key() ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$code($key, $child->key)) 5 9 14
391 5 $self->{'curr_pos'} = $pos;
392 5 $self->{'curr_child'} = $child;
393 5 ++$found;
394 5 last;
395 }
396 9 ++$pos;
397 }
398  
399 8 return $found;
400 }
401  
402 =head2 Tree::MultiNode::Handle::position
403  
404 Sets, or retrieves the current child position.
405  
406 print "curr child pos is: ", $handle->position(), "\n";
407 $handle->position(5); # sets the 6th child as the current child
408  
409 =cut
410  
411 40 ▶sub position {
Subroutine position: called CC=4 SCAR=13.9
Pod documented
412 40 my $self = shift;
413 40 my $pos = shift;
414  
415 40 _debug(__PACKAGE__, "::position() ", $self, " ", $pos, "\n");
416  
417 40 ▶ unless ( defined $pos ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pos) 11 29 40
418 11 return $self->{'curr_pos'};
419 }
420  
421 29 my $children = $self->{'curr_node'}->children;
422 29 _debug(__PACKAGE__, "::position() children: ", $children, "\n");
423 _debug(__PACKAGE__, "::position() position is $pos ",
424 29 $#{$children}, "\n");
425 29 ▶ unless ( $pos >= 0 && $pos <= $#{$children} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pos >= 0 and $pos <= $#{$children;}) 1 28 29
Condition66%2/3
$pos >= 0 and $pos <= $#{$children;}
!ll&&!rl&&r
1028
MC/DC50%1/2
$pos >= 0 and $pos <= $#{$children;}
$pos >= 0 $pos <= $#{$children;}
Truth table66%2/3
A and B
A= $pos >= 0B= $pos <= $#{$children;}
ABresult
0X0
100
111
426 1 my $num = $#{$children};
427 1 confess "Error, $pos is invalid [$num] $children.\n";
428 }
429 28 $self->{'curr_pos'} = $pos;
430 28 $self->{'curr_child'} = $self->get_child($pos);
431 28 return $self->{'curr_pos'};
432 }
433  
434 =head2 Tree::MultiNode::Handle::first
435 Tree::MultiNode::Handle::next
436 Tree::MultiNode::Handle::prev
437 Tree::MultiNode::Handle::last
438  
439 These functions manipulate the current child member. first() sets the first
440 child as the current child, while last() sets the last. next(), and prev() will
441 move to the next/prev child respectively. If there is no current child node,
442 next() will have the same effect as first(), and prev() will operate as last().
443 prev() fails if the current child is the first child, and next() fails if the
444 current child is the last child -- i.e., they do not wrap around.
445  
446 These functions will fail if there are no children for the current node.
447  
448 $handle->first(); # sets to the 0th child
449 $handle->next(); # to the 1st child
450 $handle->prev(); # back to the 0th child
451 $handle->last(); # go straight to the last child.
452  
453 =cut
454  
455 40 ▶sub first {
Subroutine first: called CC=3 SCAR=11.0
Pod documented
456 40 my $self = shift;
457 40 my $children = $self->{'curr_node'}->children;
458  
459 40 ▶ unless ( defined $children && @{$children} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $children and @{$children;}) 1 39 40
Condition66%2/3
defined $children and @{$children;}
!ll&&!rl&&r
0139
MC/DC50%1/2
defined $children and @{$children;}
defined $children @{$children;}
Truth table66%2/3
A and B
A= defined $childrenB= @{$children;}
ABresult
0X0
100
111
460 1 return undef;
461 }
462  
463 39 $self->{'curr_pos'} = 0;
464 39 $self->{'curr_child'} = $self->get_child(0);
465 _debug(__PACKAGE__, "::first() set child[", $self->{'curr_pos'}, "]: ",
466 39 $self->{'curr_child'}, "\n");
467 39 return $self->{'curr_pos'};
468 }
469  
470 9 ▶sub next {
Subroutine next: called CC=4 SCAR=13.9
Pod documented
471 9 my $self = shift;
472 9 my $children = $self->{'curr_node'}->children;
473 9 _debug(__PACKAGE__, "::next() children: ", $children, "\n");
474  
475 # If no current child, behave like first() per documented contract
476 9 ▶ unless ( defined $self->{'curr_pos'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"curr_pos"}) 2 7 9
477 2 return $self->first();
478 }
479  
480 7 my $pos = $self->{'curr_pos'} + 1;
481 7 ▶ unless ( $pos >= 0 && $pos <= $#{$children} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pos >= 0 and $pos <= $#{$children;}) 2 5 7
Condition66%2/3
$pos >= 0 and $pos <= $#{$children;}
!ll&&!rl&&r
025
MC/DC50%1/2
$pos >= 0 and $pos <= $#{$children;}
$pos >= 0 $pos <= $#{$children;}
Truth table66%2/3
A and B
A= $pos >= 0B= $pos <= $#{$children;}
ABresult
0X0
100
111
482 2 return undef;
483 }
484  
485 5 $self->{'curr_pos'} = $pos;
486 5 $self->{'curr_child'} = $self->get_child($pos);
487 5 return $self->{'curr_pos'};
488 }
489  
490 6 ▶sub prev {
Subroutine prev: called CC=4 SCAR=13.9
Pod documented
491 6 my $self = shift;
492 6 my $children = $self->{'curr_node'}->children;
493 6 _debug(__PACKAGE__, "::prev() children: ", $children, "\n");
494  
495 # If no current child, behave like last() per documented contract
496 6 ▶ unless ( defined $self->{'curr_pos'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"curr_pos"}) 2 4 6
497 2 return $self->last();
498 }
499  
500 4 my $pos = $self->{'curr_pos'} - 1;
501 4 ▶ unless ( $pos >= 0 && $pos <= $#{$children} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pos >= 0 and $pos <= $#{$children;}) 1 3 4
Condition66%2/3
$pos >= 0 and $pos <= $#{$children;}
!ll&&!rl&&r
103
MC/DC50%1/2
$pos >= 0 and $pos <= $#{$children;}
$pos >= 0 $pos <= $#{$children;}
Truth table66%2/3
A and B
A= $pos >= 0B= $pos <= $#{$children;}
ABresult
0X0
100
111
502 1 return undef;
503 }
504  
505 3 $self->{'curr_pos'} = $pos;
506 3 $self->{'curr_child'} = $self->get_child($pos);
507 3 return $self->{'curr_pos'};
508 }
509  
510 8 ▶sub last {
Subroutine last: called CC=3 SCAR=11.0
Pod documented
511 8 my $self = shift;
512 8 my $children = $self->{'curr_node'}->children;
513  
514 8 ▶ unless ( defined $children && @{$children} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $children and @{$children;}) 1 7 8
Condition66%2/3
defined $children and @{$children;}
!ll&&!rl&&r
017
MC/DC50%1/2
defined $children and @{$children;}
defined $children @{$children;}
Truth table66%2/3
A and B
A= defined $childrenB= @{$children;}
ABresult
0X0
100
111
515 1 return undef;
516 }
517  
518 7 my $pos = $#{$children};
519 7 _debug(__PACKAGE__, "::last() children [", $pos, "]: ", $children, "\n");
520  
521 7 $self->{'curr_pos'} = $pos;
522 7 $self->{'curr_child'} = $self->get_child($pos);
523 7 return $self->{'curr_pos'};
524 }
525  
526 =head2 Tree::MultiNode::Handle::down
527  
528 down() moves the handle to point at the current child node. It fails
529 if there is no current child node. When down() is called, the current
530 child becomes invalid (undef).
531  
532 $handle->down();
533  
534 =cut
535  
536 58 ▶sub down {
Subroutine down: called CC=5 SCAR=16.2
Pod documented
537 58 my $self = shift;
538 58 my $pos = shift;
539 58 my $node = $self->{'curr_node'};
540 58 ▶ return undef unless defined $node;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 0 58 58
541 58 my $children = $node->children;
542 58 _debug(__PACKAGE__, "::down() children: ", $children, "\n");
543  
544 58 ▶ if ( defined $pos ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pos) 25 33 58
545 25 ▶ unless ( defined $self->position($pos) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->position($pos)) 0 25 25
546 0 confess "Error, $pos was an invalid position.\n";
547 }
548 }
549  
550 # Prevent corrupting the handle when no child is selected
551 58 ▶ unless ( defined $self->{'curr_child'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"curr_child"}) 3 55 58
552 3 return undef;
553 }
554  
555 55 $self->{'curr_pos'} = undef;
556 55 $self->{'curr_node'} = $self->{'curr_child'};
557 55 $self->{'curr_child'} = undef;
558 55 ++$self->{'curr_depth'};
559 55 _debug(__PACKAGE__, "::down() set to: ", $self->{'curr_node'}, "\n");
560  
561 55 return 1;
562 }
563  
564 =head2 Tree::MultiNode::Handle::up
565  
566 up() moves the handle to point at the parent of the current node. It fails
567 if there is no parent node. When up() is called, the current child becomes
568 invalid (undef).
569  
570 $handle->up();
571  
572 =cut
573  
574 33 ▶sub up {
Subroutine up: called CC=3 SCAR=11.0
Pod documented
575 33 my $self = shift;
576 33 my $node = $self->{'curr_node'};
577 33 ▶ return undef unless defined $node;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 0 33 33
578 33 my $parent = $node->parent();
579  
580 33 ▶ unless ( defined $parent ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $parent) 1 32 33
581 1 return undef;
582 }
583  
584 32 $self->{'curr_pos'} = undef;
585 32 $self->{'curr_node'} = $parent;
586 32 $self->{'curr_child'} = undef;
587 32 --$self->{'curr_depth'};
588  
589 32 return 1;
590 }
591  
592 =head2 Tree::MultiNode::Handle::top
593  
594 Resets the handle to point back at the top of the tree.
595 When top() is called, the current child becomes invalid (undef).
596  
597 $handle->top();
598  
599 =cut
600  
601 8 ▶sub top {
Subroutine top: called CC=1 SCAR=0.0
Pod documented
602 8 my $self = shift;
603 8 my $tree = $self->{'tree'};
604  
605 8 $self->{'curr_pos'} = undef;
606 8 $self->{'curr_node'} = $tree->{'top'};
607 8 $self->{'curr_child'} = undef;
608 8 $self->{'curr_depth'} = 0;
609  
610 8 return 1;
611 }
612  
613 =head2 Tree::MultiNode::Handle::children
614  
615 This returns an array of Node objects that represents the children of the
616 current Node. Unlike Node::children(), the array Handle::children() is not
617 a reference to an array, but an array. Useful if you need to iterate through
618 the children of the current node.
619  
620 print "There are: ", scalar($handle->children()), " children\n";
621 foreach $child ($handle->children()) {
622 print $child->key(), " : ", $child->value(), "\n";
623 }
624  
625 =cut
626  
627 20 ▶sub children {
Subroutine children: called CC=2 SCAR=7.0
Pod documented
628 20 my $self = shift;
629 20 my $node = $self->{'curr_node'};
630 20 ▶ return undef unless defined $node;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 0 20 20
631 20 my $children = $node->children;
632  
633 20 return @{$children};
634 }
635  
636 =head2 Tree::MultiNode::Handle::num_children
637  
638 Returns the number of children for the current node. This is more
639 efficient than C<scalar($handle-E<gt>children())> because it does not
640 copy the children array.
641  
642 my $count = $handle->num_children();
643  
644 =cut
645  
646 33 ▶sub num_children {
Subroutine num_children: called CC=1 SCAR=0.0
Pod documented
647 33 my $self = shift;
648 33 return $self->{'curr_node'}->num_children();
649 }
650  
651 =head2 Tree::MultiNode::Handle::child_key_positions
652  
653 This function returns a hash table that consists of the
654 child keys as the hash keys, and the position in the child
655 array as the value. This allows for a quick and dirty way
656 of looking up the position of a given key in the child list.
657  
658 my %h = $handle->child_key_positions();
659  
660 =cut
661  
662 2 ▶sub child_key_positions {
Subroutine child_key_positions: called CC=1 SCAR=0.0
Pod documented
663 2 my $self = shift;
664 2 my $node = $self->{'curr_node'};
665  
666 2 return $node->child_key_positions();
667 }
668  
669 =head2 Tree::MultiNode::Handle::get_child_key
670  
671 Returns the key at the specified position, or from the corresponding child
672 node.
673  
674 my $key = $handle->get_child_key();
675  
676 =cut
677  
678 29 ▶sub get_child_key {
Subroutine get_child_key: called CC=3 SCAR=11.0
Pod documented
679 29 my $self = shift;
680 29 my $pos = shift;
681 29 ▶ $pos = $self->{'curr_pos'} unless defined $pos;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos 25 4 29
682  
683 29 my $node = $self->get_child($pos);
684 29 ▶ return defined $node ? $node->key() : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $node ? : 29 0 29
685 }
686  
687 =head2 Tree::MultiNode::Handle::get_child_value
688  
689 Returns the value at the specified position, or from the corresponding child
690 node.
691  
692 my $value = $handle->get_child_value();
693  
694 =cut
695  
696 6 ▶sub get_child_value {
Subroutine get_child_value: called CC=3 SCAR=11.0
Pod documented
697 6 my $self = shift;
698 6 my $pos = shift;
699 6 ▶ $pos = defined $pos ? $pos : $self->{'curr_pos'};
Branch: 1 branch
BranchTrueFalseTotal
defined $pos ? : 2 4 6
700  
701 6 _debug(__PACKAGE__, "::get_child_value() pos is: ", $pos, "\n");
702 6 my $node = $self->get_child($pos);
703 6 ▶ return defined $node ? $node->value() : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $node ? : 6 0 6
704 }
705  
706 =head2 Tree::MultiNode::Handle::kv_pairs
707  
708 Returns Tree::MultiNode::Node::child_kv_pairs() for the
709 current node for this handle.
710  
711 my %pairs = $handle->kv_pairs();
712  
713 =cut
714  
715 5 ▶sub kv_pairs {
Subroutine kv_pairs: called CC=1 SCAR=0.0
Pod documented
716 5 my $self = shift;
717 5 my $node = $self->{'curr_node'};
718  
719 5 return $node->child_kv_pairs();
720 }
721  
722 =head2 Tree::MultiNode::Handle::remove_child
723  
724 Removes the child at the specified position, or at the current child
725 position if no position is given. Returns the key and value of the
726 removed child node.
727  
728 my ($key, $value) = $handle->remove_child(0);
729  
730 =cut
731  
732 9 ▶sub remove_child {
Subroutine remove_child: called CC=6 SCAR=18.2
Pod documented
733 9 my $self = shift;
734 9 my $pos = shift;
735 9 ▶ $pos = defined $pos ? $pos : $self->{'curr_pos'};
Branch: 1 branch
BranchTrueFalseTotal
defined $pos ? : 8 1 9
736  
737 9 _debug(__PACKAGE__, "::remove_child() pos is: ", $pos, "\n");
738  
739 9 my $children = $self->{'curr_node'}->children;
740  
741 9 ▶ unless ( defined $children ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $children) 0 9 9
742 0 return undef;
743 }
744  
745 9 ▶ unless ( defined $pos && $pos >= 0 && $pos <= $#{$children} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pos and $pos >= 0 and $pos <= $#{$children;}) 1 8 9
Condition50%3/6
defined $pos and $pos >= 0 and $pos <= $#{$children;}
!ll&&!rl&&r
018
defined $pos and $pos >= 0
!ll&&!rl&&r
009
MC/DC33%1/3
defined $pos and $pos >= 0 and $pos <= $#{$children;}
defined $pos $pos >= 0 $pos <= $#{$children;}
Truth table50%2/4
A and B and C
A= defined $posB= $pos >= 0C= $pos <= $#{$children;}
ABCresult
0XX0
10X0
1100
1111
746 1 my $num = $#{$children};
747 1 confess "Error, $pos is an invalid position [$num] $children.\n";
748 }
749  
750 8 my $node = splice( @{$children}, $pos, 1 );
751 8 $self->{'curr_node'}->{'children'} = $children;
752  
753 # Reset handle's child cursor to avoid stale references
754 8 $self->{'curr_pos'} = undef;
755 8 $self->{'curr_child'} = undef;
756  
757 8 return ( $node->key, $node->value );
758 }
759  
760 =head2 Tree::MultiNode::Handle::child_keys
761  
762 Returns the keys from the current node's children.
763 Returns undef if there is no current node.
764  
765 =cut
766  
767 5 ▶sub child_keys {
Subroutine child_keys: called CC=2 SCAR=7.0
Pod documented
768 5 my $self = shift;
769 5 my $node = $self->{'curr_node'};
770 5 ▶ return undef unless $node;
Branch: 1 branch
BranchTrueFalseTotal
unless $node 0 5 5
771 5 return $node->child_keys();
772 }
773  
774 =head2 Tree::MultiNode::Handle::child_values
775  
776 Returns the values from the current node's children.
777 Returns undef if there is no current node.
778  
779 =cut
780  
781 0 ▶sub child_values {
Subroutine child_values: not called CC=2 SCAR=17.9
Pod documented
782 0 my $self = shift;
783 0 my $node = $self->{'curr_node'};
784 0 ▶ return undef unless $node;
Branch: 1 branch
BranchTrueFalseTotal
unless $node 0 0 0
785 0 return $node->child_values();
786 }
787  
788 =head2 Tree::MultiNode::Handle::traverse
789  
790 $handle->traverse(sub {
791 my $h = pop;
792 printf "%sk: %s v: %s\n",(' ' x $handle->depth()),$h->get_data();
793 });
794  
795 Traverse takes a subroutine reference, and will visit each node of the tree,
796 starting with the node the handle currently points to, recursively down from the
797 current position of the handle. Each time the subroutine is called, it will be
798 passed a handle which points to the node to be visited. Any additional
799 arguments after the sub ref will be passed to the traverse function _before_
800 the handle is passed. This should allow you to pass constant arguments to the
801 sub ref.
802  
803 Modifying the node that the handle points to will cause traverse to work
804 from the new node forward.
805  
806 =cut
807  
808 7 ▶sub traverse {
Subroutine traverse: called CC=2 SCAR=7.1
Pod documented
809 7 my ( $self, $subref, @args ) = @_;
810 7 ▶ confess "Error, invalid sub ref: $subref\n" unless 'CODE' eq ref($subref);
Branch: 1 branch
BranchTrueFalseTotal
unless "CODE" eq ref $subref 0 7 7
811  
812 # operate on a cloned handle
813 7 return Tree::MultiNode::Handle->new($self)->_traverseImpl( $subref, @args );
814 }
815  
816 22 ▶sub _traverseImpl {
Subroutine _traverseImpl: called CC=2 SCAR=6.9
817 22 my ( $self, $subref, @args ) = @_;
818 22 $subref->( @args, $self );
819 22 my $num_children = $self->num_children();
820 22 ▶ for ( my $i = 0; $i < $num_children; ++$i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $num_children) 15 22 37
821 15 $self->down($i);
822 15 $self->_traverseImpl( $subref, @args );
823 15 $self->up();
824 }
825 22 return;
826 }
827  
828 =head2 Tree::MultiNode::Handle::otraverse
829  
830 Like traverse(), but designed for passing an object method. The first
831 argument after the handle should be the object, the second should be
832 the method name or code reference, followed by any additional arguments.
833 The handle is passed as the last argument to the method.
834  
835 This allows you to have the subref be a method on an object (and still
836 pass the object's 'self' to the method).
837  
838 $handle->otraverse( $obj, \&Some::Object::method, $const1, \%const2 );
839  
840 ...
841 sub method
842 {
843 my $handle = pop;
844 my $self = shift;
845 my $const1 = shift;
846 my $const2 = shift;
847 # do something
848 }
849 =cut
850  
851 1 ▶sub otraverse {
Subroutine otraverse: called CC=2 SCAR=7.1
Pod documented
852 1 my ( $self, $obj, $method, @args ) = @_;
853 1 ▶ confess "Error, invalid sub ref: $method\n" unless 'CODE' eq ref($method);
Branch: 1 branch
BranchTrueFalseTotal
unless "CODE" eq ref $method 0 1 1
854  
855 # operate on a cloned handle
856 1 return Tree::MultiNode::Handle->new($self)->_otraverseImpl( $obj, $method, @args );
857 }
858  
859 4 ▶sub _otraverseImpl {
Subroutine _otraverseImpl: called CC=2 SCAR=6.9
860 4 my ( $self, $obj, $method, @args ) = @_;
861 4 $obj->$method( @args, $self );
862 4 my $num_children = $self->num_children();
863 4 ▶ for ( my $i = 0; $i < $num_children; ++$i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $num_children) 3 4 7
864 3 $self->down($i);
865 3 $self->_otraverseImpl( $obj, $method, @args );
866 3 $self->up();
867 }
868 4 return;
869 }
870  
871 17 1;
« MultiNode/Node.pm ↑ Summary MultiNode.pm »