我已经学习 perl6/raku 一段时间了,我真的很喜欢这个dir例程https://docs.perl6.org/routine/dir。它非常方便且易于使用。
有什么方法可以导入/反向移植dir到 Perl 中吗?我无法通过互联网搜索获得任何结果。
这与您从readdir获得的非常相似。
use strict;
use warnings;
open my $dh, $dirpath or die "Failed to open $dirpath: $!";
foreach my $file (readdir $dh) {
next if $file eq '.' or $file eq '..';
print "$dirpath/$file: $file\n";
}
Run Code Online (Sandbox Code Playgroud)
我的Dir::ls使这变得更简洁,但它的设计更多是为了模拟ls而不是在编程上有用。
use strict;
use warnings;
use Dir::ls;
foreach my $file (ls $dirpath) {
print "$dirpath/$file: $file\n";
}
Run Code Online (Sandbox Code Playgroud)
Path::Tiny使常见情况像往常一样简单 - 所有路径都是 Path::Tiny 对象。
use strict;
use warnings;
use Path::Tiny;
foreach my $filepath (path($dirpath)->children) {
my $file = $filepath->basename;
print "$filepath: $file\n";
}
Run Code Online (Sandbox Code Playgroud)
它可以过滤正则表达式(应用于基本名称,而不是完整路径):
path($dirpath)->children(qr/\.txt$/);
Run Code Online (Sandbox Code Playgroud)