我怎样才能在Perl中使用File :: Find? [英] How can I use File::Find in Perl?

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

问题描述

我从 File :: Find 文档有点困惑...什么是等于 $ find my_dir -maxdepth 2 -name * .txt

解决方案

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

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

my $ dir = shift;
my $ level = shift // 2;
$ b $ my @files = File :: Find :: Rule-> file()
- > name(*。txt)
- > maxdepth($级别)
- > in($ dir);

打印Dumper(\ @ files);

或者创建一个迭代器:

<$ p $ b $> code $ my $ ffr_obj = File :: Find :: Rule-> file()
- > name(*。txt)
- > maxdepth $ level)
- > start($ dir); ($ my $ file = $ ffr_obj-> match())
{
print$ file \\\

}
$ b


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

解决方案

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);

Or alternatively create an iterator:

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

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

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

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