| 1 |
NAME |
| 2 |
Path::Iterator::Rule - Iterative, recursive file finder |
| 3 |
|
| 4 |
VERSION |
| 5 |
version 1.015 |
| 6 |
|
| 7 |
SYNOPSIS |
| 8 |
use Path::Iterator::Rule; |
| 9 |
|
| 10 |
my $rule = Path::Iterator::Rule->new; # match anything |
| 11 |
$rule->file->size(">10k"); # add/chain rules |
| 12 |
|
| 13 |
# iterator interface |
| 14 |
my $next = $rule->iter( @dirs ); |
| 15 |
while ( defined( my $file = $next->() ) ) { |
| 16 |
... |
| 17 |
} |
| 18 |
|
| 19 |
# list interface |
| 20 |
for my $file ( $rule->all( @dirs ) ) { |
| 21 |
... |
| 22 |
} |
| 23 |
|
| 24 |
DESCRIPTION |
| 25 |
This module iterates over files and directories to identify ones |
| 26 |
matching a user-defined set of rules. The API is based heavily on |
| 27 |
File::Find::Rule, but with more explicit distinction between matching |
| 28 |
rules and options that influence how directories are searched. A |
| 29 |
"Path::Iterator::Rule" object is a collection of rules (match criteria) |
| 30 |
with methods to add additional criteria. Options that control directory |
| 31 |
traversal are given as arguments to the method that generates an |
| 32 |
iterator. |
| 33 |
|
| 34 |
Here is a summary of features for comparison to other file finding |
| 35 |
modules: |
| 36 |
|
| 37 |
* provides many "helper" methods for specifying rules |
| 38 |
|
| 39 |
* offers (lazy) iterator and flattened list interfaces |
| 40 |
|
| 41 |
* custom rules implemented with callbacks |
| 42 |
|
| 43 |
* breadth-first (default) or pre- or post-order depth-first searching |
| 44 |
|
| 45 |
* follows symlinks (by default, but can be disabled) |
| 46 |
|
| 47 |
* directories visited only once (no infinite loop; can be disabled) |
| 48 |
|
| 49 |
* doesn't chdir during operation |
| 50 |
|
| 51 |
* provides an API for extensions |
| 52 |
|
| 53 |
As a convenience, the PIR module is an empty subclass of this one that |
| 54 |
is less arduous to type for one-liners. |
| 55 |
|
| 56 |
Note: paths are constructed with unix-style forward-slashes for |
| 57 |
efficiency rather than using File::Spec. If proper path separators are |
| 58 |
needed, call canonpath on the search results. |
| 59 |
|
| 60 |
USAGE |
| 61 |
Constructors |
| 62 |
"new" |
| 63 |
my $rule = Path::Iterator::Rule->new; |
| 64 |
|
| 65 |
Creates a new rule object that matches any file or directory. It takes |
| 66 |
no arguments. For convenience, it may also be called on an object, in |
| 67 |
which case it still returns a new object that matches any file or |
| 68 |
directory. |
| 69 |
|
| 70 |
"clone" |
| 71 |
my $common = Path::Iterator::Rule->new->file->not_empty; |
| 72 |
my $big_files = $common->clone->size(">1M"); |
| 73 |
my $small_files = $common->clone->size("<10K"); |
| 74 |
|
| 75 |
Creates a copy of a rule object. Useful for customizing different rule |
| 76 |
objects against a common base. |
| 77 |
|
| 78 |
Matching and iteration |
| 79 |
"iter" |
| 80 |
my $next = $rule->iter( @dirs, \%options); |
| 81 |
while ( defined( my $file = $next->() ) ) { |
| 82 |
... |
| 83 |
} |
| 84 |
|
| 85 |
Creates a subroutine reference iterator that returns a single result |
| 86 |
when dereferenced. This iterator is "lazy" -- results are not |
| 87 |
pre-computed. |
| 88 |
|
| 89 |
It takes as arguments a list of directories to search and an optional |
| 90 |
hash reference of control options. If no search directories are |
| 91 |
provided, the current directory is used ("."). Valid options include: |
| 92 |
|
| 93 |
* "depthfirst" -- Controls order of results. Valid values are "1" |
| 94 |
(post-order, depth-first search), "0" (breadth-first search) or "-1" |
| 95 |
(pre-order, depth-first search). Default is 0. |
| 96 |
|
| 97 |
* "error_handler" -- Catches errors during execution of rule tests. |
| 98 |
Default handler dies with the filename and error. If set to undef, |
| 99 |
error handling is disabled. |
| 100 |
|
| 101 |
* "follow_symlinks" -- Follow directory symlinks when true. Default is |
| 102 |
1. |
| 103 |
|
| 104 |
* "report_symlinks" -- Includes symlinks in results when true. Default |
| 105 |
is equal to "follow_symlinks". |
| 106 |
|
| 107 |
* "loop_safe" -- Prevents visiting the same directory more than once |
| 108 |
when true. Default is 1. |
| 109 |
|
| 110 |
* "relative" -- Return matching items relative to the search |
| 111 |
directory. Default is 0. |
| 112 |
|
| 113 |
* "sorted" -- Whether entries in a directory are sorted before |
| 114 |
processing. Default is 1. |
| 115 |
|
| 116 |
* "visitor" -- An optional coderef that will be called on items |
| 117 |
matching all rules. |
| 118 |
|
| 119 |
Filesystem loops might exist from either hard or soft links. The |
| 120 |
"loop_safe" option prevents infinite loops, but adds some overhead by |
| 121 |
making "stat" calls. Because directories are visited only once when |
| 122 |
"loop_safe" is true, matches could come from a symlinked directory |
| 123 |
before the real directory depending on the search order. |
| 124 |
|
| 125 |
To get only the real files, turn off "follow_symlinks". You can have |
| 126 |
symlinks included in results, but not descend into symlink directories |
| 127 |
if you turn off "follow_symlinks", but turn on "report_symlinks". |
| 128 |
|
| 129 |
Turning "loop_safe" off and leaving "follow_symlinks" on avoids "stat" |
| 130 |
calls and will be fastest, but with the risk of an infinite loop and |
| 131 |
repeated files. The default is slow, but safe. |
| 132 |
|
| 133 |
The "error_handler" parameter must be a subroutine reference. It will be |
| 134 |
called when a rule test throws an exception. The first argument will be |
| 135 |
the file name being inspected and the second argument will be the |
| 136 |
exception. |
| 137 |
|
| 138 |
The optional "visitor" parameter must be a subroutine reference. If set, |
| 139 |
it will be called for any result that matches. It is called the same way |
| 140 |
a custom rule would be (see "EXTENDING") but its return value is |
| 141 |
ignored. It is called when an item is first inspected -- "postorder" is |
| 142 |
not respected. |
| 143 |
|
| 144 |
The paths inspected and returned will be relative to the search |
| 145 |
directories provided. If these are absolute, then the paths returned |
| 146 |
will have absolute paths. If these are relative, then the paths returned |
| 147 |
will have relative paths. |
| 148 |
|
| 149 |
If the search directories are absolute and the "relative" option is |
| 150 |
true, files returned will be relative to the search directory. Note that |
| 151 |
if the search directories are not mutually exclusive (whether containing |
| 152 |
subdirectories like @INC or symbolic links), files found could be |
| 153 |
returned relative to different initial search directories based on |
| 154 |
"depthfirst", "follow_symlinks" or "loop_safe". |
| 155 |
|
| 156 |
When the iterator is exhausted, it will return undef. |
| 157 |
|
| 158 |
"iter_fast" |
| 159 |
This works just like "iter", except that it optimizes for speed over |
| 160 |
safety. Don't do this unless you're sure you need it and accept the |
| 161 |
consequences. See "PERFORMANCE" for details. |
| 162 |
|
| 163 |
"all" |
| 164 |
my @matches = $rule->all( @dir, \%options ); |
| 165 |
|
| 166 |
Returns a list of paths that match the rule. It takes the same arguments |
| 167 |
and has the same behaviors as the "iter" method. The "all" method uses |
| 168 |
"iter" internally to fetch all results. |
| 169 |
|
| 170 |
In scalar context, it will return the count of matched paths. |
| 171 |
|
| 172 |
In void context, it is optimized to iterate over everything, but not |
| 173 |
store results. This is most useful with the "visitor" option: |
| 174 |
|
| 175 |
$rule->all( $path, { visitor => \&callback } ); |
| 176 |
|
| 177 |
"all_fast" |
| 178 |
This works just like "all", except that it optimizes for speed over |
| 179 |
safety. Don't do this unless you're sure you need it and accept the |
| 180 |
consequences. See "PERFORMANCE" for details. |
| 181 |
|
| 182 |
"test" |
| 183 |
if ( $rule->test( $path, $basename, $stash ) ) { ... } |
| 184 |
|
| 185 |
Test a file path against a rule. Used internally, but provided should |
| 186 |
someone want to create their own, custom iteration algorithm. |
| 187 |
|
| 188 |
Logic operations |
| 189 |
"Path::Iterator::Rule" provides three logic operations for adding rules |
| 190 |
to the object. Rules may be either a subroutine reference with specific |
| 191 |
semantics (described below in "EXTENDING") or another |
| 192 |
"Path::Iterator::Rule" object. |
| 193 |
|
| 194 |
"and" |
| 195 |
$rule->and( sub { -r -w -x $_ } ); # stacked filetest example |
| 196 |
$rule->and( @more_rules ); |
| 197 |
|
| 198 |
Adds one or more constraints to the current rule. E.g. "old rule AND |
| 199 |
new1 AND new2 AND ...". Returns the object to allow method chaining. |
| 200 |
|
| 201 |
"or" |
| 202 |
$rule->or( |
| 203 |
$rule->new->name("foo*"), |
| 204 |
$rule->new->name("bar*"), |
| 205 |
sub { -r -w -x $_ }, |
| 206 |
); |
| 207 |
|
| 208 |
Takes one or more alternatives and adds them as a constraint to the |
| 209 |
current rule. E.g. "old rule AND ( new1 OR new2 OR ... )". Returns the |
| 210 |
object to allow method chaining. |
| 211 |
|
| 212 |
"not" |
| 213 |
$rule->not( sub { -r -w -x $_ } ); |
| 214 |
|
| 215 |
Takes one or more alternatives and adds them as a negative constraint to |
| 216 |
the current rule. E.g. "old rule AND NOT ( new1 AND new2 AND ...)". |
| 217 |
Returns the object to allow method chaining. |
| 218 |
|
| 219 |
"skip" |
| 220 |
$rule->skip( |
| 221 |
$rule->new->dir->not_writeable, |
| 222 |
$rule->new->dir->name("foo"), |
| 223 |
); |
| 224 |
|
| 225 |
Takes one or more alternatives and will prune a directory if any of the |
| 226 |
criteria match or if any of the rules already indicate the directory |
| 227 |
should be pruned. Pruning means the directory will not be returned by |
| 228 |
the iterator and will not be searched. |
| 229 |
|
| 230 |
For files, it is equivalent to "$rule->not($rule->or(@rules))". Returns |
| 231 |
the object to allow method chaining. |
| 232 |
|
| 233 |
This method should be called as early as possible in the rule chain. See |
| 234 |
"skip_dirs" below for further explanation and an example. |
| 235 |
|
| 236 |
RULE METHODS |
| 237 |
Rule methods are helpers that add constraints. Internally, they generate |
| 238 |
a closure to accomplish the desired logic and add it to the rule object |
| 239 |
with the "and" method. Rule methods return the object to allow for |
| 240 |
method chaining. |
| 241 |
|
| 242 |
File name rules |
| 243 |
"name" |
| 244 |
$rule->name( "foo.txt" ); |
| 245 |
$rule->name( qr/foo/, "bar.*"); |
| 246 |
|
| 247 |
The "name" method takes one or more patterns and creates a rule that is |
| 248 |
true if any of the patterns match the basename of the file or directory |
| 249 |
path. Patterns may be regular expressions or glob expressions (or |
| 250 |
literal names). |
| 251 |
|
| 252 |
"iname" |
| 253 |
$rule->iname( "foo.txt" ); |
| 254 |
$rule->iname( qr/foo/, "bar.*"); |
| 255 |
|
| 256 |
The "iname" method is just like the "name" method, but matches |
| 257 |
case-insensitively. |
| 258 |
|
| 259 |
"skip_dirs" |
| 260 |
$rule->skip_dirs( @patterns ); |
| 261 |
|
| 262 |
The "skip_dirs" method skips directories that match one or more |
| 263 |
patterns. Patterns may be regular expressions or globs (just like |
| 264 |
"name"). Directories that match will not be returned from the iterator |
| 265 |
and will be excluded from further search. This includes the starting |
| 266 |
directories. If that isn't what you want, see "skip_subdirs" instead. |
| 267 |
|
| 268 |
Note: this rule should be specified early so that it has a chance to |
| 269 |
operate before a logical shortcut. E.g. |
| 270 |
|
| 271 |
$rule->skip_dirs(".git")->file; # OK |
| 272 |
$rule->file->skip_dirs(".git"); # Won't work |
| 273 |
|
| 274 |
In the latter case, when a ".git" directory is seen, the "file" rule |
| 275 |
shortcuts the rule before the "skip_dirs" rule has a chance to act. |
| 276 |
|
| 277 |
"skip_subdirs" |
| 278 |
$rule->skip_subdirs( @patterns ); |
| 279 |
|
| 280 |
This works just like "skip_dirs", except that the starting directories |
| 281 |
(depth 0) are not skipped and may be returned from the iterator unless |
| 282 |
excluded by other rules. |
| 283 |
|
| 284 |
File test rules |
| 285 |
Most of the "-X" style filetest are available as boolean rules. The |
| 286 |
table below maps the filetest to its corresponding method name. |
| 287 |
|
| 288 |
Test | Method Test | Method |
| 289 |
------|------------- ------|---------------- |
| 290 |
-r | readable -R | r_readable |
| 291 |
-w | writeable -W | r_writeable |
| 292 |
-w | writable -W | r_writable |
| 293 |
-x | executable -X | r_executable |
| 294 |
-o | owned -O | r_owned |
| 295 |
| | |
| 296 |
-e | exists -f | file |
| 297 |
-z | empty -d | directory, dir |
| 298 |
-s | nonempty -l | symlink |
| 299 |
| -p | fifo |
| 300 |
-u | setuid -S | socket |
| 301 |
-g | setgid -b | block |
| 302 |
-k | sticky -c | character |
| 303 |
| -t | tty |
| 304 |
-T | ascii |
| 305 |
-B | binary |
| 306 |
|
| 307 |
For example: |
| 308 |
|
| 309 |
$rule->file->nonempty; # -f -s $file |
| 310 |
|
| 311 |
The -X operators for timestamps take a single argument in a form that |
| 312 |
Number::Compare can interpret. |
| 313 |
|
| 314 |
Test | Method |
| 315 |
------|------------- |
| 316 |
-A | accessed |
| 317 |
-M | modified |
| 318 |
-C | changed |
| 319 |
|
| 320 |
For example: |
| 321 |
|
| 322 |
$rule->modified(">1"); # -M $file > 1 |
| 323 |
|
| 324 |
Stat test rules |
| 325 |
All of the "stat" elements have a method that takes a single argument in |
| 326 |
a form understood by Number::Compare. |
| 327 |
|
| 328 |
stat() | Method |
| 329 |
-------------------- |
| 330 |
0 | dev |
| 331 |
1 | ino |
| 332 |
2 | mode |
| 333 |
3 | nlink |
| 334 |
4 | uid |
| 335 |
5 | gid |
| 336 |
6 | rdev |
| 337 |
7 | size |
| 338 |
8 | atime |
| 339 |
9 | mtime |
| 340 |
10 | ctime |
| 341 |
11 | blksize |
| 342 |
12 | blocks |
| 343 |
|
| 344 |
For example: |
| 345 |
|
| 346 |
$rule->size(">10K") |
| 347 |
|
| 348 |
Depth rules |
| 349 |
$rule->min_depth(3); |
| 350 |
$rule->max_depth(5); |
| 351 |
|
| 352 |
The "min_depth" and "max_depth" rule methods take a single argument and |
| 353 |
limit the paths returned to a minimum or maximum depth (respectively) |
| 354 |
from the starting search directory. A depth of 0 means the starting |
| 355 |
directory itself. A depth of 1 means its children. (This is similar to |
| 356 |
the Unix "find" utility.) |
| 357 |
|
| 358 |
Perl file rules |
| 359 |
# All perl rules |
| 360 |
$rule->perl_file; |
| 361 |
|
| 362 |
# Individual perl file rules |
| 363 |
$rule->perl_module; # .pm files |
| 364 |
$rule->perl_pod; # .pod files |
| 365 |
$rule->perl_test; # .t files |
| 366 |
$rule->perl_installer; # Makefile.PL or Build.PL |
| 367 |
$rule->perl_script; # .pl or 'perl' in the shebang |
| 368 |
|
| 369 |
These rule methods match file names (or a shebang line) that are typical |
| 370 |
of Perl distribution files. |
| 371 |
|
| 372 |
Version control file rules |
| 373 |
# Skip all known VCS files |
| 374 |
$rule->skip_vcs; |
| 375 |
|
| 376 |
# Skip individual VCS files |
| 377 |
$rule->skip_cvs; |
| 378 |
$rule->skip_rcs; |
| 379 |
$rule->skip_svn; |
| 380 |
$rule->skip_git; |
| 381 |
$rule->skip_bzr; |
| 382 |
$rule->skip_hg; |
| 383 |
$rule->skip_darcs; |
| 384 |
|
| 385 |
Skips files and/or prunes directories related to a version control |
| 386 |
system. Just like "skip_dirs", these rules should be specified early to |
| 387 |
get the correct behavior. |
| 388 |
|
| 389 |
File content rules |
| 390 |
"contents_match" |
| 391 |
$rule->contents_match(qr/BEGIN .* END/xs); |
| 392 |
|
| 393 |
The "contents_match" rule takes a list of regular expressions and |
| 394 |
returns files that match one of the expressions. |
| 395 |
|
| 396 |
The expressions are applied to the file's contents as a single string. |
| 397 |
For large files, this is likely to take significant time and memory. |
| 398 |
|
| 399 |
Files are assumed to be encoded in UTF-8, but alternative Perl IO layers |
| 400 |
can be passed as the first argument: |
| 401 |
|
| 402 |
$rule->contents_match(":encoding(iso-8859-1)", qr/BEGIN .* END/xs); |
| 403 |
|
| 404 |
See perlio for further details. |
| 405 |
|
| 406 |
"line_match" |
| 407 |
$rule->line_match(qr/^new/i, qr/^Addition/); |
| 408 |
|
| 409 |
The "line_match" rule takes a list of regular expressions and returns |
| 410 |
files with at least one line that matches one of the expressions. |
| 411 |
|
| 412 |
Files are assumed to be encoded in UTF-8, but alternative Perl IO layers |
| 413 |
can be passed as the first argument. |
| 414 |
|
| 415 |
"shebang" |
| 416 |
$rule->shebang(qr/#!.*\bperl\b/); |
| 417 |
|
| 418 |
The "shebang" rule takes a list of regular expressions or glob patterns |
| 419 |
and checks them against the first line of a file. |
| 420 |
|
| 421 |
Other rules |
| 422 |
"dangling" |
| 423 |
$rule->symlink->dangling; |
| 424 |
$rule->not_dangling; |
| 425 |
|
| 426 |
The "dangling" rule method matches dangling symlinks. Use it or its |
| 427 |
inverse to control how dangling symlinks should be treated. |
| 428 |
|
| 429 |
Negated rules |
| 430 |
Most rule methods have a negated form preceded by "not_". |
| 431 |
|
| 432 |
$rule->not_name("foo.*") |
| 433 |
|
| 434 |
Because this happens automatically, it includes somewhat silly ones like |
| 435 |
"not_nonempty" (which is thus a less efficient way of saying "empty"). |
| 436 |
|
| 437 |
Rules that skip directories or version control files do not have a |
| 438 |
negated version. |
| 439 |
|
| 440 |
EXTENDING |
| 441 |
Custom rule subroutines |
| 442 |
Rules are implemented as (usually anonymous) subroutine callbacks that |
| 443 |
return a value indicating whether or not the rule matches. These |
| 444 |
callbacks are called with three arguments. The first argument is a path, |
| 445 |
which is also locally aliased as the $_ global variable for convenience |
| 446 |
in simple tests. |
| 447 |
|
| 448 |
$rule->and( sub { -r -w -x $_ } ); # tests $_ |
| 449 |
|
| 450 |
The second argument is the basename of the path, which is useful for |
| 451 |
certain types of name checks: |
| 452 |
|
| 453 |
$rule->and( sub { $_[1] =~ /foo|bar/ } ); "foo" or "bar" in basename; |
| 454 |
|
| 455 |
The third argument is a hash reference that can be used to maintain |
| 456 |
state. Keys beginning with an underscore are reserved for |
| 457 |
"Path::Iterator::Rule" to provide additional data about the search in |
| 458 |
progress. For example, the "_depth" key is used to support minimum and |
| 459 |
maximum depth checks. |
| 460 |
|
| 461 |
The custom rule subroutine must return one of four values: |
| 462 |
|
| 463 |
* A true value -- indicates the constraint is satisfied |
| 464 |
|
| 465 |
* A false value -- indicates the constraint is not satisfied |
| 466 |
|
| 467 |
* "\1" -- indicate the constraint is satisfied, and prune if it's a |
| 468 |
directory |
| 469 |
|
| 470 |
* "\0" -- indicate the constraint is not satisfied, and prune if it's |
| 471 |
a directory |
| 472 |
|
| 473 |
A reference is a special flag that signals that a directory should not |
| 474 |
be searched recursively, regardless of whether the directory should be |
| 475 |
returned by the iterator or not. |
| 476 |
|
| 477 |
The legacy "0 but true" value used previously for pruning is no longer |
| 478 |
valid and will throw an exception if it is detected. |
| 479 |
|
| 480 |
Here is an example. This is equivalent to the "max_depth" rule method |
| 481 |
with a depth of 3: |
| 482 |
|
| 483 |
$rule->and( |
| 484 |
sub { |
| 485 |
my ($path, $basename, $stash) = @_; |
| 486 |
return 1 if $stash->{_depth} < 3; |
| 487 |
return \1 if $stash->{_depth} == 3; |
| 488 |
return \0; # should never get here |
| 489 |
} |
| 490 |
); |
| 491 |
|
| 492 |
Files and directories and directories up to depth 3 will be returned and |
| 493 |
directories will be searched. Files of depth 3 will be returned. |
| 494 |
Directories of depth 3 will be returned, but their contents will not be |
| 495 |
added to the search. |
| 496 |
|
| 497 |
Returning a reference is "sticky" -- they will propagate through "and" |
| 498 |
and "or" logic. |
| 499 |
|
| 500 |
0 && \0 = \0 \0 && 0 = \0 0 || \0 = \0 \0 || 0 = \0 |
| 501 |
0 && \1 = \0 \0 && 1 = \0 0 || \1 = \1 \0 || 1 = \1 |
| 502 |
1 && \0 = \0 \1 && 0 = \0 1 || \0 = \1 \1 || 0 = \1 |
| 503 |
1 && \1 = \1 \1 && 1 = \1 1 || \1 = \1 \1 || 1 = \1 |
| 504 |
|
| 505 |
Once a directory is flagged to be pruned, it will be pruned regardless |
| 506 |
of subsequent rules. |
| 507 |
|
| 508 |
$rule->max_depth(3)->name(qr/foo/); |
| 509 |
|
| 510 |
This will return files or directories with "foo" in the name, but all |
| 511 |
directories at depth 3 will be pruned, regardless of whether they match |
| 512 |
the name rule. |
| 513 |
|
| 514 |
Generally, if you want to do directory pruning, you are encouraged to |
| 515 |
use the "skip" method instead of writing your own logic using "\0" and |
| 516 |
"\1". |
| 517 |
|
| 518 |
Extension modules and custom rule methods |
| 519 |
One of the strengths of File::Find::Rule is the many CPAN modules that |
| 520 |
extend it. "Path::Iterator::Rule" provides the "add_helper" method to |
| 521 |
provide a similar mechanism for extensions. |
| 522 |
|
| 523 |
The "add_helper" class method takes three arguments, a "name" for the |
| 524 |
rule method, a closure-generating callback, and a flag for not |
| 525 |
generating a negated form of the rule. Unless the flag is true, an |
| 526 |
inverted "not_*" method is generated automatically. Extension classes |
| 527 |
should call this as a class method to install new rule methods. For |
| 528 |
example, this adds a "foo" method that checks if the filename is "foo": |
| 529 |
|
| 530 |
package Path::Iterator::Rule::Foo; |
| 531 |
|
| 532 |
use Path::Iterator::Rule; |
| 533 |
|
| 534 |
Path::Iterator::Rule->add_helper( |
| 535 |
foo => sub { |
| 536 |
my @args = @_; # do this to customize closure with arguments |
| 537 |
return sub { |
| 538 |
my ($item, $basename) = @_; |
| 539 |
return if -d "$item"; |
| 540 |
return $basename =~ /^foo$/; |
| 541 |
} |
| 542 |
} |
| 543 |
); |
| 544 |
|
| 545 |
1; |
| 546 |
|
| 547 |
This allows the following rule methods: |
| 548 |
|
| 549 |
$rule->foo; |
| 550 |
$fule->not_foo; |
| 551 |
|
| 552 |
The "add_helper" method will warn and ignore a helper with the same name |
| 553 |
as an existing method. |
| 554 |
|
| 555 |
Subclassing |
| 556 |
Instead of processing and returning strings, this module may be |
| 557 |
subclassed to operate on objects that represent files. Such objects must |
| 558 |
stringify to a file path. |
| 559 |
|
| 560 |
The following private implementation methods must be overridden: |
| 561 |
|
| 562 |
* _objectify -- given a path, return an object |
| 563 |
|
| 564 |
* _children -- given a directory, return an (unsorted) list of [ |
| 565 |
basename, full path ] entries within it, excluding "." and ".." |
| 566 |
|
| 567 |
Note that "_children" should return a *list* of *tuples*, where the |
| 568 |
tuples are array references containing basename and full path. |
| 569 |
|
| 570 |
See Path::Class::Rule source for an example. |
| 571 |
|
| 572 |
LEXICAL WARNINGS |
| 573 |
If you run with lexical warnings enabled, "Path::Iterator::Rule" will |
| 574 |
issue warnings in certain circumstances (such as an unreadable directory |
| 575 |
that must be skipped). To disable these categories, put the following |
| 576 |
statement at the correct scope: |
| 577 |
|
| 578 |
no warnings 'Path::Iterator::Rule'; |
| 579 |
|
| 580 |
PERFORMANCE |
| 581 |
By default, "Path::Iterator::Rule" iterator options are "slow but safe". |
| 582 |
They ensure uniqueness, return files in sorted order, and throw nice |
| 583 |
error messages if something goes wrong. |
| 584 |
|
| 585 |
If you want speed over safety, set these options: |
| 586 |
|
| 587 |
%options = ( |
| 588 |
loop_safe => 0, |
| 589 |
sorted => 0, |
| 590 |
depthfirst => -1, |
| 591 |
error_handler => undef |
| 592 |
); |
| 593 |
|
| 594 |
Alternatively, use the "iter_fast" and "all_fast" methods instead, which |
| 595 |
set these options for you. |
| 596 |
|
| 597 |
$iter = $rule->iter( @dirs, \%options ); |
| 598 |
|
| 599 |
$iter = $rule->iter_fast( @dirs ); # same thing |
| 600 |
|
| 601 |
Depending on the file structure being searched, "depthfirst => -1" may |
| 602 |
or may not be a good choice. If you have lots of nested directories and |
| 603 |
all the files at the bottom, a depth first search might do less work or |
| 604 |
use less memory, particularly if the search will be halted early (e.g. |
| 605 |
finding the first N matches.) |
| 606 |
|
| 607 |
Rules will shortcut on failure, so be sure to put rules likely to fail |
| 608 |
early in a rule chain. |
| 609 |
|
| 610 |
Consider: |
| 611 |
|
| 612 |
$r1 = Path::Iterator::Rule->new->name(qr/foo/)->file; |
| 613 |
$r2 = Path::Iterator::Rule->new->file->name(qr/foo/); |
| 614 |
|
| 615 |
If there are lots of files, but only a few containing "foo", then $r1 |
| 616 |
above will be faster. |
| 617 |
|
| 618 |
Rules are implemented as code references, so long chains have some |
| 619 |
overhead. Consider testing with a custom coderef that combines several |
| 620 |
tests into one. |
| 621 |
|
| 622 |
Consider: |
| 623 |
|
| 624 |
$r3 = Path::Iterator::Rule->new->and( sub { -x -w -r $_ } ); |
| 625 |
$r4 = Path::Iterator::Rule->new->executable->writeable->readable; |
| 626 |
|
| 627 |
Rule $r3 above will be much faster, not only because it stacks the file |
| 628 |
tests, but because it requires only a single code reference. |
| 629 |
|
| 630 |
CAVEATS |
| 631 |
Some features are still unimplemented: |
| 632 |
|
| 633 |
* Untainting options |
| 634 |
|
| 635 |
* Some File::Find::Rule helpers (e.g. "grep") |
| 636 |
|
| 637 |
* Extension class loading via "import()" |
| 638 |
|
| 639 |
Filetest operators and stat rules are subject to the usual portability |
| 640 |
considerations. See perlport for details. |
| 641 |
|
| 642 |
SEE ALSO |
| 643 |
There are many other file finding modules out there. They all have |
| 644 |
various features/deficiencies, depending on your preferences and needs. |
| 645 |
Here is an (incomplete) list of alternatives, with some comparison |
| 646 |
commentary. |
| 647 |
|
| 648 |
Path::Class::Rule and IO::All::Rule are subclasses of |
| 649 |
"Path::Iterator::Rule" and operate on Path::Class and IO::All objects, |
| 650 |
respectively. Because of this, they are substantially slower on large |
| 651 |
directory trees than just using this module directly. |
| 652 |
|
| 653 |
File::Find is part of the Perl core. It requires the user to write a |
| 654 |
callback function to process each node of the search. Callbacks must use |
| 655 |
global variables to determine the current node. It only supports |
| 656 |
depth-first search (both pre- and post-order). It supports pre- and |
| 657 |
post-processing callbacks; the former is required for sorting files to |
| 658 |
process in a directory. File::Find::Closures can be used to help create |
| 659 |
a callback for File::Find. |
| 660 |
|
| 661 |
File::Find::Rule is an object-oriented wrapper around File::Find. It |
| 662 |
provides a number of helper functions and there are many more |
| 663 |
"File::Find::Rule::*" modules on CPAN with additional helpers. It |
| 664 |
provides an iterator interface, but precomputes all the results. |
| 665 |
|
| 666 |
File::Next provides iterators for file, directories or "everything". It |
| 667 |
takes two callbacks, one to match files and one to decide which |
| 668 |
directories to descend. It does not allow control over breadth/depth |
| 669 |
order, though it does provide means to sort files for processing within |
| 670 |
a directory. Like File::Find, it requires callbacks to use global |
| 671 |
variables. |
| 672 |
|
| 673 |
Path::Class::Iterator walks a directory structure with an iterator. It |
| 674 |
is implemented as Path::Class subclasses, which adds a degree of extra |
| 675 |
complexity. It takes a single callback to define "interesting" paths to |
| 676 |
return. The callback gets a Path::Class::Iterator::File or |
| 677 |
Path::Class::Iterator::Dir object for evaluation. |
| 678 |
|
| 679 |
File::Find::Object and companion File::Find::Object::Rule are like |
| 680 |
File::Find and File::Find::Rule, but without File::Find inside. They use |
| 681 |
an iterator that does not precompute results. They can return |
| 682 |
File::Find::Object::Result objects, which give a subset of the utility |
| 683 |
of Path::Class objects. File::Find::Object::Rule appears to be a literal |
| 684 |
translation of File::Find::Rule, including oddities like making "-M" |
| 685 |
into a boolean. |
| 686 |
|
| 687 |
File::chdir::WalkDir recursively descends a tree, calling a callback on |
| 688 |
each file. No iterator. Supports exclusion patterns. Depth-first |
| 689 |
post-order by default, but offers pre-order option. Does not process |
| 690 |
symlinks. |
| 691 |
|
| 692 |
File::Find::Iterator is based on iterator patterns in Higher Order Perl. |
| 693 |
It allows a filtering callback. Symlinks are followed automatically |
| 694 |
without infinite loop protection. No control over order. It offers a |
| 695 |
"state file" option for resuming interrupted work. |
| 696 |
|
| 697 |
File::Find::Declare has declarative helper rules, no iterator, is |
| 698 |
Moose-based and offers no control over ordering or following symlinks. |
| 699 |
|
| 700 |
File::Find::Node has no iterator, does matching via callback and offers |
| 701 |
no control over ordering. |
| 702 |
|
| 703 |
File::Set builds up a set of files to operate on from a list of |
| 704 |
directories to include or exclude, with control over recursion. A |
| 705 |
callback is applied to each file (or directory) in the set. There is no |
| 706 |
iterator. There is no control over ordering. Symlinks are not followed. |
| 707 |
It has several extra features for checksumming the set and creating |
| 708 |
tarballs with /bin/tar. |
| 709 |
|
| 710 |
THANKS |
| 711 |
Thank you to Ricardo Signes (rjbs) for inspiring me to write yet another |
| 712 |
file finder module, for writing file finder optimization benchmarks, and |
| 713 |
tirelessly running my code over and over to see if it got faster. |
| 714 |
|
| 715 |
* See the speed of Perl file finders |
| 716 |
<http://rjbs.manxome.org/rubric/entry/1981> |
| 717 |
|
| 718 |
SUPPORT |
| 719 |
Bugs / Feature Requests |
| 720 |
Please report any bugs or feature requests through the issue tracker at |
| 721 |
<https://github.com/dagolden/Path-Iterator-Rule/issues>. You will be |
| 722 |
notified automatically of any progress on your issue. |
| 723 |
|
| 724 |
Source Code |
| 725 |
This is open source software. The code repository is available for |
| 726 |
public review and contribution under the terms of the license. |
| 727 |
|
| 728 |
<https://github.com/dagolden/Path-Iterator-Rule> |
| 729 |
|
| 730 |
git clone https://github.com/dagolden/Path-Iterator-Rule.git |
| 731 |
|
| 732 |
AUTHOR |
| 733 |
David Golden <[email protected]> |
| 734 |
|
| 735 |
CONTRIBUTORS |
| 736 |
* David Steinbrunner <[email protected]> |
| 737 |
|
| 738 |
* Diab Jerius <[email protected]> |
| 739 |
|
| 740 |
* Edward Betts <[email protected]> |
| 741 |
|
| 742 |
* Gian Piero Carrubba <[email protected]> |
| 743 |
|
| 744 |
* Graham Knop <[email protected]> |
| 745 |
|
| 746 |
* Ricardo Signes <[email protected]> |
| 747 |
|
| 748 |
* Slaven Rezic <[email protected]> |
| 749 |
|
| 750 |
* Toby Inkster <[email protected]> |
| 751 |
|
| 752 |
COPYRIGHT AND LICENSE |
| 753 |
This software is Copyright (c) 2013 by David Golden. |
| 754 |
|
| 755 |
This is free software, licensed under: |
| 756 |
|
| 757 |
The Apache License, Version 2.0, January 2004 |
| 758 |
|