line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Tree::Builder; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
19959
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
29
|
|
4
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
665
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
=head1 NAME |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
Tree::Builder - Takes path like strings and builds a tree of hashes of hashes. |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 VERSION |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
Version 0.1.0 |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=cut |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our $VERSION = '0.1.0'; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 SYNOPSIS |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
use Tree::Builder; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
my $tb = Tree::Builder->new(); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
$tb->add('a/b/c'); |
25
|
|
|
|
|
|
|
$tb->add('some/thing'); |
26
|
|
|
|
|
|
|
$tb->add('a/some/thing'); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
my %tree=$tb->getTree; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
print $tb->getSeperator; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
$tb->setSeperator('\.'); |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
$tb->add('what.ever'); |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
#prints it using Data::Dumper |
37
|
|
|
|
|
|
|
use Data::Dumper; |
38
|
|
|
|
|
|
|
print Dumper(\%tree); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=head1 METHODS |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=head2 new |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
This initializes the object. |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head3 args hash ref |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=head4 seperator |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
This is the seperator, regexp, to use for breaking a string down |
52
|
|
|
|
|
|
|
and hadding it to the tree. |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
If not specified, the default is '\/'. |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
Be warned, this is a regular expression, so if you don't want it to |
57
|
|
|
|
|
|
|
act as such, you will want to use quotemeta. |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
#initiates it with the defaults |
60
|
|
|
|
|
|
|
my $tb=Tree::Builder->new; |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
#initiaties it with a seperator of . |
63
|
|
|
|
|
|
|
my $tb=Tree::Builder->new({seperator=>'\.'}); |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=cut |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub new { |
68
|
0
|
|
|
0
|
1
|
|
my %args; |
69
|
0
|
0
|
|
|
|
|
if(defined($_[1])){ |
70
|
0
|
|
|
|
|
|
%args= %{$_[1]}; |
|
0
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
0
|
0
|
|
|
|
|
if (defined($args{seperator})) { |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
0
|
|
|
|
|
|
my $self={error=>undef, errorString=>undef, tree=>{}}; |
77
|
0
|
|
|
|
|
|
bless $self; |
78
|
|
|
|
|
|
|
|
79
|
0
|
0
|
|
|
|
|
if (!defined($args{seperator})) { |
80
|
0
|
|
|
|
|
|
$self->{seperator}='/'; |
81
|
|
|
|
|
|
|
}else { |
82
|
0
|
|
|
|
|
|
$self->{seperator}=$args{seperator}; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
0
|
|
|
|
|
|
return $self |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=head2 add |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
This adds a new item to the tree. |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
In regards to error checking, there is no need to check this for |
94
|
|
|
|
|
|
|
errors as long as you make sure that the string passed to it is |
95
|
|
|
|
|
|
|
defined. |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
$tb->add("some/thing"); |
98
|
|
|
|
|
|
|
if($tb->{error}){ |
99
|
|
|
|
|
|
|
print "Error!\n"; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=cut |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub add{ |
105
|
0
|
|
|
0
|
1
|
|
my $self=$_[0]; |
106
|
0
|
|
|
|
|
|
my $item=$_[1]; |
107
|
|
|
|
|
|
|
|
108
|
0
|
|
|
|
|
|
$self->errorblank; |
109
|
|
|
|
|
|
|
|
110
|
0
|
0
|
|
|
|
|
if (!defined($item)) { |
111
|
0
|
|
|
|
|
|
$self->{error}=2; |
112
|
0
|
|
|
|
|
|
$self->{errorString}="Item is not defined"; |
113
|
0
|
|
|
|
|
|
warn('Tree-Builer add:'.$self->error.': '.$self->errorString); |
114
|
0
|
|
|
|
|
|
return undef; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
0
|
|
|
|
|
|
my @itemA=split(/$self->{seperator}/, $item); |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
#this initializes the first part of the tree |
120
|
0
|
0
|
|
|
|
|
if (!defined($self->{tree}{$itemA[0]})) { |
121
|
0
|
|
|
|
|
|
$self->{tree}{$itemA[0]}={}; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
#if item does not exist, return |
125
|
0
|
0
|
|
|
|
|
if (!defined($itemA[1])) { |
126
|
0
|
|
|
|
|
|
return 1; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
0
|
|
|
|
|
|
my %newhash=%{$self->{tree}{$itemA[0]}}; |
|
0
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
|
131
|
0
|
|
|
|
|
|
my %newhash2=$self->addSub(\%newhash, \@itemA, 1); |
132
|
|
|
|
|
|
|
|
133
|
0
|
|
|
|
|
|
$self->{tree}{$itemA[0]}=\%newhash2; |
134
|
|
|
|
|
|
|
|
135
|
0
|
|
|
|
|
|
return 1; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head2 addSub |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
This is a internal function. |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=cut |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
sub addSub{ |
145
|
0
|
|
|
0
|
1
|
|
my $self=$_[0]; |
146
|
0
|
|
|
|
|
|
my %hash=%{$_[1]}; |
|
0
|
|
|
|
|
|
|
147
|
0
|
|
|
|
|
|
my @itemA=@{$_[2]}; |
|
0
|
|
|
|
|
|
|
148
|
0
|
|
|
|
|
|
my $int=$_[3]; |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
#return the hash if none others are defined |
151
|
0
|
0
|
|
|
|
|
if (!defined($itemA[$int])) { |
152
|
0
|
|
|
|
|
|
return %hash; |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
#add a new hash if it does not already exist |
156
|
0
|
0
|
|
|
|
|
if (!defined($hash{$itemA[$int]})) { |
157
|
0
|
|
|
|
|
|
$hash{$itemA[$int]}={}; |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
0
|
|
|
|
|
|
my %newhash=%{$hash{$itemA[$int]}}; |
|
0
|
|
|
|
|
|
|
161
|
0
|
|
|
|
|
|
my $newint=$int + 1; |
162
|
|
|
|
|
|
|
|
163
|
0
|
|
|
|
|
|
my %newhash2=$self->addSub(\%newhash, \@itemA, $newint); |
164
|
|
|
|
|
|
|
|
165
|
0
|
|
|
|
|
|
$hash{$itemA[$int]}=\%newhash2; |
166
|
|
|
|
|
|
|
|
167
|
0
|
|
|
|
|
|
return %hash; |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=head2 getSeperator |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
This gets the current seperator being used. |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
Error checking does not need to be done on this. |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
my $seperator=$tb->getSeperator; |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=cut |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
sub getSeperator{ |
181
|
0
|
|
|
0
|
1
|
|
return $_[0]->{seperator}; |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head2 getTree |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
This fetches the tree. |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
my %hash=$tb->getTree; |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=cut |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
sub getTree{ |
193
|
0
|
|
|
0
|
1
|
|
return %{$_[0]->{tree}}; |
|
0
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=head2 setSeperator |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
As long as this is defined, there is no need to check if it errored or not. |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
$tb->setSeperator('\/'); |
201
|
|
|
|
|
|
|
if($tb->{error}){ |
202
|
|
|
|
|
|
|
print "Error!\n"; |
203
|
|
|
|
|
|
|
} |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
=cut |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
sub setSeperator{ |
208
|
0
|
|
|
0
|
1
|
|
my $self=$_[0]; |
209
|
0
|
|
|
|
|
|
my $seperator=$_[1]; |
210
|
|
|
|
|
|
|
|
211
|
0
|
|
|
|
|
|
$self->errorblank; |
212
|
|
|
|
|
|
|
|
213
|
0
|
0
|
|
|
|
|
if (!defined($seperator)) { |
214
|
0
|
|
|
|
|
|
$self->{error}=1; |
215
|
0
|
|
|
|
|
|
$self->{errorString}='No seperator specified'; |
216
|
0
|
|
|
|
|
|
warn('Tree-Builder setSeperator:'.$self->error.': '.$self->errorString); |
217
|
0
|
|
|
|
|
|
return undef; |
218
|
|
|
|
|
|
|
} |
219
|
|
|
|
|
|
|
|
220
|
0
|
|
|
|
|
|
$self->{seperator}=$seperator; |
221
|
|
|
|
|
|
|
|
222
|
0
|
|
|
|
|
|
return 1; |
223
|
|
|
|
|
|
|
} |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
=head1 ERROR RELATED METHODS |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=head2 error |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
Returns the current error code and true if there is an error. |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
If there is no error, undef is returned. |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
if($tb->error){ |
234
|
|
|
|
|
|
|
warn('error: '.$foo->error.":".$foo->errorString); |
235
|
|
|
|
|
|
|
} |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
=cut |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
sub error{ |
240
|
0
|
|
|
0
|
1
|
|
return $_[0]->{error}; |
241
|
|
|
|
|
|
|
} |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
=head2 errorString |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
Returns the error string if there is one. If there is not, |
246
|
|
|
|
|
|
|
it will return ''. |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
if($tb->error){ |
249
|
|
|
|
|
|
|
warn('error: '.$foo->error.":".$foo->errorString); |
250
|
|
|
|
|
|
|
} |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
=cut |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
sub errorString{ |
255
|
0
|
|
|
0
|
1
|
|
return $_[0]->{errorString}; |
256
|
|
|
|
|
|
|
} |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
=head2 errorblank |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
This is a internal function. |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
=cut |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
sub errorblank{ |
265
|
0
|
|
|
0
|
1
|
|
$_[0]->{error}=undef; |
266
|
0
|
|
|
|
|
|
$_[1]->{errorString}=''; |
267
|
|
|
|
|
|
|
} |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
=head1 ERROR CODES |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
As all error codes are positive, $tb->error can be checked to see if it |
272
|
|
|
|
|
|
|
is true and if it is, then there is an error. |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
=head2 1 |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
No seperator specified. |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
=head2 2 |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
Item to add not defined. |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
=head1 AUTHOR |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
Zane C. Bowers-Hadley, C<< >> |
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
=head1 BUGS |
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
289
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you'll |
290
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
291
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
=head1 SUPPORT |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
perldoc Tree::Builder |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
You can also look for information at: |
303
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
=over 4 |
305
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
L |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
311
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
L |
313
|
|
|
|
|
|
|
|
314
|
|
|
|
|
|
|
=item * CPAN Ratings |
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
L |
317
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
=item * Search CPAN |
319
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
L |
321
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
=back |
323
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
|
325
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
326
|
|
|
|
|
|
|
|
327
|
|
|
|
|
|
|
Emanuele, #69928, for pointing out the crappy docs for 0.0.0 |
328
|
|
|
|
|
|
|
|
329
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
330
|
|
|
|
|
|
|
|
331
|
|
|
|
|
|
|
Copyright 2011 Zane C. Bowers-Hadley, all rights reserved. |
332
|
|
|
|
|
|
|
|
333
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
334
|
|
|
|
|
|
|
under the same terms as Perl itself. |
335
|
|
|
|
|
|
|
|
336
|
|
|
|
|
|
|
|
337
|
|
|
|
|
|
|
=cut |
338
|
|
|
|
|
|
|
|
339
|
|
|
|
|
|
|
1; # End of Tree::Builder |