File Coverage

blib/lib/File/Find/Rule/DirCompare.pm
Criterion Covered Total %
statement 72 76 94.7
branch 20 28 71.4
condition n/a
subroutine 16 16 100.0
pod 0 4 0.0
total 108 124 87.1


line stmt bran cond sub pod time code
1             package File::Find::Rule::DirCompare;
2              
3 3     3   109111 use warnings;
  3         8  
  3         114  
4 3     3   67 use strict;
  3         7  
  3         167  
5              
6 3     3   18 use base 'File::Find::Rule';
  3         11  
  3         2487  
7 3     3   19054 use Carp qw(croak);
  3         6  
  3         194  
8 3     3   16 use File::Spec;
  3         4  
  3         67  
9 3     3   927 use File::stat;
  3         9483  
  3         23  
10 3     3   3164 use Params::Util qw(_ARRAY0);
  3         8959  
  3         238  
11              
12 3     3   3288 use Data::Dumper;
  3         22753  
  3         2345  
13              
14             =head1 NAME
15              
16             File::Find::Rule::DirCompare - Find files by comparing with 2nd directory
17              
18             =head1 VERSION
19              
20             Version 0.01
21              
22             =cut
23              
24             our $VERSION = '0.020';
25              
26             =head1 SYNOPSIS
27              
28             use File::Find::Rule::DirCompare;
29              
30             my @only_in_searchdir = find( file => not_exists_in => $cmpdir, in => $searchdir );
31             my @in_both_dirs = find( file => exists_in => $cmpdir, in => $searchdir );
32             my @newer_in_cmpdir = find( file => newer_in => $cmpdir, in => $searchdir );
33             my @older_in_cmpdir = find( file => older_in => $cmpdir, in => $searchdir );
34              
35             # or use OO interface
36             my $Find;
37              
38             $Find = File::Find::Rule->file->not_exists_in( $cmpdir );
39             my @ois = $Find->in( $searchdir );
40              
41             $Find = File::Find::Rule->file->exists_in( $cmpdir );
42             my @ibd = $Find->in( $searchdir );
43              
44             $Find = File::Find::Rule->file->newer_in( $cmpdir );
45             my @nic = $Find->in( $searchdir );
46              
47             $Find = File::Find::Rule->file->older_in( $cmpdir );
48             my @oic = $Find->in( $searchdir );
49              
50             # sure it can be combined ...
51             $Find = File::Find::Rule->file
52             ->any(
53             File::Find::Rule->new->not_exists_in( $cmpdir ),
54             File::Find::Rule->new->newer_in( $cmpdir ),
55             );
56             my @newer_or_newly = $Find->in( $searchdir );
57              
58             =head1 EXPORT
59              
60             This module doesn't export any function. The provided functionality is called
61             by L according to the matching rules.
62              
63             =head1 SUBROUTINES/METHODS
64              
65             =head2 exists_in
66              
67             Accept a list of directory names to be checked if found files exists in any
68             of them or not. The directory names are interpreted relative to the current
69             directory (L), not relative to the directory specified in C.
70             To ensure the right directory is chosen, specify an absolute path.
71              
72             If the first argument is an array reference, the list in this array
73             will be used.
74              
75             =head2 not_exists_in
76              
77             Accept a list of directory names to be checked if found files does not
78             exists in any of them or not. The directory names are interpreted relative
79             to the current directory (L), not relative to the directory
80             specified in C. To ensure the right directory is chosen, specify an
81             absolute path.
82              
83             If the first argument is an array reference, the list in this array
84             will be used.
85              
86             =head2 newer_in
87              
88             Accept a list of directory names to be checked if found files exists in any
89             of them and have a newer timestamp when it's last modified. The directory
90             names are interpreted relative to the current directory (L), not
91             relative to the directory specified in C. To ensure the right directory
92             is chosen, specify an absolute path.
93              
94             If the first argument is an array reference, the list in this array
95             will be used.
96              
97             =head2 older_in
98              
99             Accept a list of directory names to be checked if found files exists in any
100             of them and have an older timestamp when it's last modified. The directory
101             names are interpreted relative to the current directory (L), not
102             relative to the directory specified in C. To ensure the right directory
103             is chosen, specify an absolute path.
104              
105             If the first argument is an array reference, the list in this array
106             will be used.
107              
108             =cut
109              
110             sub File::Find::Rule::not_exists_in
111             {
112 4     4 0 3724 my $self = shift;
113 4 50       36 my @params = defined( _ARRAY0( $_[0] ) ) ? @{$_[0]} : @_;
  0         0  
114 4 50       17 croak "Missing at least a directory to compare" unless( scalar( @params ) );
115             my $code = sub {
116 16     16   3704 my $fn = $_;
117 16         29 foreach my $cmpdir (@params)
118             {
119 16         153 my $cmpfn = File::Spec->catfile( $cmpdir, $fn );
120 16 100       463 return 1 unless( -e $cmpfn );
121             }
122 12         291 return;
123 4         28 };
124 4         23 $self->exec( $code );
125             }
126              
127             sub File::Find::Rule::exists_in
128             {
129 3     3 0 3778 my $self = shift;
130 3 50       21 my @params = defined( _ARRAY0( $_[0] ) ) ? @{$_[0]} : @_;
  0         0  
131 3 50       16 croak "Missing at least a directory to compare" unless( scalar( @params ) );
132             my $code = sub {
133 12     12   2417 my $fn = $_;
134 12         23 foreach my $cmpdir (@params)
135             {
136 12         166 my $cmpfn = File::Spec->catfile( $cmpdir, $fn );
137 12 100       488 return 1 if( -e $cmpfn );
138             }
139 3         73 return;
140 3         17 };
141 3         14 $self->exec( $code );
142             }
143              
144             sub File::Find::Rule::newer_in
145             {
146 4     4 0 2261 my $self = shift;
147 4 50       27 my @params = defined( _ARRAY0( $_[0] ) ) ? @{$_[0]} : @_;
  0         0  
148 4 50       15 croak "Missing at least a directory to compare" unless( scalar( @params ) );
149             my $code = sub {
150 15     15   2216 my $fn = $_;
151 15         47 my $fnstat = stat($fn);
152 15         2015 my $fnmtime = $fnstat->mtime();
153 15         100 foreach my $cmpdir (@params)
154             {
155 15         156 my $cmpfn = File::Spec->catfile( $cmpdir, $fn );
156 15 100       54 my $cmpstat = stat($cmpfn) or next;
157 12         1653 my $cmpmtime = $cmpstat->mtime();
158 12 100       236 return 1 if( $fnmtime < $cmpmtime );
159             }
160 11         492 return;
161 4         26 };
162 4         18 $self->exec( $code );
163             }
164              
165             sub File::Find::Rule::older_in
166             {
167 3     3 0 2276 my $self = shift;
168 3 50       27 my @params = defined( _ARRAY0( $_[0] ) ) ? @{$_[0]} : @_;
  0         0  
169 3 50       12 croak "Missing at least a directory to compare" unless( scalar( @params ) );
170             my $code = sub {
171 12     12   2213 my $fn = $_;
172 12         36 my $fnstat = stat($fn);
173 12         1457 my $fnmtime = $fnstat->mtime();
174 12         73 foreach my $cmpdir (@params)
175             {
176 12         123 my $cmpfn = File::Spec->catfile( $cmpdir, $fn );
177 12 100       40 my $cmpstat = stat($cmpfn) or next;
178 9         1200 my $cmpmtime = $cmpstat->mtime();
179 9 100       145 return 1 if( $fnmtime > $cmpmtime );
180             }
181 9         395 return;
182 3         22 };
183 3         15 $self->exec( $code );
184             }
185              
186             =head1 AUTHOR
187              
188             Jens Rehsack, C<< >>
189              
190             =head1 BUGS
191              
192             Please report any bugs or feature requests to
193             C, or through the web
194             interface at
195             L.
196             I will be notified, and then you'll automatically be notified of progress
197             on your bug as I make changes.
198              
199             =head1 SUPPORT
200              
201             You can find documentation for this module with the perldoc command.
202              
203             perldoc File::Find::Rule::DirCompare
204              
205             You can also look for information at:
206              
207             =over 4
208              
209             =item * RT: CPAN's request tracker
210              
211             L
212              
213             =item * AnnoCPAN: Annotated CPAN documentation
214              
215             L
216              
217             =item * CPAN Ratings
218              
219             L
220              
221             =item * Search CPAN
222              
223             L
224              
225             =back
226              
227             =head1 LICENSE AND COPYRIGHT
228              
229             Copyright 2010-2013 Jens Rehsack.
230              
231             This program is free software; you can redistribute it and/or modify it
232             under the terms of either: the GNU General Public License as published
233             by the Free Software Foundation; or the Artistic License.
234              
235             See http://dev.perl.org/licenses/ for more information.
236              
237             =cut
238              
239             1; # End of File::Find::Rule::DirCompare