line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package File::Object; |
2
|
|
|
|
|
|
|
|
3
|
11
|
|
|
11
|
|
114700
|
use strict; |
|
11
|
|
|
|
|
87
|
|
|
11
|
|
|
|
|
315
|
|
4
|
11
|
|
|
11
|
|
58
|
use warnings; |
|
11
|
|
|
|
|
22
|
|
|
11
|
|
|
|
|
327
|
|
5
|
|
|
|
|
|
|
|
6
|
11
|
|
|
11
|
|
5504
|
use Class::Utils qw(set_params); |
|
11
|
|
|
|
|
114303
|
|
|
11
|
|
|
|
|
228
|
|
7
|
11
|
|
|
11
|
|
812
|
use Error::Pure qw(err); |
|
11
|
|
|
|
|
34
|
|
|
11
|
|
|
|
|
445
|
|
8
|
11
|
|
|
11
|
|
5427
|
use File::Spec::Functions qw(catdir catfile splitdir); |
|
11
|
|
|
|
|
9732
|
|
|
11
|
|
|
|
|
821
|
|
9
|
11
|
|
|
11
|
|
5276
|
use FindBin qw($Bin $Script); |
|
11
|
|
|
|
|
22726
|
|
|
11
|
|
|
|
|
20640
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = 0.17; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
# Constructor. |
14
|
|
|
|
|
|
|
sub new { |
15
|
39
|
|
|
39
|
1
|
20484
|
my ($class, @params) = @_; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# Create object. |
18
|
39
|
|
|
|
|
100
|
my $self = bless {}, $class; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# Directory path. |
21
|
39
|
|
|
|
|
132
|
$self->{'dir'} = []; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# File path. |
24
|
39
|
|
|
|
|
85
|
$self->{'file'} = undef; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# Type of path. |
27
|
39
|
|
|
|
|
78
|
$self->{'type'} = 'dir'; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# Process parameters. |
30
|
39
|
|
|
|
|
153
|
set_params($self, @params); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# Check to right 'type'. |
33
|
37
|
100
|
100
|
|
|
829
|
if (! $self->{'type'} || ($self->{'type'} ne 'file' |
|
|
|
100
|
|
|
|
|
34
|
|
|
|
|
|
|
&& $self->{'type'} ne 'dir')) { |
35
|
|
|
|
|
|
|
|
36
|
2
|
|
|
|
|
9
|
err 'Bad \'type\' parameter.'; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# Check to dir. |
40
|
35
|
100
|
|
|
|
122
|
if (ref $self->{'dir'} ne 'ARRAY') { |
41
|
1
|
|
|
|
|
4
|
err '\'dir\' parameter must be a reference to array.'; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# Check to file. |
45
|
34
|
100
|
100
|
|
|
141
|
if ($self->{'type'} eq 'file' && @{$self->{'dir'}} |
|
15
|
|
100
|
|
|
105
|
|
46
|
|
|
|
|
|
|
&& ! defined $self->{'file'}) { |
47
|
|
|
|
|
|
|
|
48
|
1
|
|
|
|
|
6
|
err 'Bad file constructor with undefined \'file\' parameter.'; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# Remove undef dirs. |
52
|
33
|
100
|
|
|
|
56
|
if (@{$self->{'dir'}}) { |
|
33
|
|
|
|
|
109
|
|
53
|
20
|
100
|
|
|
|
37
|
my @dir = map { defined $_ ? $_ : () } @{$self->{'dir'}}; |
|
34
|
|
|
|
|
148
|
|
|
20
|
|
|
|
|
54
|
|
54
|
20
|
|
|
|
|
60
|
$self->{'dir'} = \@dir; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
# Update path. |
58
|
33
|
|
|
|
|
126
|
$self->_update_path; |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
# Set values. |
61
|
33
|
|
|
|
|
96
|
$self->set; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
# Object. |
64
|
33
|
|
|
|
|
157
|
return $self; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
# Add dir. |
68
|
|
|
|
|
|
|
sub dir { |
69
|
17
|
|
|
17
|
1
|
44
|
my ($self, @dirs) = @_; |
70
|
|
|
|
|
|
|
|
71
|
17
|
|
|
|
|
38
|
foreach my $dir (@dirs) { |
72
|
8
|
100
|
|
|
|
22
|
if (defined $dir) { |
73
|
6
|
|
|
|
|
21
|
$self->_dir($dir); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# Object. |
78
|
17
|
|
|
|
|
29
|
return $self; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
# Add file. |
82
|
|
|
|
|
|
|
sub file { |
83
|
12
|
|
|
12
|
1
|
51
|
my ($self, @file_path) = @_; |
84
|
|
|
|
|
|
|
|
85
|
12
|
|
|
|
|
23
|
my $file = pop @file_path; |
86
|
12
|
|
|
|
|
31
|
$self->dir(@file_path); |
87
|
12
|
|
|
|
|
33
|
$self->_file($file); |
88
|
|
|
|
|
|
|
|
89
|
12
|
|
|
|
|
29
|
return $self; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
# Get dir. |
94
|
|
|
|
|
|
|
sub get_dir { |
95
|
12
|
|
|
12
|
1
|
3002
|
my ($self, $dir_num) = @_; |
96
|
|
|
|
|
|
|
|
97
|
12
|
|
100
|
|
|
63
|
$dir_num ||= 1; |
98
|
12
|
100
|
|
|
|
31
|
if ($self->{'type'} eq 'file') { |
99
|
3
|
|
|
|
|
4
|
$dir_num++; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
12
|
|
|
|
|
79
|
return $self->{'path'}->[-$dir_num]; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# Get file. |
106
|
|
|
|
|
|
|
sub get_file { |
107
|
2
|
|
|
2
|
1
|
9
|
my $self = shift; |
108
|
|
|
|
|
|
|
|
109
|
2
|
100
|
|
|
|
6
|
if ($self->{'type'} eq 'file') { |
110
|
1
|
|
|
|
|
3
|
return $self->{'path'}->[-1]; |
111
|
|
|
|
|
|
|
} else { |
112
|
1
|
|
|
|
|
3
|
return; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
# Reset to constructor values. |
117
|
|
|
|
|
|
|
sub reset { |
118
|
10
|
|
|
10
|
1
|
22
|
my $self = shift; |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
# Reset to setted values. |
121
|
10
|
|
|
|
|
24
|
$self->{'dir'} = $self->{'_set'}->{'dir'}; |
122
|
10
|
|
|
|
|
23
|
$self->{'file'} = $self->{'_set'}->{'file'}; |
123
|
10
|
|
|
|
|
16
|
$self->{'type'} = $self->{'_set'}->{'type'}; |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
# Update path. |
126
|
10
|
|
|
|
|
30
|
$self->_update_path; |
127
|
|
|
|
|
|
|
|
128
|
10
|
|
|
|
|
34
|
return $self; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
# Serialize path. |
132
|
|
|
|
|
|
|
sub s { |
133
|
34
|
|
|
34
|
1
|
123
|
my $self = shift; |
134
|
|
|
|
|
|
|
|
135
|
34
|
100
|
|
|
|
84
|
if ($self->{'type'} eq 'dir') { |
136
|
15
|
|
|
|
|
23
|
return catdir(@{$self->{'path'}}); |
|
15
|
|
|
|
|
171
|
|
137
|
|
|
|
|
|
|
} else { |
138
|
19
|
|
|
|
|
26
|
return catfile(@{$self->{'path'}}); |
|
19
|
|
|
|
|
197
|
|
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
# Set actual values to constructor values. |
143
|
|
|
|
|
|
|
sub set { |
144
|
35
|
|
|
35
|
1
|
57
|
my $self = shift; |
145
|
|
|
|
|
|
|
|
146
|
35
|
|
|
|
|
49
|
my @path = @{$self->{'path'}}; |
|
35
|
|
|
|
|
98
|
|
147
|
35
|
|
|
|
|
98
|
$self->{'_set'}->{'type'} = $self->{'type'}; |
148
|
35
|
100
|
|
|
|
89
|
if ($self->{'type'} eq 'file') { |
149
|
15
|
|
|
|
|
35
|
$self->{'_set'}->{'file'} = pop @path; |
150
|
15
|
|
|
|
|
34
|
$self->{'_set'}->{'dir'} = \@path; |
151
|
|
|
|
|
|
|
} else { |
152
|
20
|
|
|
|
|
41
|
$self->{'_set'}->{'dir'} = \@path; |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
35
|
|
|
|
|
73
|
return $self; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
# Go to parent directory. |
159
|
|
|
|
|
|
|
sub up { |
160
|
9
|
|
|
9
|
1
|
40
|
my ($self, $up_num) = @_; |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
# Check number and positive number. |
163
|
9
|
100
|
100
|
|
|
44
|
if (! $up_num || $up_num !~ m/^\d$/ms) { |
164
|
7
|
|
|
|
|
12
|
$up_num = 1; |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
# Process. |
168
|
9
|
|
|
|
|
31
|
foreach (1 .. $up_num) { |
169
|
10
|
100
|
|
|
|
21
|
if ($self->{'type'} eq 'file') { |
170
|
2
|
100
|
|
|
|
3
|
if (@{$self->{'path'}} > 2) { |
|
2
|
|
|
|
|
6
|
|
171
|
1
|
|
|
|
|
3
|
$self->{'type'} = 'dir'; |
172
|
1
|
|
|
|
|
2
|
$self->{'file'} = undef; |
173
|
1
|
|
|
|
|
2
|
splice @{$self->{'path'}}, -2; |
|
1
|
|
|
|
|
2
|
|
174
|
|
|
|
|
|
|
} else { |
175
|
1
|
|
|
|
|
4
|
err 'Cannot go up.', 'PATH', $self->s; |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
} else { |
178
|
8
|
100
|
|
|
|
13
|
if (@{$self->{'path'}}) { |
|
8
|
|
|
|
|
17
|
|
179
|
7
|
|
|
|
|
10
|
pop @{$self->{'path'}}; |
|
7
|
|
|
|
|
21
|
|
180
|
|
|
|
|
|
|
} else { |
181
|
1
|
|
|
|
|
4
|
err 'Cannot go up.', 'PATH', $self->s; |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
# Object. |
187
|
7
|
|
|
|
|
23
|
return $self; |
188
|
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
# Add dir array. |
191
|
|
|
|
|
|
|
sub _dir { |
192
|
6
|
|
|
6
|
|
18
|
my ($self, @dir) = @_; |
193
|
|
|
|
|
|
|
|
194
|
6
|
100
|
|
|
|
21
|
if ($self->{'type'} eq 'file') { |
195
|
1
|
|
|
|
|
2
|
$self->{'type'} = 'dir'; |
196
|
1
|
|
|
|
|
3
|
$self->{'file'} = undef; |
197
|
1
|
|
|
|
|
1
|
pop @{$self->{'path'}}; |
|
1
|
|
|
|
|
2
|
|
198
|
|
|
|
|
|
|
} |
199
|
6
|
|
|
|
|
12
|
push @{$self->{'path'}}, @dir; |
|
6
|
|
|
|
|
17
|
|
200
|
|
|
|
|
|
|
|
201
|
6
|
|
|
|
|
15
|
return; |
202
|
|
|
|
|
|
|
} |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
# Add file array. |
205
|
|
|
|
|
|
|
sub _file { |
206
|
12
|
|
|
12
|
|
22
|
my ($self, $file) = @_; |
207
|
|
|
|
|
|
|
|
208
|
12
|
100
|
|
|
|
31
|
if (! defined $file) { |
209
|
1
|
|
|
|
|
3
|
return; |
210
|
|
|
|
|
|
|
} |
211
|
11
|
100
|
|
|
|
40
|
if ($self->{'type'} eq 'file') { |
212
|
6
|
|
|
|
|
13
|
pop @{$self->{'path'}}; |
|
6
|
|
|
|
|
12
|
|
213
|
6
|
|
|
|
|
12
|
push @{$self->{'path'}}, $file; |
|
6
|
|
|
|
|
12
|
|
214
|
|
|
|
|
|
|
} else { |
215
|
5
|
|
|
|
|
9
|
push @{$self->{'path'}}, $file; |
|
5
|
|
|
|
|
14
|
|
216
|
5
|
|
|
|
|
10
|
$self->{'type'} = 'file'; |
217
|
|
|
|
|
|
|
} |
218
|
|
|
|
|
|
|
|
219
|
11
|
|
|
|
|
18
|
return; |
220
|
|
|
|
|
|
|
} |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
# Update path. |
223
|
|
|
|
|
|
|
sub _update_path { |
224
|
43
|
|
|
43
|
|
85
|
my $self = shift; |
225
|
|
|
|
|
|
|
|
226
|
43
|
100
|
|
|
|
119
|
if ($self->{'type'} eq 'file') { |
227
|
|
|
|
|
|
|
$self->{'path'} = [ |
228
|
17
|
|
|
|
|
86
|
@{$self->{'dir'}}, |
229
|
17
|
100
|
|
|
|
38
|
defined $self->{'file'} ? $self->{'file'} : (), |
230
|
|
|
|
|
|
|
]; |
231
|
17
|
100
|
|
|
|
28
|
if (! @{$self->{'path'}}) { |
|
17
|
|
|
|
|
56
|
|
232
|
2
|
|
|
|
|
9
|
$self->{'path'} = [splitdir($Bin), $Script]; |
233
|
|
|
|
|
|
|
} |
234
|
|
|
|
|
|
|
} else { |
235
|
26
|
|
|
|
|
53
|
$self->{'path'} = [@{$self->{'dir'}}]; |
|
26
|
|
|
|
|
97
|
|
236
|
26
|
100
|
|
|
|
46
|
if (! @{$self->{'path'}}) { |
|
26
|
|
|
|
|
159
|
|
237
|
10
|
|
|
|
|
49
|
$self->{'path'} = [splitdir($Bin)]; |
238
|
|
|
|
|
|
|
} |
239
|
|
|
|
|
|
|
} |
240
|
|
|
|
|
|
|
|
241
|
43
|
|
|
|
|
235
|
return; |
242
|
|
|
|
|
|
|
} |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
1; |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
__END__ |