1DirCompare(3) User Contributed Perl Documentation DirCompare(3)
2
3
4
6 File::DirCompare - Perl module to compare two directories using
7 callbacks.
8
10 use File::DirCompare;
11
12 # Simple diff -r --brief replacement
13 use File::Basename;
14 File::DirCompare->compare($dir1, $dir2, sub {
15 my ($a, $b) = @_;
16 if (! $b) {
17 printf "Only in %s: %s\n", dirname($a), basename($a);
18 } elsif (! $a) {
19 printf "Only in %s: %s\n", dirname($b), basename($b);
20 } else {
21 print "Files $a and $b differ\n";
22 }
23 });
24
25 # Version-control like Deleted/Added/Modified listing
26 my (@listing, @modified); # use closure to collect results
27 File::DirCompare->compare('old_tree', 'new_tree', sub {
28 my ($a, $b) = @_;
29 if (! $b) {
30 push @listing, "D $a";
31 } elsif (! $a) {
32 push @listing, "A $b";
33 } else {
34 if (-f $a && -f $b) {
35 push @listing, "M $b";
36 push @modified, $b;
37 } else {
38 # One file, one directory - treat as delete + add
39 push @listing, "D $a";
40 push @listing, "A $b";
41 }
42 }
43 });
44
46 File::DirCompare is a perl module to compare two directories using a
47 callback, invoked for all files that are 'different' between the two
48 directories, and for any files that exist only in one or other
49 directory ('unique' files).
50
51 File::DirCompare has a single public compare() method, with the
52 following signature:
53
54 File::DirCompare->compare($dir1, $dir2, $sub, $opts);
55
56 The first three arguments are required - $dir1 and $dir2 are paths to
57 the two directories to be compared, and $sub is the subroutine
58 reference called for all unique or different files. $opts is an
59 optional hashref of options - see OPTIONS below.
60
61 The provided subroutine is called for all unique files, and for every
62 pair of 'different' files encountered, with the following signature:
63
64 $sub->($file1, $file2)
65
66 where $file1 and $file2 are the paths to the two files. For 'unique'
67 files i.e. where a file exists in only one directory, the subroutine is
68 called with the other argument 'undef' i.e. for:
69
70 $sub->($file1, undef)
71 $sub->(undef, $file2)
72
73 the first indicates $file1 exists only in the first directory given
74 ($dir1), and the second indicates $file2 exists only in the second
75 directory given ($dir2).
76
77 OPTIONS
78 The following optional arguments are supported, passed in using a hash
79 reference after the three required arguments to compare() e.g.
80
81 File::DirCompare->compare($dir1, $dir2, $sub, {
82 cmp => $cmp_sub,
83 ignore_unique => 1,
84 });
85
86 cmp By default, two files are regarded as different if their contents
87 do not match (tested with File::Compare::compare). That default
88 behaviour can be overridden by providing a 'cmp' subroutine to do
89 the file comparison, returning zero if the two files are equal, and
90 non-zero if not.
91
92 E.g. to compare using modification times instead of file contents:
93
94 File::DirCompare->compare($dir1, $dir2, $sub, {
95 cmp => sub { -M $_[0] <=> -M $_[1] },
96 });
97
98 ignore_cmp
99 If you want to see all corresponding files, not just 'different'
100 ones, set the 'ignore_cmp' flag to tell File::DirCompare to skip
101 its file comparison checks i.e.
102
103 File::DirCompare->compare($dir1, $dir2, $sub,
104 { ignore_cmp => 1 });
105
106 ignore_unique
107 If you want to ignore files that only exist in one of the two
108 directories, set the 'ignore_unique' flag i.e.
109
110 File::DirCompare->compare($dir1, $dir2, $sub,
111 { ignore_unique => 1 });
112
114 File::Dircmp, which provides similar functionality (and whose directory
115 walking code I've adapted for this module), but a simpler reporting-
116 only interface, something like the first example in the SYNOPSIS above.
117
119 Gavin Carr <gavin@openfusion.com.au>
120
121 Thanks to Robin Barker for a bug report and fix for glob problems with
122 whitespace.
123
125 Copyright 2006-2007 by Gavin Carr.
126
127 This library is free software; you can redistribute it and/or modify it
128 under the same terms as Perl itself.
129
130
131
132perl v5.12.0 2010-03-02 DirCompare(3)