| 1 | package File::Find::Wanted; | ||||||||||
| 2 | |||||||||||
| 3 | =head1 NAME | ||||||||||
| 4 | |||||||||||
| 5 | File::Find::Wanted - More obvious wrapper around File::Find | ||||||||||
| 6 | |||||||||||
| 7 | =head1 VERSION | ||||||||||
| 8 | |||||||||||
| 9 | Version 1.00 | ||||||||||
| 10 | |||||||||||
| 11 | =cut | ||||||||||
| 12 | |||||||||||
| 13 | 3 | our $VERSION = '1.00'; | |||||||||
| 14 | |||||||||||
| 15 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 3 | ▶ | use File::Find; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | |||||||||||
| 18 | 3 | our @ISA = qw( Exporter ); | |||||||||
| 19 | 3 | our @EXPORT = qw( find_wanted ); | |||||||||
| 20 | |||||||||||
| 21 | =head1 SYNOPSIS | ||||||||||
| 22 | |||||||||||
| 23 | File::Find is a great module, except that it doesn't actually find | ||||||||||
| 24 | anything. Its C<find()> function walks a directory tree and calls | ||||||||||
| 25 | a callback function. Unfortunately, the callback function is | ||||||||||
| 26 | deceptively called C<wanted>, which implies that it should return | ||||||||||
| 27 | a boolean saying whether you want the file. That's not how it | ||||||||||
| 28 | works. | ||||||||||
| 29 | |||||||||||
| 30 | Most of the time you call C<find()>, you just want to build a list | ||||||||||
| 31 | of files. There are other modules that do this for you, most notably | ||||||||||
| 32 | Richard Clamp's great L<File::Find::Rule>, but in many cases, it's | ||||||||||
| 33 | overkill, and you need to learn a new syntax. | ||||||||||
| 34 | |||||||||||
| 35 | With the C<find_wanted> function, you supply a callback sub and a | ||||||||||
| 36 | list of starting directories, but the sub actually should return a | ||||||||||
| 37 | boolean saying whether you want the file in your list or not. | ||||||||||
| 38 | |||||||||||
| 39 | To get a list of all files ending in F<.jpg>: | ||||||||||
| 40 | |||||||||||
| 41 | my @files = find_wanted( sub { -f && /\.jpg$/ }, $dir ); | ||||||||||
| 42 | |||||||||||
| 43 | For a list of all directories that are not F<CVS> or F<.svn>: | ||||||||||
| 44 | |||||||||||
| 45 | my @files = find_wanted( sub { -d && !/^(CVS|\.svn)$/ }, $dir ) ); | ||||||||||
| 46 | |||||||||||
| 47 | It's easy, direct, and simple. | ||||||||||
| 48 | |||||||||||
| 49 | =head1 WHY DO THIS? | ||||||||||
| 50 | |||||||||||
| 51 | The cynical may say "that's just the same as doing this": | ||||||||||
| 52 | |||||||||||
| 53 | my @files; | ||||||||||
| 54 | find( sub { push @files, $File::Find::name if -f && /\.jpg$/ }, $dir ); | ||||||||||
| 55 | |||||||||||
| 56 | Sure it is, but File::Find::Wanted makes it more obvious, and saves | ||||||||||
| 57 | a line of code. That's worth it to me. I'd like it if find_wanted() | ||||||||||
| 58 | made its way into the File::Find distro, but for now, this will do. | ||||||||||
| 59 | |||||||||||
| 60 | =head1 FUNCTIONS | ||||||||||
| 61 | |||||||||||
| 62 | =head2 find_wanted( I<&wanted>, I<@directories> ) | ||||||||||
| 63 | |||||||||||
| 64 | Descends through I<@directories>, calling the I<wanted> function as | ||||||||||
| 65 | it finds each file. The function returns a list of all the files and | ||||||||||
| 66 | directories for which the I<wanted> function returned a true value. | ||||||||||
| 67 | |||||||||||
| 68 | This is just a wrapper around C<File::Find::find()>. See L<File::Find> | ||||||||||
| 69 | for details on how to modify its behavior. | ||||||||||
| 70 | |||||||||||
| 71 | =cut | ||||||||||
| 72 | |||||||||||
| 73 | 2 | ▶ | sub find_wanted { | ||||||||
|
Subroutine
find_wanted: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 74 | 2 | my $func = shift; | |||||||||
| 75 | 2 | my @files; | |||||||||
| 76 | |||||||||||
| 77 | 2 | local $_; | |||||||||
| 78 | 2 | ▶ | find( sub { push @files, $File::Find::name if &$func }, @_ ); | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||
| 79 | |||||||||||
| 80 | 2 | return @files; | |||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | =head1 COPYRIGHT & LICENSE | ||||||||||
| 84 | |||||||||||
| 85 | Copyright 2005-2012 Andy Lester. | ||||||||||
| 86 | |||||||||||
| 87 | This program is free software; you can redistribute it and/or modify | ||||||||||
| 88 | it under the terms of the Artistic License v2.0. | ||||||||||
| 89 | |||||||||||
| 90 | =cut | ||||||||||
| 91 | |||||||||||
| 92 | 3 | 1; | |||||||||