如何递归地读出Perl中的目录? [英] How can I recursively read out directories in Perl?

查看:132
本文介绍了如何递归地读出Perl中的目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过Template :: Toolkit在HTML页面中递归地读出目录来打印数据结构。
但我挂在如何保存路径和文件的形式,可以阅读我们的简单。

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.

我的想法开始像这样

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中的目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆