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_cmp => 1,
84 ignore_unique => 1,
85 matches => $matches_sub,
86 });
87
88 cmp By default, two files are regarded as different if their contents
89 do not match (tested with File::Compare::compare). That default
90 behaviour can be overridden by providing a 'cmp' subroutine to do
91 the file comparison, returning zero if the two files are equal, and
92 non-zero if not.
93
94 E.g. to compare using modification times instead of file contents:
95
96 File::DirCompare->compare($dir1, $dir2, $sub, {
97 cmp => sub { -M $_[0] <=> -M $_[1] },
98 });
99
100 ignore_cmp
101 If you want to see all corresponding files, not just 'different'
102 ones, set the 'ignore_cmp' flag to tell File::DirCompare to skip
103 its file comparison checks i.e.
104
105 File::DirCompare->compare($dir1, $dir2, $sub,
106 { ignore_cmp => 1 });
107
108 ignore_unique
109 If you want to ignore files that only exist in one of the two
110 directories, set the 'ignore_unique' flag i.e.
111
112 File::DirCompare->compare($dir1, $dir2, $sub,
113 { ignore_unique => 1 });
114
115 matches
116 Subroutine to be called for file pairs that match, with the
117 following signature:
118
119 $sub->($file1, $file2)
120
121 These pairs are ordinarily ignored (unless "ignore_cmp" is set).
122
124 File::Dircmp, which provides similar functionality (and whose directory
125 walking code I've adapted for this module), but a simpler reporting-
126 only interface, something like the first example in the SYNOPSIS above.
127
129 Gavin Carr <gavin@openfusion.com.au>
130
131 Thanks to Robin Barker for a bug report and fix for glob problems with
132 whitespace.
133
135 Copyright 2006-2012 by Gavin Carr <gavin@openfusion.com.au>.
136
137 This library is free software; you can redistribute it and/or modify it
138 under the same terms as Perl itself.
139
140
141
142perl v5.32.1 2021-01-27 DirCompare(3)