| 1 | package Tree::Builder; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 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 | 2 | 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 | 0 | ▶ | sub new { | ||||||||
|
Subroutine
new: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 68 | 0 | my %args; | |||||||||
| 69 | 0 | ▶ | if(defined($_[1])){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 0 | %args= %{$_[1]}; | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | 0 | ▶ | if (defined($args{seperator})) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | 0 | my $self={error=>undef, errorString=>undef, tree=>{}}; | |||||||||
| 77 | 0 | bless $self; | |||||||||
| 78 | |||||||||||
| 79 | 0 | ▶ | if (!defined($args{seperator})) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 0 | ▶ | sub add{ | ||||||||
|
Subroutine
add: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 105 | 0 | my $self=$_[0]; | |||||||||
| 106 | 0 | my $item=$_[1]; | |||||||||
| 107 | |||||||||||
| 108 | 0 | $self->errorblank; | |||||||||
| 109 | |||||||||||
| 110 | 0 | ▶ | if (!defined($item)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | ▶ | if (!defined($self->{tree}{$itemA[0]})) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 121 | 0 | $self->{tree}{$itemA[0]}={}; | |||||||||
| 122 | } | ||||||||||
| 123 | |||||||||||
| 124 | #if item does not exist, return | ||||||||||
| 125 | 0 | ▶ | if (!defined($itemA[1])) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 126 | 0 | return 1; | |||||||||
| 127 | } | ||||||||||
| 128 | |||||||||||
| 129 | 0 | my %newhash=%{$self->{tree}{$itemA[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 | 0 | ▶ | sub addSub{ | ||||||||
|
Subroutine
addSub: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 145 | 0 | my $self=$_[0]; | |||||||||
| 146 | 0 | my %hash=%{$_[1]}; | |||||||||
| 147 | 0 | my @itemA=@{$_[2]}; | |||||||||
| 148 | 0 | my $int=$_[3]; | |||||||||
| 149 | |||||||||||
| 150 | #return the hash if none others are defined | ||||||||||
| 151 | 0 | ▶ | if (!defined($itemA[$int])) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 152 | 0 | return %hash; | |||||||||
| 153 | } | ||||||||||
| 154 | |||||||||||
| 155 | #add a new hash if it does not already exist | ||||||||||
| 156 | 0 | ▶ | if (!defined($hash{$itemA[$int]})) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 157 | 0 | $hash{$itemA[$int]}={}; | |||||||||
| 158 | } | ||||||||||
| 159 | |||||||||||
| 160 | 0 | my %newhash=%{$hash{$itemA[$int]}}; | |||||||||
| 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 | 0 | ▶ | sub getSeperator{ | ||||||||
|
Subroutine
getSeperator: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 181 | 0 | 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 | 0 | ▶ | sub getTree{ | ||||||||
|
Subroutine
getTree: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 193 | 0 | return %{$_[0]->{tree}}; | |||||||||
| 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 | 0 | ▶ | sub setSeperator{ | ||||||||
|
Subroutine
setSeperator: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 208 | 0 | my $self=$_[0]; | |||||||||
| 209 | 0 | my $seperator=$_[1]; | |||||||||
| 210 | |||||||||||
| 211 | 0 | $self->errorblank; | |||||||||
| 212 | |||||||||||
| 213 | 0 | ▶ | if (!defined($seperator)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 0 | ▶ | sub error{ | ||||||||
|
Subroutine
error: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 240 | 0 | 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 | 0 | ▶ | sub errorString{ | ||||||||
|
Subroutine
errorString: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 255 | 0 | return $_[0]->{errorString}; | |||||||||
| 256 | } | ||||||||||
| 257 | |||||||||||
| 258 | =head2 errorblank | ||||||||||
| 259 | |||||||||||
| 260 | This is a internal function. | ||||||||||
| 261 | |||||||||||
| 262 | =cut | ||||||||||
| 263 | |||||||||||
| 264 | 0 | ▶ | sub errorblank{ | ||||||||
|
Subroutine
errorblank: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 265 | 0 | $_[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<< <vvelox at vvelox.net> >> | ||||||||||
| 285 | |||||||||||
| 286 | =head1 BUGS | ||||||||||
| 287 | |||||||||||
| 288 | Please report any bugs or feature requests to C<bug-tree-builder at rt.cpan.org>, or through | ||||||||||
| 289 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Tree-Builder>. 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<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Tree-Builder> | ||||||||||
| 309 | |||||||||||
| 310 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 311 | |||||||||||
| 312 | L<http://annocpan.org/dist/Tree-Builder> | ||||||||||
| 313 | |||||||||||
| 314 | =item * CPAN Ratings | ||||||||||
| 315 | |||||||||||
| 316 | L<http://cpanratings.perl.org/d/Tree-Builder> | ||||||||||
| 317 | |||||||||||
| 318 | =item * Search CPAN | ||||||||||
| 319 | |||||||||||
| 320 | L<http://search.cpan.org/dist/Tree-Builder/> | ||||||||||
| 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 | 2 | 1; # End of Tree::Builder | |||||||||