| 1 | package PPI::Find; | ||||||||||
| 2 | |||||||||||
| 3 | =pod | ||||||||||
| 4 | |||||||||||
| 5 | =head1 NAME | ||||||||||
| 6 | |||||||||||
| 7 | PPI::Find - Object version of the Element->find method | ||||||||||
| 8 | |||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||
| 10 | |||||||||||
| 11 | # Create the Find object | ||||||||||
| 12 | my $Find = PPI::Find->new( \&wanted ); | ||||||||||
| 13 | |||||||||||
| 14 | # Return all matching Elements as a list | ||||||||||
| 15 | my @found = $Find->in( $Document ); | ||||||||||
| 16 | |||||||||||
| 17 | # Can we find any matching Elements | ||||||||||
| 18 | if ( $Find->any_matches($Document) ) { | ||||||||||
| 19 | print "Found at least one matching Element"; | ||||||||||
| 20 | } | ||||||||||
| 21 | |||||||||||
| 22 | # Use the object as an iterator | ||||||||||
| 23 | $Find->start($Document) or die "Failed to execute search"; | ||||||||||
| 24 | while ( my $token = $Find->match ) { | ||||||||||
| 25 | ... | ||||||||||
| 26 | } | ||||||||||
| 27 | |||||||||||
| 28 | =head1 DESCRIPTION | ||||||||||
| 29 | |||||||||||
| 30 | PPI::Find is the primary PDOM searching class in the core PPI package. | ||||||||||
| 31 | |||||||||||
| 32 | =head2 History | ||||||||||
| 33 | |||||||||||
| 34 | It became quite obvious during the development of PPI that many of the | ||||||||||
| 35 | modules that would be built on top of it were going to need large numbers | ||||||||||
| 36 | of saved, storable or easily creatable search objects that could be | ||||||||||
| 37 | reused a number of times. | ||||||||||
| 38 | |||||||||||
| 39 | Although the internal ->find method provides a basic ability to search, | ||||||||||
| 40 | it is by no means thorough. PPI::Find attempts to resolve this problem. | ||||||||||
| 41 | |||||||||||
| 42 | =head2 Structure and Style | ||||||||||
| 43 | |||||||||||
| 44 | PPI::Find provides a similar API to the popular L<File::Find::Rule> | ||||||||||
| 45 | module for file searching, but without the ability to assemble queries. | ||||||||||
| 46 | |||||||||||
| 47 | The implementation of a separate PPI::Find::Rule sub-class that does | ||||||||||
| 48 | provide this ability is left as an exercise for the reader. | ||||||||||
| 49 | |||||||||||
| 50 | =head2 The &wanted function | ||||||||||
| 51 | |||||||||||
| 52 | At the core of each PPI::Find object is a "wanted" function that is | ||||||||||
| 53 | passed a number of arguments and returns a value which controls the | ||||||||||
| 54 | flow of the search. | ||||||||||
| 55 | |||||||||||
| 56 | As the search executes, each Element will be passed to the wanted function | ||||||||||
| 57 | in depth-first order. | ||||||||||
| 58 | |||||||||||
| 59 | It will be provided with two arguments. The current Element to test as $_[0], | ||||||||||
| 60 | and the top-level Element of the search as $_[1]. | ||||||||||
| 61 | |||||||||||
| 62 | The &wanted function is expected to return 1 (positive) if the Element | ||||||||||
| 63 | matches the condition, 0 (false) if it does not, and undef (undefined) if | ||||||||||
| 64 | the condition does not match, and the Find search should not descend to | ||||||||||
| 65 | any of the current Element's children. | ||||||||||
| 66 | |||||||||||
| 67 | Errors should be reported from the &wanted function via die, which will be | ||||||||||
| 68 | caught by the Find object and returned as an error. | ||||||||||
| 69 | |||||||||||
| 70 | =head1 METHODS | ||||||||||
| 71 | |||||||||||
| 72 | =cut | ||||||||||
| 73 | |||||||||||
| 74 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 75 | 1 | ▶ | use Params::Util qw{_INSTANCE}; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 76 | |||||||||||
| 77 | 1 | ▶ | use vars qw{$VERSION}; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 78 | BEGIN { | ||||||||||
| 79 | 1 | ▶ | $VERSION = '1.236'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | |||||||||||
| 83 | |||||||||||
| 84 | |||||||||||
| 85 | |||||||||||
| 86 | ##################################################################### | ||||||||||
| 87 | # Constructor | ||||||||||
| 88 | |||||||||||
| 89 | =pod | ||||||||||
| 90 | |||||||||||
| 91 | =head2 new &wanted | ||||||||||
| 92 | |||||||||||
| 93 | The C<new> constructor takes a single argument of the &wanted function, | ||||||||||
| 94 | as described above and creates a new search. | ||||||||||
| 95 | |||||||||||
| 96 | Returns a new PPI::Find object, or C<undef> if not passed a CODE reference. | ||||||||||
| 97 | |||||||||||
| 98 | =cut | ||||||||||
| 99 | |||||||||||
| 100 | sub new { | ||||||||||
| 101 | 0 | ▶ | my $class = ref $_[0] ? ref shift : shift; | ||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
documented
Branch: 1 branch
| |||||||||||
| 102 | 0 | ▶ | my $wanted = ref $_[0] eq 'CODE' ? shift : return undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | |||||||||||
| 104 | # Create the object | ||||||||||
| 105 | 0 | my $self = bless { | |||||||||
| 106 | wanted => $wanted, | ||||||||||
| 107 | }, $class; | ||||||||||
| 108 | |||||||||||
| 109 | 0 | $self; | |||||||||
| 110 | } | ||||||||||
| 111 | |||||||||||
| 112 | =pod | ||||||||||
| 113 | |||||||||||
| 114 | =head2 clone | ||||||||||
| 115 | |||||||||||
| 116 | The C<clone> method creates another instance of the same Find object. | ||||||||||
| 117 | |||||||||||
| 118 | The cloning is done safely, so if your existing Find object is in the | ||||||||||
| 119 | middle of an iteration, the cloned Find object will not also be in the | ||||||||||
| 120 | iteration and can be safely used independently. | ||||||||||
| 121 | |||||||||||
| 122 | Returns a duplicate PPI::Find object. | ||||||||||
| 123 | |||||||||||
| 124 | =cut | ||||||||||
| 125 | |||||||||||
| 126 | sub clone { | ||||||||||
| 127 | 0 | ▶ | my $self = ref $_[0] ? shift | ||||||||
|
Subroutine
clone: not called CC=2 SCAR=17.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 128 | : die "->clone can only be called as an object method"; | ||||||||||
| 129 | 0 | my $class = ref $self; | |||||||||
| 130 | |||||||||||
| 131 | # Create the object | ||||||||||
| 132 | my $clone = bless { | ||||||||||
| 133 | wanted => $self->{wanted}, | ||||||||||
| 134 | 0 | }, $class; | |||||||||
| 135 | |||||||||||
| 136 | 0 | $clone; | |||||||||
| 137 | } | ||||||||||
| 138 | |||||||||||
| 139 | |||||||||||
| 140 | |||||||||||
| 141 | |||||||||||
| 142 | |||||||||||
| 143 | #################################################################### | ||||||||||
| 144 | # Search Execution Methods | ||||||||||
| 145 | |||||||||||
| 146 | =pod | ||||||||||
| 147 | |||||||||||
| 148 | =head2 in $Document [, array_ref => 1 ] | ||||||||||
| 149 | |||||||||||
| 150 | The C<in> method starts and completes a full run of the search. | ||||||||||
| 151 | |||||||||||
| 152 | It takes as argument a single L<PPI::Element> object which will | ||||||||||
| 153 | serve as the top of the search process. | ||||||||||
| 154 | |||||||||||
| 155 | Returns a list of PPI::Element objects that match the condition | ||||||||||
| 156 | described by the &wanted function, or the null list on error. | ||||||||||
| 157 | |||||||||||
| 158 | You should check the ->errstr method for any errors if you are | ||||||||||
| 159 | returned the null list, which may also mean simply that no Elements | ||||||||||
| 160 | were found that matched the condition. | ||||||||||
| 161 | |||||||||||
| 162 | Because of this need to explicitly check for errors, an alternative | ||||||||||
| 163 | return value mechanism is provide. If you pass the C<< array_ref => 1 >> | ||||||||||
| 164 | parameter to the method, it will return the list of matched Elements | ||||||||||
| 165 | as a reference to an ARRAY. The method will return false if no elements | ||||||||||
| 166 | were matched, or C<undef> on error. | ||||||||||
| 167 | |||||||||||
| 168 | The ->errstr method can still be used to get the error message as normal. | ||||||||||
| 169 | |||||||||||
| 170 | =cut | ||||||||||
| 171 | |||||||||||
| 172 | sub in { | ||||||||||
| 173 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
in: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||
| 174 | 0 | my $Element = shift; | |||||||||
| 175 | 0 | my %params = @_; | |||||||||
| 176 | 0 | delete $self->{errstr}; | |||||||||
| 177 | |||||||||||
| 178 | # Are we already acting as an iterator | ||||||||||
| 179 | 0 | ▶ | if ( $self->{in} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 180 | 0 | return $self->_error('->in called while another search is in progress', %params); | |||||||||
| 181 | } | ||||||||||
| 182 | |||||||||||
| 183 | # Get the root element for the search | ||||||||||
| 184 | 0 | ▶ | unless ( _INSTANCE($Element, 'PPI::Element') ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 185 | 0 | return $self->_error('->in was not passed a PPI::Element object', %params); | |||||||||
| 186 | } | ||||||||||
| 187 | |||||||||||
| 188 | # Prepare the search | ||||||||||
| 189 | 0 | $self->{in} = $Element; | |||||||||
| 190 | 0 | $self->{matches} = []; | |||||||||
| 191 | |||||||||||
| 192 | # Execute the search | ||||||||||
| 193 | 0 | ▶ | if ( !eval { $self->_execute; 1 } ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 194 | 0 | my $errstr = $@; | |||||||||
| 195 | 0 | $errstr =~ s/\s+at\s+line\s+.+$//; | |||||||||
| 196 | 0 | return $self->_error("Error while searching: $errstr", %params); | |||||||||
| 197 | } | ||||||||||
| 198 | |||||||||||
| 199 | # Clean up and return | ||||||||||
| 200 | 0 | delete $self->{in}; | |||||||||
| 201 | 0 | ▶ | if ( $params{array_ref} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 202 | 0 | ▶ | if ( @{$self->{matches}} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 203 | 0 | return delete $self->{matches}; | |||||||||
| 204 | } | ||||||||||
| 205 | 0 | delete $self->{matches}; | |||||||||
| 206 | 0 | return ''; | |||||||||
| 207 | } | ||||||||||
| 208 | |||||||||||
| 209 | # Return as a list | ||||||||||
| 210 | 0 | my $matches = delete $self->{matches}; | |||||||||
| 211 | 0 | @$matches; | |||||||||
| 212 | } | ||||||||||
| 213 | |||||||||||
| 214 | =pod | ||||||||||
| 215 | |||||||||||
| 216 | =head2 start $Element | ||||||||||
| 217 | |||||||||||
| 218 | The C<start> method lets the Find object act as an iterator. The method | ||||||||||
| 219 | is passed the parent PPI::Element object as for the C<in> method, but does | ||||||||||
| 220 | not accept any parameters. | ||||||||||
| 221 | |||||||||||
| 222 | To simplify error handling, the entire search is done at once, with the | ||||||||||
| 223 | results cached and provided as-requested. | ||||||||||
| 224 | |||||||||||
| 225 | Returns true if the search completes, and false on error. | ||||||||||
| 226 | |||||||||||
| 227 | =cut | ||||||||||
| 228 | |||||||||||
| 229 | sub start { | ||||||||||
| 230 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
start: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 231 | 0 | my $Element = shift; | |||||||||
| 232 | 0 | delete $self->{errstr}; | |||||||||
| 233 | |||||||||||
| 234 | # Are we already acting as an iterator | ||||||||||
| 235 | 0 | ▶ | if ( $self->{in} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 236 | 0 | return $self->_error('->in called while another search is in progress'); | |||||||||
| 237 | } | ||||||||||
| 238 | |||||||||||
| 239 | # Get the root element for the search | ||||||||||
| 240 | 0 | ▶ | unless ( _INSTANCE($Element, 'PPI::Element') ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 241 | 0 | return $self->_error('->in was not passed a PPI::Element object'); | |||||||||
| 242 | } | ||||||||||
| 243 | |||||||||||
| 244 | # Prepare the search | ||||||||||
| 245 | 0 | $self->{in} = $Element; | |||||||||
| 246 | 0 | $self->{matches} = []; | |||||||||
| 247 | |||||||||||
| 248 | # Execute the search | ||||||||||
| 249 | 0 | ▶ | if ( !eval { $self->_execute; 1 } ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 250 | 0 | my $errstr = $@; | |||||||||
| 251 | 0 | $errstr =~ s/\s+at\s+line\s+.+$//; | |||||||||
| 252 | 0 | $self->_error("Error while searching: $errstr"); | |||||||||
| 253 | 0 | return undef; | |||||||||
| 254 | } | ||||||||||
| 255 | |||||||||||
| 256 | 0 | 1; | |||||||||
| 257 | } | ||||||||||
| 258 | |||||||||||
| 259 | =pod | ||||||||||
| 260 | |||||||||||
| 261 | =head2 match | ||||||||||
| 262 | |||||||||||
| 263 | The C<match> method returns the next matching Element in the iteration. | ||||||||||
| 264 | |||||||||||
| 265 | Returns a PPI::Element object, or C<undef> if there are no remaining | ||||||||||
| 266 | Elements to be returned. | ||||||||||
| 267 | |||||||||||
| 268 | =cut | ||||||||||
| 269 | |||||||||||
| 270 | sub match { | ||||||||||
| 271 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
match: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 272 | 0 | ▶ | return undef unless $self->{matches}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 273 | |||||||||||
| 274 | # Fetch and return the next match | ||||||||||
| 275 | 0 | my $match = shift @{$self->{matches}}; | |||||||||
| 276 | 0 | ▶ | return $match if $match; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 277 | |||||||||||
| 278 | 0 | $self->finish; | |||||||||
| 279 | 0 | undef; | |||||||||
| 280 | } | ||||||||||
| 281 | |||||||||||
| 282 | =pod | ||||||||||
| 283 | |||||||||||
| 284 | =head2 finish | ||||||||||
| 285 | |||||||||||
| 286 | The C<finish> method provides a mechanism to end iteration if you wish to | ||||||||||
| 287 | stop the iteration prematurely. It resets the Find object and allows it to | ||||||||||
| 288 | be safely reused. | ||||||||||
| 289 | |||||||||||
| 290 | A Find object will be automatically finished when C<match> returns false. | ||||||||||
| 291 | This means you should only need to call C<finish> when you stop | ||||||||||
| 292 | iterating early. | ||||||||||
| 293 | |||||||||||
| 294 | You may safely call this method even when not iterating and it will return | ||||||||||
| 295 | without failure. | ||||||||||
| 296 | |||||||||||
| 297 | Always returns true | ||||||||||
| 298 | |||||||||||
| 299 | =cut | ||||||||||
| 300 | |||||||||||
| 301 | sub finish { | ||||||||||
| 302 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
finish: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 303 | 0 | delete $self->{in}; | |||||||||
| 304 | 0 | delete $self->{matches}; | |||||||||
| 305 | 0 | delete $self->{errstr}; | |||||||||
| 306 | 0 | 1; | |||||||||
| 307 | } | ||||||||||
| 308 | |||||||||||
| 309 | |||||||||||
| 310 | |||||||||||
| 311 | |||||||||||
| 312 | |||||||||||
| 313 | ##################################################################### | ||||||||||
| 314 | # Support Methods and Error Handling | ||||||||||
| 315 | |||||||||||
| 316 | sub _execute { | ||||||||||
| 317 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_execute: not called CC=8 SCAR=42.8
| |||||||||||
| 318 | 0 | my $wanted = $self->{wanted}; | |||||||||
| 319 | 0 | my @queue = ( $self->{in} ); | |||||||||
| 320 | |||||||||||
| 321 | # Pull entries off the queue and hand them off to the wanted function | ||||||||||
| 322 | 0 | ▶ | while ( my $Element = shift @queue ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 323 | 0 | my $rv = &$wanted( $Element, $self->{in} ); | |||||||||
| 324 | |||||||||||
| 325 | # Add to the matches if returns true | ||||||||||
| 326 | 0 | ▶ | push @{$self->{matches}}, $Element if $rv; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 327 | |||||||||||
| 328 | # Continue and don't descend if it returned undef | ||||||||||
| 329 | # or if it doesn't have children | ||||||||||
| 330 | 0 | ▶ | next unless defined $rv; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 331 | 0 | ▶ | next unless $Element->isa('PPI::Node'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 332 | |||||||||||
| 333 | # Add the children to the head of the queue | ||||||||||
| 334 | 0 | ▶ | if ( $Element->isa('PPI::Structure') ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 335 | 0 | ▶ | unshift @queue, $Element->finish if $Element->finish; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 336 | 0 | unshift @queue, $Element->children; | |||||||||
| 337 | 0 | ▶ | unshift @queue, $Element->start if $Element->start; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 338 | } else { | ||||||||||
| 339 | 0 | unshift @queue, $Element->children; | |||||||||
| 340 | } | ||||||||||
| 341 | } | ||||||||||
| 342 | |||||||||||
| 343 | 0 | 1; | |||||||||
| 344 | } | ||||||||||
| 345 | |||||||||||
| 346 | =pod | ||||||||||
| 347 | |||||||||||
| 348 | =head2 errstr | ||||||||||
| 349 | |||||||||||
| 350 | The C<errstr> method returns the error messages when a given PPI::Find | ||||||||||
| 351 | object fails any action. | ||||||||||
| 352 | |||||||||||
| 353 | Returns a string, or C<undef> if there is no error. | ||||||||||
| 354 | |||||||||||
| 355 | =cut | ||||||||||
| 356 | |||||||||||
| 357 | sub errstr { | ||||||||||
| 358 | 0 | ▶ | shift->{errstr}; | ||||||||
|
Subroutine
errstr: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 359 | } | ||||||||||
| 360 | |||||||||||
| 361 | sub _error { | ||||||||||
| 362 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_error: not called CC=2 SCAR=17.9
| |||||||||||
| 363 | 0 | $self->{errstr} = shift; | |||||||||
| 364 | 0 | my %params = @_; | |||||||||
| 365 | 0 | ▶ | $params{array_ref} ? undef : (); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 366 | } | ||||||||||
| 367 | |||||||||||
| 368 | 1 | 1; | |||||||||
| 369 | |||||||||||
| 370 | =pod | ||||||||||
| 371 | |||||||||||
| 372 | =head1 TO DO | ||||||||||
| 373 | |||||||||||
| 374 | - Implement the L<PPI::Find::Rule> class | ||||||||||
| 375 | |||||||||||
| 376 | =head1 SUPPORT | ||||||||||
| 377 | |||||||||||
| 378 | See the L<support section|PPI/SUPPORT> in the main module. | ||||||||||
| 379 | |||||||||||
| 380 | =head1 AUTHOR | ||||||||||
| 381 | |||||||||||
| 382 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||||||||||
| 383 | |||||||||||
| 384 | =head1 COPYRIGHT | ||||||||||
| 385 | |||||||||||
| 386 | Copyright 2001 - 2011 Adam Kennedy. | ||||||||||
| 387 | |||||||||||
| 388 | This program is free software; you can redistribute | ||||||||||
| 389 | it and/or modify it under the same terms as Perl itself. | ||||||||||
| 390 | |||||||||||
| 391 | The full text of the license can be found in the | ||||||||||
| 392 | LICENSE file included with this module. | ||||||||||
| 393 | |||||||||||
| 394 | =cut | ||||||||||