点击(此处)折叠或打开

  1. #!/usr/bin/perl

  2. use strict;
  3. use warnings;

  4. use Data::Dumper;
  5. use File::Basename;
  6. use File::Spec::Functions;


  7. my $data = data_for_path_deep('/home/MyProject/PerlProject');


  8. sub data_for_path_deep{    
  9.     my ($path) = @_;
  10.     print "-------------------------",$path,"";
  11.     my $data = {};
  12.     my @queue = ([$path, $data]);
  13.     
  14.     while(my $next = shift @queue){
  15.         my ($path, $ref) = @$next;
  16.         
  17.         my $basename = basename($path);
  18.         $ref->{$basename} = do{
  19.             if(-f $path or -l $path){undef}
  20.             else{
  21.                 my $hash = {};
  22.                 opendir my $dir_fh, $path
  23.                     or die "can not open dir: $!";
  24.                 my @new_paths = map{
  25.                     catfile($path, $_)
  26.                 }grep{! /^\.\.?\z/}readdir $dir_fh;


  27.                 unshift @queue, map{[$_, $hash]} @new_paths;
  28.                 $hash;
  29.             }
  30.         }
  31.     }
  32.         
  33.     $data;
  34. }

  35. print Dumper $data;

09-10 04:33