line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Text::FileTree; |
2
|
|
|
|
|
|
|
# Copyright (c) 2012 - Olof Johansson |
3
|
|
|
|
|
|
|
# All rights reserved. |
4
|
|
|
|
|
|
|
# |
5
|
|
|
|
|
|
|
# This program is free software; you can redistribute it and/or |
6
|
|
|
|
|
|
|
# modify it under the same terms as Perl itself. |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = 0.22; |
9
|
|
|
|
|
|
|
|
10
|
2
|
|
|
2
|
|
83254
|
use warnings FATAL => 'all'; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
95
|
|
11
|
2
|
|
|
2
|
|
10
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
56
|
|
12
|
2
|
|
|
2
|
|
8
|
use Carp; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
111
|
|
13
|
2
|
|
|
2
|
|
10
|
use File::Slurp; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
144
|
|
14
|
2
|
|
|
2
|
|
9
|
use File::Spec; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
32
|
|
15
|
2
|
|
|
2
|
|
8
|
use File::Basename; |
|
2
|
|
|
|
|
13
|
|
|
2
|
|
|
|
|
174
|
|
16
|
2
|
|
|
2
|
|
5338
|
use Module::Load; |
|
2
|
|
|
|
|
2441
|
|
|
2
|
|
|
|
|
14
|
|
17
|
|
|
|
|
|
|
|
18
|
2
|
|
|
2
|
|
2530
|
use Data::Dumper; |
|
2
|
|
|
|
|
25806
|
|
|
2
|
|
|
|
|
831
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 NAME |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
Text::FileTree - convert a list of files with full paths to a tree |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head1 DESCRIPTION |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
A file list can be organized in a number of ways. The two that |
27
|
|
|
|
|
|
|
most probably comes to mind is a "state free" way, where each |
28
|
|
|
|
|
|
|
file is listed with full path and doesn't depend on its context. |
29
|
|
|
|
|
|
|
The C command outputs files in this way. |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
There is also the "stateful" way of display file lists, where |
32
|
|
|
|
|
|
|
each file is sorted by its common parents. E.g., instead of |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
foo/bar |
35
|
|
|
|
|
|
|
foo/baz |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
you might have the following structure instead: |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
foo/ |
40
|
|
|
|
|
|
|
bar |
41
|
|
|
|
|
|
|
baz |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
This module does just that, converts a "plain" file listing in to |
44
|
|
|
|
|
|
|
the "stateful", directory sorted, way. |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=head1 CONSTRUCTOR |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
my $ft = Text::FileTree->new( ); |
49
|
|
|
|
|
|
|
my $ft_w32 = Text::FileTree->new( platform => 'Win32' ); |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
Create a FileTree parser object. By default, it assume the files |
52
|
|
|
|
|
|
|
are in the platform native format, but this can be overriden. |
53
|
|
|
|
|
|
|
Internally, L is used, but by specifiying a platform |
54
|
|
|
|
|
|
|
parameter C> is used instead. |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=cut |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub new { |
59
|
2
|
|
|
2
|
0
|
539
|
my $class = shift; |
60
|
2
|
|
|
|
|
16
|
my $self = bless { |
61
|
|
|
|
|
|
|
fs => "File::Spec", |
62
|
|
|
|
|
|
|
@_, |
63
|
|
|
|
|
|
|
data => {}, |
64
|
|
|
|
|
|
|
}, $class; |
65
|
|
|
|
|
|
|
|
66
|
2
|
50
|
|
|
|
15
|
if($self->{platform}) { |
67
|
2
|
|
|
|
|
959
|
require "File/Spec/$self->{platform}.pm"; |
68
|
2
|
|
|
|
|
2612
|
$self->{fs} = "File::Spec::$self->{platform}"; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
2
|
|
|
|
|
9
|
return $self; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head1 METHODS |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head2 parse |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=cut |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub parse { |
81
|
10
|
|
|
10
|
1
|
1344
|
my $self = shift; |
82
|
|
|
|
|
|
|
|
83
|
10
|
|
|
|
|
41
|
for my $str (@_) { |
84
|
22
|
|
|
|
|
145
|
$self->__parse_file($_) for split /\n/, $str; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
10
|
|
|
|
|
122
|
return $self->{data}; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub __parse_file { |
91
|
40
|
|
|
40
|
|
63
|
my $self = shift; |
92
|
40
|
|
|
|
|
52
|
my $file = shift; |
93
|
40
|
|
|
|
|
69
|
my $fs = $self->{fs}; |
94
|
|
|
|
|
|
|
|
95
|
40
|
50
|
|
|
|
166
|
return unless $file =~ /\S/; |
96
|
|
|
|
|
|
|
|
97
|
40
|
|
|
|
|
69
|
my $prnt = $self->{data}; |
98
|
|
|
|
|
|
|
|
99
|
40
|
|
|
|
|
252
|
for($fs->splitdir($file)) { |
100
|
100
|
100
|
|
|
|
647
|
$prnt = $prnt->{$_} = defined $prnt->{$_} ? $prnt->{$_} : {}; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head2 from_file |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
Load the file list from a file. |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=cut |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub from_file { |
111
|
2
|
|
|
2
|
1
|
7
|
my $self = shift; |
112
|
2
|
|
|
|
|
5
|
my $filename = shift; |
113
|
2
|
|
|
|
|
12
|
return $self->parse(read_file($filename, err_mode => 'carp')); |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head2 from_fh |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
Load the file list from a filehandle (or a filename). Examples: |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
open(my $pipe, '-|', 'find', '/'); |
121
|
|
|
|
|
|
|
Text::FileTree::from_fh($pipe); |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=cut |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
sub from_fh { |
126
|
4
|
|
|
4
|
1
|
22
|
my $self = shift; |
127
|
4
|
|
|
|
|
19
|
my $fh = shift; |
128
|
4
|
|
|
|
|
90696
|
return $self->parse(join '', <$fh>) |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=head1 AVAILABILITY AND BUG REPORTING |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
Latest released version is available through CPAN. Latest |
134
|
|
|
|
|
|
|
development version is available on github: |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=over |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=item * L |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=item * L |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=back |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Bugs can be reported on either rt.cpan.org, as an issue on github |
145
|
|
|
|
|
|
|
or as an e-mail to me. The primary issue tracker is github. |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head1 KNOWN BUGS AND LIMITATIONS |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
This module does not separate between a file and a directory. |
150
|
|
|
|
|
|
|
This only affects leaf nodes, as you can deduce that a file with |
151
|
|
|
|
|
|
|
children is indeed a directory. This is unlikely to be fixed, as |
152
|
|
|
|
|
|
|
there is really no way of distinguish them in, say, the output |
153
|
|
|
|
|
|
|
from find. Often, you should be able to determine this by |
154
|
|
|
|
|
|
|
context: e.g. by giving C the C<-type f> flag --- now all |
155
|
|
|
|
|
|
|
leaf nodes are regular files. |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head1 COPYRIGHT |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
Copyright (c) 2012 - Olof Johansson |
160
|
|
|
|
|
|
|
All rights reserved. |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or |
163
|
|
|
|
|
|
|
modify it under the same terms as Perl itself. |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=cut |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
1; |