perl - 找到特定文件后,如何摆脱递归查找功能?

标签 perl recursion file-find

我正在使用 File::Find遍历目录树的模块。找到特定文件后,我想停止搜索。我怎样才能做到这一点?

   find (\$processFile, $mydir);

   sub processFile() {
      if ($_ =~ /target/) {
         # How can I return from find here?
      }
   }

最佳答案

似乎你将不得不死:

eval {
    find (\$processFile, $mydir);
};

if ( $@ ) {
   if ( $@ =~ m/^found it/ ) {
        # be happy
    }
    else ( $@ ) {
        die $@;
    }
}
else {
   # be sad
}


sub processFile() {
   if ($_ =~ /target/) {
      die 'found it';
   }
}

关于perl - 找到特定文件后,如何摆脱递归查找功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/381395/

相关文章:

perl - 将 -1 分配给 $#array 有什么作用?

perl - 在主目录中安装 perl 模块时出现问题

linux - Perl 模块在 Linux 和 Win32 下运行

java - 构建 ArrayList 树 - Java

sql-server - 将触发表上的更新将是递归的

linux - 对我的 Cron 工作感到困惑

python - 超出最大递归深度,但仅在使用装饰器时

perl - 为什么 File::Find 不处理我损坏的符号链接(symbolic link)?

perl - 用 Perl 从网络驱动器读取文件