perl - File::Find 是否有更简洁的方法来返回所需文件的列表?

标签 perl file-find

我发现 File::Find::find 背后的设计选择有点意外。我遇到的所有示例都显示 find 在 void 上下文中使用。

documentation还澄清了 find(\&wanted, @dirs ) 中的 \&wanted coderef 并不是一个过滤器(强调我自己的):

The wanted() function does whatever verifications you want on each file and directory. Note that despite its name, the wanted() function is a generic callback function, and does not tell File::Find if a file is "wanted" or not. In fact, its return value is ignored.


但是如果我确实想以类似于 grep 的方式将其用作过滤器怎么办? ?我很想知道是否还有另一种写法:

use strict;
use warnings;
use feature 'say';

use File::Find;

my $wanted = qr/^\d{2}_/;  # e.g.

my @wanted;
find( sub { -f && /$wanted/ && push @wanted, $_ }, '.' );

# I wish my @wanted = find( ... ); worked

say for @wanted;

最佳答案

在 CPAN 上,我发现了几个 File::Find 的替代接口(interface),旨在简化接口(interface)。

我会尝试 File::Finder ,首先由著名的 Perl 专家 Randal Schwartz 撰写。

File::Find::Rule是另一个。

(可以肯定地说,如果人们正在编写模块来执行此操作,那么没有简单的内置方法可以做到这一点。)

关于perl - File::Find 是否有更简洁的方法来返回所需文件的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12620423/

相关文章:

Perl File::Find::Rule 从数组中排除目录

perl - 跳过文件的前 x 行和最后 y 行

perl - 如何将两个数组和一个字符串传递给 Perl 子例程?

Perl - 如何更改字符串中出现的每个 $variable ";"

perl - 我的 Perl 代码如何捕获 Ctrl+D?

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

linux - 在终端/Linux 上查找包含特定日期范围文件名的所有文件

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

perl - 虫虫 | perl模块安装噩梦: no cc, gcc可以吗?