如何在 Perl 中使用 File::Find? [英] How can I use File::Find in Perl?

查看:19
本文介绍了如何在 Perl 中使用 File::Find?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 File::Find 文档有点困惑... $ find my_dir -maxdepth 2 -name "*.txt" 的等价物是什么?

I'm a bit confused from File::Find documentation... What is the equivalent to $ find my_dir -maxdepth 2 -name "*.txt"?

推荐答案

我个人更喜欢 File::Find::Rule 因为这不需要你创建回调例程.

Personally, I prefer File::Find::Rule as this doesn't need you to create callback routines.

use strict;
use Data::Dumper;
use File::Find::Rule;

my $dir = shift;
my $level = shift // 2;

my @files = File::Find::Rule->file()
                            ->name("*.txt")
                            ->maxdepth($level)
                            ->in($dir);

print Dumper(@files);

或者创建一个迭代器:

my $ffr_obj = File::Find::Rule->file()
                              ->name("*.txt")
                              ->maxdepth($level)
                              ->start($dir);

while (my $file = $ffr_obj->match())
{
    print "$file
"
}

这篇关于如何在 Perl 中使用 File::Find?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆