我想我会使用glob,因为你真的不需要所有目录遍历的东西:
my @files = glob( '*.txt */*.txt' );
我创建了File::Find::Closures,以便您轻松创建传递给find 的回调:
use File::Find::Closures qw( find_by_regex );
use File::Find qw( find );
my( $wanted, $reporter ) = File::Find::Closures::find_by_regex( qr/\.txt\z/ );
find( $wanted, @dirs );
my @files = $reporter->();
通常,您可以使用 find2perl 将 find(1) 命令转换为 Perl 程序(在 v5.20 中已删除,但在 CPAN 上):
% find2perl my_dir -d 2 -name "*.txt"
但显然find2perl 不理解-maxdepth,所以你可以不说:
% find2perl my_dir -name "*.txt"
#! /usr/local/perls/perl-5.13.5/bin/perl5.13.5 -w
eval 'exec /usr/local/perls/perl-5.13.5/bin/perl5.13.5 -S $0 ${1+"$@"}'
if 0; #$running_under_some_shell
use strict;
use File::Find ();
# Set the variable $File::Find::dont_use_nlink if you're using AFS,
# since AFS cheats.
# for the convenience of &wanted calls, including -eval statements:
use vars qw/*name *dir *prune/;
*name = *File::Find::name;
*dir = *File::Find::dir;
*prune = *File::Find::prune;
sub wanted;
# Traverse desired filesystems
File::Find::find({wanted => \&wanted}, 'my_dir');
exit;
sub wanted {
/^.*\.txt\z/s
&& print("$name\n");
}
现在您已经开始编程,您可以插入任何其他所需内容,包括修剪树的preprocess 步骤。