确定为什么-f标记只说.pl文件是文件

Kat*_*Kat 1 directory perl file

我正在编写的程序假设打开两个目录并读取其中的文件以比较这些文件的内容.然后应将文件中已更改的函数打印到文件中.该程序主要是检查.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
Run Code Online (Sandbox Code Playgroud)

这是我想要打开文件来阅读它们的地方:

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
}
Run Code Online (Sandbox Code Playgroud)

我尝试添加标志

my @allNewFiles = grep { $_ ne '.' and $_ ne '..' && -e $_} readdir newDir;
my @allNewFiles = grep { $_ ne '.' and $_ ne '..' && -f $_} readdir newDir;
Run Code Online (Sandbox Code Playgroud)

但这只会删除所有不是.pl文件扩展名的文件.我测试了一些简单的目录,我有两个.txt,.cpp,.h,.c,.py和.pl文件扩展名的副本,它只会显示.pl文件是文件.

我是perl的新手,任何帮助都将不胜感激.

ike*_*ami 6

-f返回undef$!设置为"No such file or directory",因为您要传递文件名-f而不是文件路径.

更改

-f $_
Run Code Online (Sandbox Code Playgroud)

-f "$newDir/$_"
Run Code Online (Sandbox Code Playgroud)