我正在编写的程序假设打开两个目录并阅读其中的文件以比较这些文件的内容。然后应该将文件中已更改的功能打印到文件中。该程序将主要检查 .cpp 文件和 .h 文件。
目前我正在尝试浏览目录并打开我所在的当前文件以打印已更改的功能。但是,我不断收到一条错误消息,指出该文件不是文件并且无法打开。
这是我正在使用的当前代码的一部分
use strict;
use warnings;
use diagnostics -verbose;
use File::Compare;
use Text::Diff;
my $newDir = 'C:\Users\kkahla\Documents\Perl\TestFiles2';
my $oldDir = 'C:\Users\kkahla\Documents\Perl\TestFiles';
chomp $newDir;
$newDir =~ s#/$##;
chomp $oldDir;
$oldDir =~ s#/$##;
# Checks to make sure they are directories
unless(-d $newDir or -d $oldDir) {
print STDERR "Invalid directory path for one of the directories";
exit(0);
}
# Makes a directory for the outputs to go to unless one already exists
mkdir "Outputs", 0777 unless -d "Outputs";
# opens output file
open (OUTPUTFILE, ">Outputs\\diffDirectoriesOutput.txt");
print OUTPUTFILE "Output statistics for comparing two directories\n\n";
# opens both directories
opendir newDir, $newDir;
my @allNewFiles = grep { $_ ne '.' and $_ ne '..'} readdir newDir;
closedir newDir;
opendir oldDir, $oldDir;
my @allOldFiles = grep { $_ ne '.' and $_ ne '..'} readdir oldDir;
closedir oldDir
这是我要打开文件以阅读它们的地方:
elsif((File::Compare::compare("$newDir/$_", "$oldDir/$_") == 1)) {
print OUTPUTFILE "File: $_ has been update. Please check marked functions for differences\n\n";
diff "$newDir/$_", "$oldDir/$_", { STYLE => "Table" , OUTPUT => \*OUTPUTFILE};
#Here is where I want to open the file but when I try it throws an error
#Here are the two opens I have tried:
open (FILE, "<$newDir/$_") or die "Can't open file"; #first attempt
open (FILE, "<$_") or die "Can't open file"; #second attempt to see if it worked
}
我尝试添加标志
my @allNewFiles = grep { $_ ne '.' and $_ ne '..' && -e $_} readdir newDir;
my @allNewFiles = grep { $_ ne '.' and $_ ne '..' && -f $_} readdir newDir;
但这只会删除所有不是 .pl 文件扩展名的文件。我在一些简单的目录上进行了测试,这些目录有两个 .txt、.cpp、.h、.c、.py 和 .pl 文件扩展名的副本,它只会显示 .pl 文件是文件。
我是 perl 的新手,任何帮助将不胜感激。