DirCompare(3pm)
π NAME π
hiii bestie, welcome to DirCompare(3pm) β the manual page for a little Perl gem called File::DirCompare: a Perl module that compares two directories using callbacks. yes, callbacks, we love a callback π
β¨ SYNOPSIS β¨
okay so here's how you actually use it, copy-paste energy, no cap:
use File::DirCompare;
# Simple diff -r --brief replacement
use File::Basename;
File::DirCompare->compare($dir1, $dir2, sub {
my ($a, $b) = @_;
if (! $b) {
printf "Only in %s: %s\n", dirname($a), basename($a);
} elsif (! $a) {
printf "Only in %s: %s\n", dirname($b), basename($b);
} else {
print "Files $a and $b differ\n";
}
});
# Version-control like Deleted/Added/Modified listing
my (@listing, @modified); # use closure to collect results
File::DirCompare->compare('old_tree', 'new_tree', sub {
my ($a, $b) = @_;
if (! $b) {
push @listing, "D $a";
} elsif (! $a) {
push @listing, "A $b";
} else {
if (-f $a && -f $b) {
push @listing, "M $b";
push @modified, $b;
} else {
# One file, one directory - treat as delete + add
push @listing, "D $a";
push @listing, "A $b";
}
}
});
like, that's it. that's the whole vibe π
π DESCRIPTION π
File::DirCompare is a perl module to compare two directories using a callback. that callback gets invoked for all files that are 'different' between the two directories, AND for any files that exist only in one or the other directory β the so-called 'unique' files. she's thorough, she's giving attention to detail π«Ά
File::DirCompare has a single public compare() method, with the following signature:
File::DirCompare->compare($dir1, $dir2, $sub, $opts);
The first three arguments are required, periodt:
$dir1and$dir2are paths to the two directories to be compared.$subis the subroutine reference called for all unique or different files.
$opts is an optional hashref of options β see OPTIONS below, bestie π
The provided subroutine is called for all unique files, and for every pair of 'different' files encountered, with the following signature:
$sub->($file1, $file2)
where $file1 and $file2 are the paths to the two files. For 'unique' files i.e. where a file exists in only one directory, the subroutine is called with the other argument 'undef' i.e. for:
$sub->($file1, undef)
$sub->(undef, $file2)
- the first one indicates
$file1exists only in the first directory given ($dir1) π - and the second indicates
$file2exists only in the second directory given ($dir2) π
iykyk. it's literally just "who's here and who isn't" π
πΈ OPTIONS πΈ
The following optional arguments are supported, passed in using a hash reference after the three required arguments to compare() e.g.
File::DirCompare->compare($dir1, $dir2, $sub, {
cmp => $cmp_sub,
ignore_cmp => 1,
ignore_unique => 1,
matches => $matches_sub,
});
and here's the tea on each one π
cmpβ By default, two files are regarded as different if their contents do not match (tested withFile::Compare::compare). That default behaviour can be overridden by providing a'cmp'subroutine to do the file comparison, returning zero if the two files are equal, and non-zero if not.
E.g. to compare using modification times instead of file contents:
File::DirCompare->compare($dir1, $dir2, $sub, {
cmp => sub { -M $_[0] <=> -M $_[1] },
});
ignore_cmpβ If you want to see all corresponding files, not just 'different' ones, set the'ignore_cmp'flag to tellFile::DirCompareto skip its file comparison checks i.e.
File::DirCompare->compare($dir1, $dir2, $sub,
{ ignore_cmp => 1 });
ignore_uniqueβ If you want to ignore files that only exist in one of the two directories, set the'ignore_unique'flag i.e.
File::DirCompare->compare($dir1, $dir2, $sub,
{ ignore_unique => 1 });
matchesβ Subroutine to be called for file pairs that match, with the following signature:
$sub->($file1, $file2)
These pairs are ordinarily ignored (unless "ignore_cmp" is set).
okay important: those matching pairs get ignored by default unless"ignore_cmp"is set, so don't come crying to me when yourmatchescallback is sitting there in silence π
π₯ SEE ALSO π₯
File::Dircmp β which provides similar functionality (and whose directory walking code I've adapted for this module), but a simpler reporting-only interface, something like the first example in the SYNOPSIS above. so like, if you want the chill low-effort version, she's right there π
π AUTHOR AND CREDITS π
Gavin Carr <gavin@openfusion.com.au>
Thanks to Robin Barker for a bug report and fix for glob problems with whitespace. shoutout Robin, doing the lord's work π«‘
π COPYRIGHT AND LICENSE π
Copyright 2006-2012 by Gavin Carr <gavin@openfusion.com.au>.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
and that's the whole page bestie, no notes π
this was DirCompare(3pm) from perl v5.26.1, dated 2018-03-31 β go forth and diff those directories, you absolute icon π¦β¨
π¦ where this page comes from
- debian bookworm: libfile-dircompare-perl 0.7-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libfile-dircompare-perl 0.7-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu jammy: libfile-dircompare-perl 0.7-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libfile-dircompare-perl 0.7-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms