我想递归地读出一个目录,以使用 Template::Toolkit 打印 HTML 页面中的数据结构。 但我一直在思考如何以易于阅读的形式保存路径和文件。
我的想法是这样开始的
sub list_dirs{
my ($rootPath) = @_;
my (@paths);
$rootPath .= '/' if($rootPath !~ /\/$/);
for my $eachFile (glob($path.'*'))
{
if(-d $eachFile)
{
push (@paths, $eachFile);
&list_dirs($eachFile);
}
else
{
push (@files, $eachFile);
}
}
return @paths;
}
我该如何解决这个问题?
最佳答案
这应该可以解决问题
use strict;
use warnings;
use File::Find qw(finddepth);
my @files;
finddepth(sub {
return if($_ eq '.' || $_ eq '..');
push @files, $File::Find::name;
}, '/my/dir/to/search');
关于perl - 如何在 Perl 中递归读取目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2476019/