点击(此处)折叠或打开
- #!/usr/bin/perl
- use strict;
- use warnings;
- use Data::Dumper;
- use File::Basename;
- use File::Spec::Functions;
- my $data = data_for_path_deep('/home/MyProject/PerlProject');
- sub data_for_path_deep{
- my ($path) = @_;
- print "-------------------------",$path,"";
- my $data = {};
- my @queue = ([$path, $data]);
-
- while(my $next = shift @queue){
- my ($path, $ref) = @$next;
-
- my $basename = basename($path);
- $ref->{$basename} = do{
- if(-f $path or -l $path){undef}
- else{
- my $hash = {};
- opendir my $dir_fh, $path
- or die "can not open dir: $!";
- my @new_paths = map{
- catfile($path, $_)
- }grep{! /^\.\.?\z/}readdir $dir_fh;
- unshift @queue, map{[$_, $hash]} @new_paths;
- $hash;
- }
- }
- }
-
- $data;
- }
- print Dumper $data;