-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdata_for_path.pl
107 lines (95 loc) · 1.88 KB
/
data_for_path.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use strict;
use warnings;
use utf8;
use Data::Dumper;
sub data_for_path
{
my $path = shift;
if (-f $path or -l $path) # -f 判断file -l 判断link
{
return undef;
}
if (-d $path) # -d 判断文件夹
{
my %directory;
opendir PATH,$path or die "Cannot opendir $path:$!";
my @names = readdir PATH;
closedir PATH;
for my $name (@names)
{
next if $name eq '.' or $name eq '..'; # 排除. 和.. 文件
$directory{$name} = data_for_path("$path/$name");
}
return \%directory;
}
warn "$path is neither a file nor a directory\n";
return undef;
}
# 只需要给定一个目录名称
# 结果是一个引用
print Dumper(data_for_path("BioPerl"));
my %data;
sub dump_data_for_path
{
my $path = shift;
my $data = shift;
if (not defined $data)
{
print "$path\n";
return;
}
foreach (sort keys %{$data})
{
dump_data_for_path("$path/$_",$$data{$_}); # 这个地方是$$
}
}
dump_data_for_path("BioPerl",data_for_path("BioPerl"));
=cut
# another way
sub dump_data_for_path
{
my $path = shift;
my $data = shift;
if (not defined $data)
{
print "$path\n";
return;
}
my %directory = %$data;
foreach (sort keys %directory)
{
dump_data_for_path("$path/$_",$directory($_)); # 这个地方是$$
}
}
# another way Avoiding Recursion
=cut
use File::Basename;
use File::Spec::Functions;
my $data=data_for_path('/Users/Gilligan/Desktop');
sub data_for_path
{
my ($path) = @_;
my $data = {};
my @queue = ([$path,$data]) # queue是一个路径和$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 ($db),$path;
my @new_paths=map
{
catfile($path,$_)
} grep {! /^\.\.?\z/ } readdir $dh;
unshift @queue,map{[$_,$hash]} @new_paths;
$hash;
}
};
}
$data;
}