且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我怎样才能在Perl中使用File :: Find?

更新时间:2023-01-19 17:37:44

个人而言,我更喜欢 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);

或者创建一个迭代器:

- > 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"
}