我试图找出 Perl 中两个目录之间的区别。我想优化它以高效运行,也不确定如何忽略某些文件(比如扩展名为 .txt 或 .o)
我到目前为止的代码是:
use strict;
use warnings;
use Parallel::ForkManager;
use File::Find;
use List::MoreUtils qw(uniq);
my $dir1 = "/path/to/dir/first";
my $dir2 = "/path/to/dir/second";
my @comps = ('abc');
my (%files1, %files2);
my $workernum = 500;
my $pm = new Parallel::ForkManager($workernum);
my @common = ();
my @differ = ();
my @only_in_first = ();
my @only_in_second = ();
foreach my $comp (@comps) {
find( sub { -f ($files1{$_} = $File::Find::name) }, "$dir1");
find( sub { -f ($files2{$_} = $File::Find::name) }, "$dir2");
my @all = uniq(keys %files1, keys %files2);
for my $file (@all) {
my $pid = $pm->start and next; # do the fork
my $result;
if ($files1{$file} && $files2{$file}) { # file exists in both dirs
$result = qx(/usr/bin/diff -q $files1{$file} $files2{$file});
if ($result =~m/^Common subdirectories/) {
push (@common, $result);
} else {
push (@differ, $result);
}
} elsif ($files1{$file}) {
push (@only_in_first, $file);
} else {
push (@only_in_second, $file);
}
$pm->finish; # do the exit in child process
}
}