File Coverage

blib/lib/Dir/Iterate.pm
Criterion Covered Total %
statement 29 29 100.0
branch 2 2 100.0
condition n/a
subroutine 10 10 100.0
pod 2 2 100.0
total 43 43 100.0


line stmt bran cond sub pod time code
1             package Dir::Iterate;
2              
3             =head1 NAME
4              
5             Dir::Iterate - map/grep-style directory traversal
6              
7             =head1 SYNOPSIS
8              
9             use Dir::Iterate;
10            
11             my @config_dirs = grepdir { -d } '/etc';
12             my @filenames = mapdir { (split '/')[-1] } $ENV{HOME}, '/usr';
13              
14             =head1 DESCRIPTION
15              
16             Dir::Iterate implements equivalents to the built-in C and C functions
17             which traverse directories instead of arrays. The block will be called for
18             each file and directory below the given list of directories. It acts as a
19             more usable layer on top of File::Find.
20              
21             =head2 Functions
22              
23             =over 4
24              
25             =cut
26              
27 3     3   99780 use strict;
  3         7  
  3         108  
28 3     3   17 use warnings;
  3         4  
  3         83  
29              
30 3     3   15 use Exporter;
  3         9  
  3         133  
31 3     3   19 use base 'Exporter';
  3         5  
  3         498  
32              
33             our $VERSION = 0.01;
34             our @EXPORT = qw(grepdir mapdir);
35              
36 3     3   19 use File::Find ();
  3         5  
  3         53  
37 3     3   15 use File::Spec;
  3         16  
  3         703  
38              
39             =item mapdir { ... } $path1[, $path2...]
40              
41             The block is called for each file, folder, or other filesystem entity under the
42             given path(s). The full path to the object is in $_. The return value or
43             values of the block are collected together and returned in a list.
44              
45             =cut
46              
47             sub mapdir(&@) {
48 9     9 1 7938 my($closure, @paths) = @_;
49            
50 9         13 my @results;
51            
52             File::Find::find(
53             {
54             wanted => sub {
55 442     442   30053 local $_ = $File::Find::fullname;
56 442         791 push @results, $closure->();
57             },
58 9         1244 no_chdir => 1,
59             follow => 1
60             },
61 9         66 map { File::Spec->rel2abs($_) } @paths
62             );
63            
64 9         401 return @results;
65             }
66              
67             =item grepdir { ... } $path1[, $path2...]
68              
69             The block is called for each file, folder, or other filesystem entity under the
70             given path(s). The full path to the object is in $_. If the return value of
71             the block is true, the full path will be in the list returned by the method.
72              
73             =cut
74              
75             sub grepdir(&@) {
76 5     5 1 13240 my $predicate = shift;
77 5 100   250   24 unshift @_, sub { $predicate->() ? $_ : () };
  250         434  
78 5         23 goto &mapdir;
79             }
80              
81             =back 4
82              
83             =head1 EXPORTS
84              
85             C and C by default.
86              
87             =head1 AUTHOR
88              
89             Brent Royal-Gordon , for the University of Kent.
90              
91             =head1 LICENSE
92              
93             This library is free software; you can redistribute it and/or modify it under
94             the same terms as Perl itself.
95              
96             =cut
97              
98             1;