Tree/Numbered.pm

filter: Statementstmt 65.1% 125 / 192 Branchbran 44.5% 33 / 74 Conditioncond 20.0% 1 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 73.3% 22 / 30 Podpod 88.8% 24 / 27 totaltotal 61.9% 205 / 331 CC 47 SCAR 22.6
CC
47
Cov
59%
follow
42.0
getSubTree
30.0
delete
12.0
CRAP 9.6
« Tree/Numbered/DB.pm ↑ Summary Javascript/Menu.pm »
1 package Tree::Numbered;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 our $VERSION = '1.00';
6  
7 3 our @counters = (); # For getting new serial numbers.
8  
9 12 ▶sub getNewSerial {
Subroutine getNewSerial: called CC=2 SCAR=6.9
Pod undocumented
10 12 my $lucky_number = shift;
11 12 ▶ $counters[$lucky_number] = 1 unless (exists $counters[$lucky_number]);
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Tree::Numbered::counters[$lucky_number] 3 9 12
12 12 return ($counters[$lucky_number]++);
13 }
14  
15 5 ▶sub getNewLucky {
Subroutine getNewLucky: called CC=1 SCAR=0.0
Pod undocumented
16 5 $#counters++;
17 5 return $#counters;
18 }
19  
20 # predeclare subs used in <new>.
21 sub addField;
22  
23 # <new> constructs a new tree or node.
24 # Arguments: $value - the default value to be stored in the node.
25 # %extra - extra fields as name => value.
26 # Returns: The tree object.
27  
28 12 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
29 12 my ($parent, $value, $second, %extra) = @_;
30 12 my $class;
31
32 12 my $properties = {
33 Items => [],
34 Cursor => -1,
35 };
36  
37
38 12 ▶ if ($class = ref($parent)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class = ref $parent) { } 9 3 12
39 # Give it the same number as its parent.
40 9 $properties->{_LuckyNumber} = $parent->{_LuckyNumber};
41 9 $properties->{_ParentRef} = $parent;
42 9 bless $properties, $class;
43  
44 # Inherit fields from parent (unless assigned in argument list).
45 $properties->addField($_, $parent->{$_})
46 9 foreach ($parent->getFieldNames);
47 } else {
48 3 $class = $parent;
49 3 $properties->{_LuckyNumber} = getNewLucky;
50 3 bless $properties, $class
51 }
52 12 $properties->{_Serial} = getNewSerial($properties->{_LuckyNumber});
53  
54 # Detect parameter passing style for backward compatibility.
55 12 ▶ if (scalar @_ > 2) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @_ > 2) { } 2 10 12
elsif (scalar @_ == 2) { } 9 1 10
56 2 $extra{$value} = $second;
57 } elsif (scalar @_ == 2) {
58 9 ▶ $extra{Value} = $value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 9 0 9
59 }
60  
61 # Add requested fields, default to 'Value'.
62 12 foreach (keys %extra) {
63 # Calls are separated to override inherited settings.
64 13 $properties->addField($_);
65 13 $properties->setField($_, $extra{$_});
66 }
67 12 return $properties;
68 }
69  
70 # <nextNode> moves the cursor forward by one.
71 # Arguments: None.
72 # Returns: Whatever is pointed by the cursor, undef on overflow, first item
73 # on subsequent overflow.
74  
75 26 ▶sub nextNode {
Subroutine nextNode: called CC=4 SCAR=13.9
Pod documented
76 26 my $self = shift;
77  
78 26 my $cursor = $self->{Cursor};
79 26 my $length = $self->childCount;
80 26 $cursor++;
81  
82 # return undef when end of iterations. On next call - reset counter.
83 26 ▶ if ($cursor > $length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cursor > $length) 1 25 26
84 1 ▶ $cursor = ($length) ? 0 : -1;
Branch: 1 branch
BranchTrueFalseTotal
$length ? : 1 0 1
85 }
86 26 $self->{Cursor} = $cursor;
87  
88 26 ▶ if (exists $self->{Items}->[$cursor]) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"Items"}[$cursor]) 17 9 26
89 17 return $self->{Items}->[$cursor];
90 }
91 9 return undef;
92 }
93  
94 # <reset> returns the counter to the beginning of the list.
95 # Arguments: None.
96 # Returns: Nothing.
97  
98 8 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
99 8 my $self = shift;
100 8 $self->{Cursor} = -1;
101 }
102  
103 # <delete> deletes the item pointed to by the cursor.
104 # The curser is not changed, which means it effectively moves to the next item.
105 # However it does change to be just after the end if it is already there,
106 # so you won't get an overflow.
107 # Arguments: None.
108 # Returns: The deleted item or undef if none was deleted.
109  
110 0 ▶sub delete {
Subroutine delete: not called CC=3 SCAR=24.8
Pod documented
111 0 my $self = shift;
112 0 my $cursor = $self->{Cursor};
113
114 0 ▶ if (exists $self->{Items}->[$cursor]) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"Items"}[$cursor]) 0 0 0
115 0 my $deleted = splice(@{$self->{Items}}, $cursor, 1);
116  
117 # Make sure the cursor doesn't overflow:
118 0 ▶ if ($cursor > $self->childCount) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cursor > $self->childCount) 0 0 0
119 0 $self->{Cursor} = $self->childCount;
120 }
121 0 return $deleted;
122 }
123  
124 0 return undef;
125 }
126  
127 # <append> adds a node at the end of the list.
128 # Arguments: parameters for new.
129 # Returns: The added node or undef on error.
130  
131 9 ▶sub append {
Subroutine append: called CC=2 SCAR=7.0
Pod documented
132 9 my $self = shift;
133 9 my $newNode = $self->new(@_);
134 9 ▶ return undef unless $newNode;
Branch: 1 branch
BranchTrueFalseTotal
unless $newNode 0 9 9
135  
136 9 push @{$self->{Items}}, $newNode;
137 9 return $newNode;
138 }
139  
140 # <savePlace> saves the place of the cursor.
141 # Arguments: None.
142 # Returns: Nothing.
143  
144 8 ▶sub savePlace {
Subroutine savePlace: called CC=1 SCAR=0.0
Pod documented
145 8 my $self = shift;
146 8 $self->{Saved} = $self->{Cursor};
147 }
148  
149 # <restorePlace> returns the cursor to its saved place if any. The place is
150 # still saved untill the save is changed.
151 # Arguments: None.
152 # Returns: 1 if restored, undef otherwise.
153  
154 8 ▶sub restorePlace {
Subroutine restorePlace: called CC=3 SCAR=12.7
Pod documented
155 8 my $self = shift;
156 8 ▶ if (exists($self->{Saved}) && ($self->{Saved} <= $self->childCount)) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"Saved"} and $self->{"Saved"} <= $self->childCount) 8 0 8
Condition33%1/3
exists $self->{"Saved"} and $self->{"Saved"} <= $self->childCount
!ll&&!rl&&r
008
MC/DC0%0/2
exists $self->{"Saved"} and $self->{"Saved"} <= $self->childCount
exists $self->{"Saved"} $self->{"Saved"} <= $self->childCount
Truth table33%1/3
A and B
A= exists $self->{"Saved"}B= $self->{"Saved"} <= $self->childCount
ABresult
0X0
100
111
157 8 $self->{Cursor} = $self->{Saved};
158 8 return 1;
159 }
160 0 return undef;
161 }
162  
163 # <clone> returns a new tree that is the same as the cloned one except for
164 # its lucky number.
165 # Arguments: None.
166 # Returns: None.
167  
168 8 ▶sub clone {
Subroutine clone: called CC=3 SCAR=11.0
Pod documented
169 8 my $self = shift;
170 8 my ($lucky_number, $parent) = @_;
171  
172 8 ▶ unless (defined $lucky_number) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $lucky_number) 2 6 8
173 2 $lucky_number = getNewLucky;
174 2 $counters[$lucky_number] = $counters[$self->{_LuckyNumber}];
175 }
176  
177 8 my $cloned = {};
178 8 $cloned->{$_} = $self->{$_} foreach (keys %$self);
179 8 $cloned->{_ParentRef} = $parent;
180 8 $cloned->{_LuckyNumber} = $lucky_number;
181 6 $cloned->{Items} = [map {$_->clone($lucky_number, $cloned)}
182 8 @{ $self->{Items} }];
183
184 8 return bless $cloned, ref($self);
185 }
186  
187 # <deepProcess> runs a given subroutine on all descendants of a node.
188 # Arguments: $subref - the sub to be run.
189 # all remaining arguments will be passed to the subroutine,
190 # prepended by a ref to the node being processed.
191 # Returns: Nothing.
192  
193 4 ▶sub deepProcess {
Subroutine deepProcess: called CC=2 SCAR=6.9
Pod documented
194 4 my $self = shift;
195 4 my ($subref, @args) = @_;
196  
197 # I do not use the savePlace + reset metods, because the subroutine
198 # passed by the user may mess it up.
199 4 foreach my $child (@{ $self->{Items} }) {
200 3 $subref->($child, @args);
201 3 $child->deepProcess($subref, @args);
202 }
203 }
204  
205 # <allProcess> does the same as deepProcess except that it also processes the
206 # root element.
207 # Arguments: see <deepProcess>.
208 # Returns: Nothing.
209  
210 1 ▶sub allProcess {
Subroutine allProcess: called CC=1 SCAR=0.0
Pod documented
211 1 my $self = shift;
212 1 my ($subref, @args) = @_;
213
214 1 $subref->($self, @args);
215 1 $self->deepProcess($subref, @args);
216 }
217  
218 #*******************************************************************
219 # Accessors:
220  
221 46 ▶sub childCount {
Subroutine childCount: called CC=1 SCAR=0.0
Pod documented
222 46 my $self = shift;
223 46 return scalar @{$self->{Items}};
224 }
225  
226 # There is no setNumber because numbers are handled only by the object.
227 0 ▶sub getNumber {
Subroutine getNumber: not called CC=1 SCAR=6.9
Pod documented
228 0 my $self = shift;
229 0 return $self->{_Serial};
230 }
231  
232 # same for LuckyNumber.
233 4 ▶sub getLuckyNumber {
Subroutine getLuckyNumber: called CC=1 SCAR=0.0
Pod documented
234 4 my $self = shift;
235 4 return $self->{_LuckyNumber};
236 }
237  
238 3 ▶sub getParentRef {
Subroutine getParentRef: called CC=1 SCAR=0.0
Pod documented
239 3 my $self = shift;
240 3 return $self->{_ParentRef};
241 }
242  
243 # <getFieldNames> returns a list of fields for an object, not including Value.
244 # Arguments: None.
245 # Returns: The list in list context, a ref to it in scalar context.
246  
247 32 ▶sub getFieldNames {
Subroutine getFieldNames: called CC=3 SCAR=11.8
Pod documented
248 32 my $self = shift;
249 32 ▶ return undef unless ($self->{Fields});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Fields"} 0 32 32
250
251 32 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 32 0 32
252 32 return @{ $self->{Fields} };
253 } else {
254 0 return $self->{Fields};
255 }
256 }
257  
258 # <hasField> is a boolean function to determine if a field exists.
259 # Arguments: $name - field name.
260 # Returns: True if the field exists, undef if not.
261  
262 22 ▶sub hasField {
Subroutine hasField: called CC=3 SCAR=11.0
Pod undocumented
263 22 my $self = shift;
264 22 my $name = shift;
265  
266 22 for ($self->getFieldNames) {
267 34 ▶ return 1 if ($_ eq $name);
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq $name 20 14 34
268 }
269 2 return undef;
270 }
271  
272 # <get/setField> accesses a field by name.
273 # Arguments: $name - field name.
274 # $value - field value.
275 # Returns: Field value on success, undef otherwise.
276  
277 4 ▶sub getField {
Subroutine getField: called CC=2 SCAR=7.6
Pod documented
278 4 my $self = shift;
279 4 my $name = shift;
280  
281 4 ▶ return $self->{$name} if $self->hasField($name);
Branch: 1 branch
BranchTrueFalseTotal
if $self->hasField($name) 4 0 4
282 0 return undef;
283 }
284  
285 14 ▶sub setField {
Subroutine setField: called CC=2 SCAR=7.6
Pod documented
286 14 my $self = shift;
287 14 my ($name, $value) = @_;
288  
289 14 ▶ return $self->{$name} = $value if $self->hasField($name);
Branch: 1 branch
BranchTrueFalseTotal
if $self->hasField($name) 14 0 14
290 0 return undef;
291 }
292  
293 # <getFields> returns a hash ref of field => value for each extra field.
294 # Arguments: None.
295 # Returns: See above.
296  
297 0 ▶sub getFields {
Subroutine getFields: not called CC=2 SCAR=17.9
Pod documented
298 0 my $self = shift;
299 0 ▶ return undef unless ($self->{Fields});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Fields"} 0 0 0
300  
301 0 my %fields = map { $_ => $self->getField($_) } $self->getFieldNames;
302 0 return \%fields;
303 }
304  
305 0 ▶sub setFields {
Subroutine setFields: not called CC=3 SCAR=24.8
Pod documented
306 0 my $self = shift;
307 0 ▶ return undef unless ($self->{Fields});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Fields"} 0 0 0
308  
309 0 my (%fields) = @_;
310 0 $self->setField($_, $fields{$_}) foreach (keys %fields);
311 }
312  
313 # <addField> adds an extra field to the object.
314 # Arguments: $name - field name.
315 # $value - field value.
316 # Returns: True on success, undef on failure (field exists).
317  
318 26 ▶sub addField {
Subroutine addField: called CC=2 SCAR=6.9
Pod documented
319 26 my $self = shift;
320 26 my ($name, $value) = @_;
321
322 # Fail if field exists or is a predefined field.
323 26 ▶ return undef if (exists $self->{$name});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{$name} 8 18 26
324
325 18 push @{ $self->{Fields} }, $name;
326 18 $self->{$name} = $value;
327 18 return 1;
328 }
329  
330 # <removeField> removes an extra field from the object.
331 # Arguments: $name - field name.
332 # Returns: True on success, undef on failure (field doesn't exist).
333  
334 0 ▶sub removeField {
Subroutine removeField: not called CC=3 SCAR=24.8
Pod documented
335 0 my $self = shift;
336 0 my $name = shift;
337  
338 0 my @fields = $self->getFieldNames;
339 0 for my $i (0..$#fields) {
340 0 ▶ if ($fields[$i] eq $name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields[$i] eq $name) 0 0 0
341 0 delete $self->{$name};
342 0 splice @fields, $i, 1;
343 0 $self->{Fields} = \@fields;
344 0 return 1;
345 }
346 }
347 0 return undef;
348 }
349  
350 #***********************************************************************
351 # scary: AUTOLOAD captures access requests for fields.
352  
353 3 our $AUTOLOAD;
354  
355 1 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=5 SCAR=23.1
356 1 my $self = shift;
357 1 $AUTOLOAD =~ s/(.*):://;
358 1 my $pkg = $1;
359  
360 1 ▶ if ($AUTOLOAD =~ /get(.*)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($Tree::Numbered::AUTOLOAD =~ /get(.*)$/) { } 1 0 1
elsif ($Tree::Numbered::AUTOLOAD =~ /set(.*)$/) { } 0 0 0
361 1 ▶ return $self->getField($1) if $self->hasField($1);
Branch: 1 branch
BranchTrueFalseTotal
if $self->hasField($1) 1 0 1
362 0 die ("No such field ($1) in tree");
363 } elsif ($AUTOLOAD =~ /set(.*)$/) {
364 0 ▶ return $self->setField($1, shift) if $self->hasField($1);
Branch: 1 branch
BranchTrueFalseTotal
if $self->hasField($1) 0 0 0
365 0 die ("No such field ($1) in tree");
366 }
367  
368 0 die "Can't call method $AUTOLOAD via package $pkg";
369 }
370  
371 0 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
372 #***************************************************************************
373 # Service methods.
374  
375 # <getSubTree> returns the sub tree whose root element's serial number
376 # is requested.
377 # Arguments: $serial - the requested serial number.
378 # Returns - the matching object if it's there, undef otherwise.
379  
380 0 ▶sub getSubTree {
Subroutine getSubTree: not called CC=5 SCAR=34.0
Pod documented
381 0 my ($self, $serial) = @_;
382 0 ▶ return $self if ($serial == $self->getNumber);
Branch: 1 branch
BranchTrueFalseTotal
if $serial == $self->getNumber 0 0 0
383
384 0 $self->savePlace;
385 0 $self->reset;
386  
387 0 ▶ while (my $branch = $self->nextNode) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $branch = $self->nextNode) 0 0 0
388 0 ▶ if ($branch->getNumber == $serial) {
Branch: 2 branches
BranchTrueFalseTotal
if ($branch->getNumber == $serial) { } 0 0 0
elsif (my $subtree = getSubTree($branch, $serial)) { } 0 0 0
389 0 $self->restorePlace;
390 0 return $branch;
391 } elsif (my $subtree = getSubTree($branch, $serial)) {
392 0 $self->restorePlace;
393 0 return $subtree;
394 }
395 }
396 0 $self->restorePlace;
397 0 return undef;
398 }
399  
400 # <listChildNumbers> returns a list of serial numbers of all items under
401 # an item whose serial number is given as an argument.
402 # Arguments: $serial - denoting the item requested.
403  
404 8 ▶sub listChildNumbers {
Subroutine listChildNumbers: called CC=4 SCAR=13.9
Pod documented
405 8 my $self = shift;
406 8 my $serial = shift;
407  
408 8 my @subSerials = ();
409 8 ▶ my $subtree = ($serial) ? getSubTree($self, $serial) : $self;
Branch: 1 branch
BranchTrueFalseTotal
$serial ? : 0 8 8
410  
411 8 $subtree->savePlace;
412 8 $subtree->reset;
413
414 8 ▶ while (my $branch = $subtree->nextNode) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $branch = $subtree->nextNode) 12 8 20
415 12 push @subSerials, $branch->{_Serial};
416
417 12 ▶ if ($branch->childCount > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($branch->childCount > 0) 4 8 12
418 4 push @subSerials, $branch->listChildNumbers;
419 }
420 }
421  
422 8 $subtree->restorePlace;
423 8 return @subSerials;
424 }
425  
426 # <follow> Will find an item in a tree by its serial number
427 # and return a list of all values for a requested field up to and including
428 # the requested one.
429 # Arguments: $serial - number of target node.
430 # $field - field name (defauld - Value)
431  
432 0 ▶sub follow {
Subroutine follow: not called CC=6 SCAR=37.4
Pod documented
433 0 my $self = shift;
434 0 my ($serial, $field) = @_;
435 0 ▶ $field ||= 'Value'; # backward compatibility.
Condition0%0/2
$field ||= "Value"
l!l
00
MC/DC0%0/1
$field ||= "Value"
$field
Truth table0%0/2
A
A= $field
Aresult
11
00
436  
437 0 $self->savePlace;
438 0 $self->reset;
439
440 0 ▶ while (my $branch = $self->nextNode) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $branch = $self->nextNode) 0 0 0
441 0 my @patharray = ();
442 0 ▶ if ($branch->{_Serial} == $serial) {
Branch: 2 branches
BranchTrueFalseTotal
if ($branch->{'_Serial'} == $serial) { } 0 0 0
elsif ($branch->childCount) { } 0 0 0
443 0 $self->restorePlace;
444 0 return ($branch->getField($field));
445 } elsif ($branch->childCount) {
446 0 @patharray = $branch->follow($serial, $field);
447 }
448
449 0 ▶ if ($#patharray >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#patharray >= 0) 0 0 0
450 # Parent nodes go first:
451 0 unshift @patharray, $branch->getField($field);
452 0 $self->restorePlace;
453 0 return @patharray;
454 }
455 }
456  
457 0 $self->restorePlace;
458 0 return ();
459 }
460  
461 3 1;
462  
463 =head1 NAME
464  
465 Tree::Numbered - a thin N-ary tree structure with a unique number for each item.
466  
467 =head1 SYNOPSYS
468  
469 use Tree::Numbered;
470 my $tree = Tree::Numbered->new('John Doe');
471 $tree->append('John Doe Jr.');
472 $tree->append('Marry-Jane Doe');
473  
474 while (my $branch = $tree->nextNode) {
475 $branch->delete if ($branch->getValue eq 'Stuff I dont want');
476 }
477
478 my $itemId = what_the_DB_says;
479 print join ' --- ', $tree->follow($itemId); # a list of items up to itemId.
480
481 $tree->allProcess( sub {
482 my $self = shift;
483 $self->getValue =~ /^(\S*)/;
484 $self->addField('FirstName', $1);
485 } );
486  
487 etc.
488  
489 =head1 DESCRIPTION
490  
491 Tree::Numbered is a special N-ary tree with a number for each node. This is useful on many occasions. The first use I found for that (and wrote this for) was to store information about the selected item as a number instead of storing the whole value which is space-expensive.
492  
493 Every tree also has a lucky number of his own that distinguishes it from other trees created by the same module. This module is thin on purpose and is meant to be a base class for stuff that can make use of this behaveiour. For example, I wrote Tree::Numbered::DB which ties a tree to a table in a database, and Javascript::Menu which uses this tree to build menus for websites.
494  
495 One more feature that the module implements for the ease of subclassing it is an API for adding and removing fields from trees and nodes.
496  
497 =head1 BUILDING AND DESTROYING A TREE
498  
499 =over 4
500  
501 =item Tree::Numbered->new
502  
503 There is only one correct way to start an independent tree with its own lucky number from scratch: calling the class function I<new>. Using new as a method is wrong because it will create a node for the same tree but the tree won't know of its existence. See below.
504  
505 There are two forms of calling I<new>. The first is calling it with one argument only - the value for the node. If you do that, the tree / node will be created with one field, called 'Value', and this field will receive the value you supplied as an argument.
506  
507 The second form is calling I<new> with a hash of <field name> => <field value> pairs. For each pair, a field by the name <field name> be created, and it's value will be <field value>. The Value field will not be created unless you specifically request it.
508  
509 Note: Calling I<new> with no arguments is the same as calling it in the second form.
510  
511 =item $tree->clone
512  
513 Another way to obtain a new tree object is to clone an existing one. The clone method does that. The original object remains untouched.
514  
515 =item $tree->append
516  
517 This is the correct way to add an item to a tree or a branch thereof. Internally uses $tree->new but does other stuff. The arguments for I<append> are the same as for I<new>.
518  
519 =item $tree->delete
520  
521 Deletes the child pointed to by the cursor (see below) and returns the deleted item. Note that it becomes risky to use this item since its parent tree knows nothing about it from the moment it is deleted and you can cause collisions so use with caution.
522  
523 =back
524  
525 =head1 WORKING WITH FIELDS
526  
527 As this module is designed for subclassing, I added a mechanism to easily create fields in a tree. Fields are different from normal object attributes in that they are specifically registered as fields within their object, and can be added, removed, and querried for existence. For every field a set of accessors is auto created (actually, autoloaded). In the section 'Subclassing Issues' there's more on using fields vs. regular attributes.
528  
529 One more important thing to know about fields is that every node inherits all fields that his parent had at the moment of creation. The value of the field is also inherited unless the field was requested in the argument list for I<append>, in which case it takes the value provided as an argument.
530  
531 Naming your fields: If you want to make use of the automatic accessors, you might want to name your fields with a capital first letter. This is because the accessors created for a field are nothing more then the name of the field prefixed with either 'get' or 'set'. Alternatively, use underscore-lower letter if that's your style.
532  
533 =head2 But what if I only need one field for storing some value?
534  
535 No problem - The module knows of a default field called 'Value' which is created if you only give one argument - the value - to I<new> or I<append>. When you build a tree like that, you'll have the methods getValue and setValue. Furthermore, the method I<follow> guesses that you want this field if you do not specify any other. So working with one field only is just a short-code version of working with many.
536  
537 =head2 Methods for working with fields:
538  
539 =over 4
540  
541 =item getFieldNames
542  
543 Returns a list of all registered fields within a node.
544  
545 =item addField(name, [value])
546  
547 Adds a field I<only> to the node on which the method was invoked. if value is not specified, the field's value will default to undef. If the field does not exist, will do nothing - won't even set its value.
548  
549 If you need to add a field to all existing descendants of a node (future ones inherit the field automatically) use either I<deepProcess> or I<allProcess> as described above in 'Synopsys'.
550  
551 Returns: True on success, undef on failure.
552  
553 =item removeField(name)
554  
555 Removes the field by that name, and deletes its value.
556  
557 Returns: True on success, undef on failure.
558  
559 =item getField(name)
560  
561 Returns the value of the field given in the name argument. if the field does not exist returns undef. If you want to check if the field exists, use I<hasField> or try to call the automatic getter for that field, an attempt that will cause a painfull death if there's no such field.
562  
563 =item setField(name, [value])
564  
565 Sets the value of the requested field. If value is not specified, undef is assumed. Returns the new value on success, undef on failure (field does not exist).
566  
567 =item getFields
568  
569 Returns a reference to a hash of Field => Value pairs, for each field the node owns.
570  
571 =item setFields([Field => Value, Field => Value, ...])
572  
573 Sets the requested fields to the requested values. Keeps quiet if a field does not exist, so watch out.
574  
575 =back
576  
577 =head1 ITERATING OVER CHILD ITEMS
578  
579 =head2 The cursor
580  
581 Every node in the tree has its own cursor that points to the current item. When you start iterating, the cursor is placed just B<before> the first child. When you are at the last item, trying to move beyond the last item will put the
582 cursor B<after> the last item (which will result in an undef value, signalling the end) but the next attempt will cause the cursor to B<start over> from the first child.
583  
584 =head2 Methods for iteration
585  
586 =over 4
587  
588 =item nextNode
589  
590 Moves the cursor one child forward and returns the pointed child.
591  
592 =item reset
593  
594 Resets the cursor to point before the first child.
595  
596 =item savePlace
597  
598 Allows you to save the cursor's place, do stuff, then go back. There is only one save, though, so don't try to nest saves.
599  
600 =item restorePlace
601  
602 Tries to set the cursor to the item at the same place as it was when its place was saved and returns true on success. If the saved place doesn't exist anymore returns undef. Note: If you deleted items from the tree you might not get to the right place.
603  
604 =head1 ACCESSORS
605  
606 The following accessors are always available and deal with the node's properties, not with fields:
607  
608 =over 4
609  
610 =item getNumber
611  
612 Gets the number of the node within its tree. There is no setter since this number is special.
613  
614 =item getLuckyNumber
615  
616 Gets the number of the main tree that the node is a member of. Again, there is no setter since this is special.
617  
618 =item getParentRef
619  
620 Returns a reference to the parent of the node (of course the root will return undef).
621  
622 =item childCount
623  
624 Gets the number of childs for this node.
625  
626 =back
627  
628 Also, as described above, for each field you create, a getter and a setter automatically show up when needed. For each field *, you'll have the methods get* and set*.
629  
630 =head1 THINGS YOU CAN DO WITH NODE NUMBERS
631  
632 Well, I didn't include the node numbers just for fun, this is actually very needed sometimes. There are three basic service methods that use this (subclasses may add to this):
633  
634 =over 4
635  
636 =item getSubTree([number])
637  
638 If a node who's number is given is a member of the subtree that the method was invoked on, that node will be returned, undef otherwise. To be consistent with set theory, any tree is considered to be its own child, so giving the number of the root node will return that node.
639  
640 =item listChildNumbers([number])
641  
642 returns a list of all numbers of nodes that are decendants (any level) of the subtree whose number is given. Number is optional, the node's own number is used if no number is specifically requested.
643  
644 =item follow(number, [field])
645  
646 returns a list of all field values starting from the decendant node with the requested number, through every parent of the node and up to the node the method was invoked on. If no such node exists, returns an empty list. If no field is specified, defaults to the Value field.
647  
648 =back
649  
650 =head1 OTHER METHODS
651  
652 There are two methods that apply a certain subroutine to whole trees:
653  
654 =over 4
655  
656 =item deepProcess (SUBREF, ARG, ARG, ...)
657  
658 For each child of the node, runs the subroutine referenced by SUBREF with an argument list that starts with a reference to the child being processed and continues with the rest of the arguments passed to deepProcess. In short, your subroutine is called as a method of the child items, so you can shift $self out of the arguments list and use it.
659  
660 =item allProcess (SUBREF, ARG, ARG, ...)
661  
662 does the same as deepProcess but runs on the root node first.
663  
664 =back
665  
666 =head1 SUBCLASSING ISSUES
667  
668 =over 4
669  
670 =item Fields vs. normal attributes
671  
672 The usual implementation of a data field is as a hash key, either in one hash per object, or one hash per property (an inside-out object). The fields mechanism allows you to maintain a list of fields, but assumes that the fields are regular data fields. My subclasses of this modules use Fields for regular data, but do not register as fields anything with 'magical' or 'internal use only' data. The reason is that not using fields gives more control over the behaveiour of these fields.
673  
674 For example, if you implement a property using the fields mechanism, and you don't want the user to access it, you'll have to manually override the automatic accessors to die or do something unpleasant when they're used.
675  
676 Secondly, if you register a property as a field - it is easy to remove it by accident even if you count on it to always exist. That's bad, isn't it?
677  
678 And lastly, if you count on a value to be frequently used, you might not want the overhead of autoloading the accessors or you don't need it in the list of fields.
679  
680 The benefits of using fields, are in the easiness of managing a set of fields acording to changing demands and the simplicity of extending the behaveiour of a class. For example, check out my Javascript::Menu which creates the URL field if the user is just trying to build a navigational menu, but doesn't bother if the user uses the more complex, action based menus that I designed the module for.
681  
682 =item Setting attributes that have no setters
683  
684 You'll notice that none of the constant properties of this class (the number, lucky number and parent ref) have a setter. This is on purpose. These properties are determined automatically and are not supposed to be messed with.
685  
686 If you do want to change the behaveiour, I assume that you know what you're doing and that you have read some source. Then you'll have no problem with implementing your desired behaveiour.
687  
688 =item Extending field behaveiour
689  
690 The fields mechanism, however, is more object-oriented. Everything that has to do with fields tries to use field methods even internally. Even I<new> calls addField to add fields. The main implication of this (that I found) is that if you want things to happen when a field is created, you must make sure that the data for this happening (if any is required) is already available at the time of adding the field. For an example of that, see how my Tree::Numbered::DB makes sure that a field mapping will always be available, before calling SUPER::new in its own constructor.
691  
692 =back
693  
694 =head1 BUGS AND PROBLEMS
695  
696 Works pretty well for me. If you found anything, please send a bug report:
697 E<lt>http://rt.cpan.org/NoAuth/Bugs.html?Dist=Tree-NumberedE<gt>
698 or send mail to E<lt>bug-Tree-Numbered#rt.cpan.orgE<gt>
699  
700 =head1 SEE ALSO
701  
702 Tree::Numbered::DB, Javascript::Menu
703  
704 =head1 AUTHOR
705  
706 Yosef Meller, E<lt> mellerf@netvision.net.il E<gt>
707  
708 =head1 COPYRIGHT AND LICENSE
709  
710 Copyright 2003 by Yosef Meller
711  
712 This library is free software; you can redistribute it and/or modify
713 it under the same terms as Perl itself.
714  
715 =cut
« Tree/Numbered/DB.pm ↑ Summary Javascript/Menu.pm »