【问题标题】:Is it possible to provide find2perl mindepth and maxdepth arguments?是否可以提供 find2perl mindepth 和 maxdepth 参数?
【发布时间】:2012-11-14 07:50:13
【问题描述】:

我没有看到 find2perl 文档提到任何关于支持 -mindepth-maxdepth 参数的内容。

以下示例适用于find

$ find2perl . -mindepth 2 -maxdepth 2 -name "*txt" -type f
Unrecognized switch: -mindepth

问题:

  • find2perl 是否支持此类功能?
  • 如果是这样,我该如何指定mindepthmaxdepth

【问题讨论】:

  • 我不知道答案,但我喜欢你的问题 ID 以 1337 开头。 :)
  • 文档中均未提及。为什么不试试看呢?
  • @ikegami :我试过了。开关无法识别。
  • @memowe : 请原谅我的无知,但意义何在?

标签: perl


【解决方案1】:

File::Find::Rule有这些选项,它有一个命令行程序findrule

如果你想用 File::Find 来做,你可以通过检查文件的深度来实现 maxdepth,当你走得太深时设置$File::Find::prune。 mindepth 是类似的,但是你会提前从你的函数中返回。今天早上我很懒,所以我会把编码留给其他人。

更新:其他人进行了编程,即 File::Find::Rule。 Here's the code they use.

my $maxdepth = 2;
my $mindepth = 2;
my $topdir   = "something/something/something";
sub wanted {
    # figure out the relative path and depth
    my $relpath = $File::Find::name;
    $relpath =~ s{^\Q$topdir\E/?}{};
    my $depth = File::Spec->splitdir($relpath);

    defined $maxdepth && $depth >= $maxdepth
       and $File::Find::prune = 1;

    defined $mindepth && $depth < $mindepth
       and return;

    ...your code goes here...
}

find \&wanted, $topdir;

【讨论】:

  • 看起来F::F 最初并未设置为处理这些开关。哦,好吧...
【解决方案2】:

我刚刚查看了 find2perl 和 File::Find 的源代码,它是 find2perl 使用的目录遍历器。目前既没有实现 mindepth 也没有实现 maxdepth。

File::Find 在遍历目录时显示轨道深度,在一个名为 $CdLvl 的变量中。您可以通过与 $CdLvl 进行比较来实现 mindepth 和 maxdepth。

【讨论】:

  • 这是 File::Find 内部的一个词法变量。搞不定。
  • 我巧妙地鼓励 OP 将该功能添加到 F::F。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-14
  • 2021-08-06
  • 2013-06-25
  • 2012-09-10
  • 2022-01-08
相关资源
最近更新 更多