尝试从 perl 调用 find 时出错

尝试从 perl 调用 find 时出错
use warnings;
use File::Find;

my $srceDir = "//mnt/Share_Drive/Verizon PM&T/Capture Files/";
opendir(DIR, $srceDir) or die "Can't open $srceDir: $!";
my @files = (find -type f -newermt "12 Feb 2013", $srceDir);
closedir(DIR);

我可以在 Linux 中find使用该-newermt选项运行该命令,但是当我将其放入 perl 脚本中时,出现以下错误,您能帮忙吗?谢谢

String found where operator expected at ./queryAlm.pl line 11, near "newermt "12 Feb 2013""
  (Do you need to predeclare newermt?)
syntax error at ./queryAlm.pl line 11, near "newermt "12 Feb 2013""

答案1

perlFile::Find模块与该命令几乎没有关系find。请参阅perldoc File::Find如何使用它。

正如 jordanm 指出的,您可以使用find2perl来帮助您编写perl代码,但请注意,它find2perl仅识别标准find语法,因此通常不识别 BSD/GNU 扩展,例如-newermt.您必须自己编写 perl 代码(调用stat()该文件并将其mtime与进行比较POSIX::mktime(0,0,0,12,2,113))。

要运行该find命令,您不需要该File::Find模块,只需执行以下操作:

my $srceDir = "//mnt/Share_Drive/Verizon PM&T/Capture Files/";
my @find_cmd = ("find", $srceDir, "-type", "f", "-newermt", "12 Feb 2013", "-print0");

open FIND, "-|", @find_cmd;
$/ = "\0";
my @files = <FIND>; chomp @files;
my $ret = close FIND or warn $! ?
    "Error closing find pipe: $!" :
    "find exited with non-zero exit status: $?";

相关内容