perl - 如何在 Perl 中递归读取目录?

标签 perl recursion treeview directory

我想递归地读出一个目录,以使用 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/

相关文章:

regex - 在 Perl 正则表达式中匹配平衡括号

java - Perl校验和计算迁移到Java

c - 我无法理解函数的递归。怎么运行的?这些值是如何存储的?

c# - 仅在子节点中的复选框?

c# - 我不知道如何将子节点添加到 TreeView

regex - 限制单词长度的 Perl 正则表达式

perl - Perl 中的条件编译

javascript - 提高使用解构和递归处理数组的函数的性能

java - 不带循环的整数数组交集递归

c# - 我可以在 AfterLabelEdit 期间将节点插入到 TreeView 中而不开始编辑它们吗?