如何在 Perl 中递归读取目录?
我想递归地读出一个目录,以使用 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;
}
我该如何解决这个问题?
I want to read out a directory recursively to print the data-structure in an HTML-Page with Template::Toolkit.
But I'm hanging in how to save the Paths and Files in a form that can be read our easy.
My idea started like this
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;
}
How could I solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这应该可以解决问题
This should do the trick
您应该始终使用严格和警告来帮助您调试代码。例如,Perl 会警告您未声明
@files
。但您的函数的真正问题在于,您在每次递归调用list_dirs
时声明了一个词法变量@paths
,并且在递归步骤之后不将返回值推回。如果您不想安装额外的模块,以下解决方案可能会帮助您:
You should always use strict and warnings to help you debug your code. Perl would have warned you for example that
@files
is not declared. But the real problem with your function is that you declare a lexical variable@paths
on every recursive call tolist_dirs
and don't push the return value back after the recursion step.If you don't want to install additional modules, the following solution should probably help you:
mdom 的答案解释了您最初的尝试是如何误入歧途的。我还建议您考虑使用
File::Find
更友好的替代方案。 CPAN 有多种选择。这是一个。另请参阅此处:
SO 答案 提供CPAN
File::Find
的替代方案。关于目录迭代器的SO问题。
这是递归解决方案的重写。注意事项:
使用严格
;使用警告
;以及使用作用域块为子例程创建静态变量。The answer by mdom explains how your initial attempt went astray. I would also suggest that you consider friendlier alternatives to
File::Find
. CPAN has several options. Here's one.Also see here:
SO answer providing CPAN
alternatives to
File::Find
.SO question on directory iterators.
And here is a rewrite of your recursive solution. Things to note:
use strict
;use warnings
; and the use of a scoping block to create a static variable for the subroutine.我认为您的代码中的以下行有问题
您将 $path 变量更改为 $rootpath。
它将正确存储路径。
I think you have problem in the following line in your code
You change the $path variable into $rootpath.
It will store the path correctly.
我使用这个脚本从我的 USB Pendrive 中删除隐藏文件(由 Mac OS X 创建),我通常用它在车里听音乐,以及任何以“.mp3”结尾的文件,即使它以“._”开头”,将被列入汽车音响清单。
I use this script to remove hidden files (created by Mac OS X) from my USB Pendrive, where I usually use it to listen music in the car, and any file ending with ".mp3", even when it starts with "._", will be listed in the car audio list.
您可以使用此方法作为分隔特定文件类型的递归文件搜索,
you can use this method as recursive file search that separate specific file types,