一旦找到特定文件,我怎样才能打破递归查找功能?

MCS*_*MCS 5 recursion perl file-find

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

   find (\$processFile, $mydir);

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

inn*_*naM 9

好像你将不得不死:

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

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


sub processFile() {
   if ($_ =~ /target/) {
      die 'found it';
   }
}
Run Code Online (Sandbox Code Playgroud)