line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
## no critic: Subroutines::ProhibitExplicitReturnUndef |
2
|
|
|
|
|
|
|
package File::MoreUtil; |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY |
5
|
|
|
|
|
|
|
our $DATE = '2021-10-13'; # DATE |
6
|
|
|
|
|
|
|
our $DIST = 'File-MoreUtil'; # DIST |
7
|
|
|
|
|
|
|
our $VERSION = '0.625'; # VERSION |
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
87993
|
use 5.010001; |
|
1
|
|
|
|
|
12
|
|
10
|
1
|
|
|
1
|
|
7
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
33
|
|
11
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
8
|
|
|
1
|
|
|
|
|
28
|
|
12
|
|
|
|
|
|
|
|
13
|
1
|
|
|
1
|
|
5
|
use Cwd (); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
2099
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
require Exporter; |
16
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
17
|
|
|
|
|
|
|
our @EXPORT_OK = qw( |
18
|
|
|
|
|
|
|
file_exists |
19
|
|
|
|
|
|
|
l_abs_path |
20
|
|
|
|
|
|
|
dir_empty |
21
|
|
|
|
|
|
|
dir_not_empty |
22
|
|
|
|
|
|
|
dir_has_entries |
23
|
|
|
|
|
|
|
dir_has_files |
24
|
|
|
|
|
|
|
dir_has_dot_files |
25
|
|
|
|
|
|
|
dir_has_non_dot_files |
26
|
|
|
|
|
|
|
dir_has_subdirs |
27
|
|
|
|
|
|
|
dir_has_non_subdirs |
28
|
|
|
|
|
|
|
dir_has_dot_subdirs |
29
|
|
|
|
|
|
|
dir_has_non_dot_subdirs |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
get_dir_entries |
32
|
|
|
|
|
|
|
get_dir_dot_entries |
33
|
|
|
|
|
|
|
get_dir_subdirs |
34
|
|
|
|
|
|
|
get_dir_non_subdirs |
35
|
|
|
|
|
|
|
get_dir_dot_subdirs |
36
|
|
|
|
|
|
|
get_dir_non_dot_subdirs |
37
|
|
|
|
|
|
|
get_dir_files |
38
|
|
|
|
|
|
|
get_dir_dot_files |
39
|
|
|
|
|
|
|
get_dir_non_dot_files |
40
|
|
|
|
|
|
|
); |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
our %SPEC; |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub file_exists { |
45
|
4
|
|
|
4
|
1
|
2177
|
my $path = shift; |
46
|
|
|
|
|
|
|
|
47
|
4
|
100
|
100
|
|
|
98
|
!(-l $path) && (-e _) || (-l _); |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub l_abs_path { |
51
|
5
|
|
|
5
|
1
|
3851
|
my $path = shift; |
52
|
5
|
100
|
|
|
|
112
|
return Cwd::abs_path($path) unless (-l $path); |
53
|
|
|
|
|
|
|
|
54
|
4
|
|
|
|
|
19
|
$path =~ s!/\z!!; |
55
|
4
|
|
|
|
|
22
|
my ($parent, $leaf); |
56
|
4
|
50
|
|
|
|
26
|
if ($path =~ m!(.+)/(.+)!s) { |
57
|
4
|
|
|
|
|
73
|
$parent = Cwd::abs_path($1); |
58
|
4
|
50
|
|
|
|
14
|
return undef unless defined($path); |
59
|
4
|
|
|
|
|
11
|
$leaf = $2; |
60
|
|
|
|
|
|
|
} else { |
61
|
0
|
|
|
|
|
0
|
$parent = Cwd::getcwd(); |
62
|
0
|
|
|
|
|
0
|
$leaf = $path; |
63
|
|
|
|
|
|
|
} |
64
|
4
|
|
|
|
|
31
|
"$parent/$leaf"; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub dir_empty { |
68
|
8
|
|
|
8
|
1
|
7588
|
my ($dir) = @_; |
69
|
8
|
100
|
|
|
|
174
|
return undef unless (-d $dir); |
70
|
7
|
50
|
|
|
|
249
|
return undef unless opendir my($dh), $dir; |
71
|
7
|
|
|
|
|
133
|
while (defined(my $e = readdir $dh)) { |
72
|
18
|
100
|
100
|
|
|
92
|
next if $e eq '.' || $e eq '..'; |
73
|
6
|
|
|
|
|
103
|
return 0; |
74
|
|
|
|
|
|
|
} |
75
|
1
|
|
|
|
|
38
|
1; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub dir_not_empty { |
79
|
10
|
|
|
10
|
1
|
21
|
my ($dir) = @_; |
80
|
10
|
100
|
|
|
|
141
|
return undef unless (-d $dir); |
81
|
8
|
50
|
|
|
|
214
|
return undef unless opendir my($dh), $dir; |
82
|
8
|
|
|
|
|
121
|
while (defined(my $e = readdir $dh)) { |
83
|
18
|
100
|
100
|
|
|
97
|
next if $e eq '.' || $e eq '..'; |
84
|
6
|
|
|
|
|
99
|
return 1; |
85
|
|
|
|
|
|
|
} |
86
|
2
|
|
|
|
|
32
|
0; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
5
|
|
|
5
|
1
|
22
|
sub dir_has_entries { goto \&dir_not_empty } |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub dir_has_files { |
92
|
10
|
|
|
10
|
1
|
22
|
my ($dir) = @_; |
93
|
10
|
100
|
|
|
|
140
|
return undef unless (-d $dir); |
94
|
9
|
50
|
|
|
|
257
|
return undef unless opendir my($dh), $dir; |
95
|
9
|
|
|
|
|
154
|
while (defined(my $e = readdir $dh)) { |
96
|
22
|
100
|
100
|
|
|
155
|
next if $e eq '.' || $e eq '..'; |
97
|
8
|
100
|
|
|
|
156
|
next unless -f "$dir/$e"; |
98
|
4
|
|
|
|
|
88
|
return 1; |
99
|
|
|
|
|
|
|
} |
100
|
5
|
|
|
|
|
95
|
0; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub dir_has_dot_files { |
104
|
10
|
|
|
10
|
1
|
26
|
my ($dir) = @_; |
105
|
10
|
100
|
|
|
|
142
|
return undef unless (-d $dir); |
106
|
9
|
50
|
|
|
|
220
|
return undef unless opendir my($dh), $dir; |
107
|
9
|
|
|
|
|
136
|
while (defined(my $e = readdir $dh)) { |
108
|
24
|
100
|
100
|
|
|
156
|
next if $e eq '.' || $e eq '..'; |
109
|
8
|
100
|
|
|
|
52
|
next unless $e =~ /\A\./; |
110
|
3
|
100
|
|
|
|
47
|
next unless -f "$dir/$e"; |
111
|
2
|
|
|
|
|
33
|
return 1; |
112
|
|
|
|
|
|
|
} |
113
|
7
|
|
|
|
|
117
|
0; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub dir_has_non_dot_files { |
117
|
10
|
|
|
10
|
1
|
32
|
my ($dir) = @_; |
118
|
10
|
100
|
|
|
|
143
|
return undef unless (-d $dir); |
119
|
9
|
50
|
|
|
|
240
|
return undef unless opendir my($dh), $dir; |
120
|
9
|
|
|
|
|
137
|
while (defined(my $e = readdir $dh)) { |
121
|
24
|
100
|
100
|
|
|
130
|
next if $e eq '.' || $e eq '..'; |
122
|
8
|
100
|
|
|
|
44
|
next if $e =~ /\A\./; |
123
|
5
|
100
|
|
|
|
86
|
next unless -f "$dir/$e"; |
124
|
2
|
|
|
|
|
35
|
return 1; |
125
|
|
|
|
|
|
|
} |
126
|
7
|
|
|
|
|
110
|
0; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub dir_has_subdirs { |
130
|
12
|
|
|
12
|
1
|
28
|
my ($dir) = @_; |
131
|
12
|
100
|
|
|
|
171
|
return undef unless (-d $dir); |
132
|
11
|
50
|
|
|
|
283
|
return undef unless opendir my($dh), $dir; |
133
|
11
|
|
|
|
|
161
|
while (defined(my $e = readdir $dh)) { |
134
|
29
|
100
|
100
|
|
|
146
|
next if $e eq '.' || $e eq '..'; |
135
|
10
|
100
|
|
|
|
163
|
next if -l "$dir/$e"; |
136
|
4
|
100
|
|
|
|
24
|
next unless -d _; |
137
|
2
|
|
|
|
|
32
|
return 1; |
138
|
|
|
|
|
|
|
} |
139
|
9
|
|
|
|
|
159
|
0; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
sub dir_has_non_subdirs { |
143
|
9
|
|
|
9
|
1
|
23
|
my ($dir) = @_; |
144
|
9
|
100
|
|
|
|
128
|
return undef unless (-d $dir); |
145
|
8
|
50
|
|
|
|
202
|
return undef unless opendir my($dh), $dir; |
146
|
8
|
|
|
|
|
120
|
while (defined(my $e = readdir $dh)) { |
147
|
21
|
100
|
100
|
|
|
122
|
next if $e eq '.' || $e eq '..'; |
148
|
7
|
100
|
|
|
|
137
|
return 1 if -l "$dir/$e"; |
149
|
4
|
100
|
|
|
|
46
|
return 1 if !(-d _); |
150
|
|
|
|
|
|
|
} |
151
|
3
|
|
|
|
|
49
|
0; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
sub dir_has_dot_subdirs { |
155
|
6
|
|
|
6
|
1
|
15
|
my ($dir) = @_; |
156
|
6
|
100
|
|
|
|
85
|
return undef unless (-d $dir); |
157
|
5
|
50
|
|
|
|
162
|
return undef unless opendir my($dh), $dir; |
158
|
5
|
|
|
|
|
74
|
while (defined(my $e = readdir $dh)) { |
159
|
13
|
100
|
100
|
|
|
81
|
next if $e eq '.' || $e eq '..'; |
160
|
4
|
100
|
|
|
|
39
|
next unless $e =~ /\A\./; |
161
|
2
|
50
|
|
|
|
51
|
next if -l "$dir/$e"; |
162
|
2
|
100
|
|
|
|
29
|
next unless -d _; |
163
|
1
|
|
|
|
|
19
|
return 1; |
164
|
|
|
|
|
|
|
} |
165
|
4
|
|
|
|
|
75
|
0; |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
sub dir_has_non_dot_subdirs { |
169
|
6
|
|
|
6
|
1
|
16
|
my ($dir) = @_; |
170
|
6
|
100
|
|
|
|
125
|
return undef unless (-d $dir); |
171
|
5
|
50
|
|
|
|
133
|
return undef unless opendir my($dh), $dir; |
172
|
5
|
|
|
|
|
119
|
while (defined(my $e = readdir $dh)) { |
173
|
12
|
100
|
100
|
|
|
96
|
next if $e eq '.' || $e eq '..'; |
174
|
4
|
100
|
|
|
|
28
|
next if $e =~ /\A\./; |
175
|
2
|
50
|
|
|
|
56
|
next if -l "$dir/$e"; |
176
|
2
|
100
|
|
|
|
13
|
next unless -d _; |
177
|
1
|
|
|
|
|
18
|
return 1; |
178
|
|
|
|
|
|
|
} |
179
|
4
|
|
|
|
|
74
|
0; |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
sub get_dir_entries { |
183
|
2
|
|
|
2
|
1
|
3137
|
my ($dir) = @_; |
184
|
2
|
|
100
|
|
|
11
|
$dir //= "."; |
185
|
2
|
50
|
|
|
|
71
|
opendir my($dh), $dir or die "Can't opendir $dir: $!"; |
186
|
2
|
100
|
|
|
|
44
|
my @res = grep { $_ ne '.' && $_ ne '..' } readdir $dh; |
|
9
|
|
|
|
|
41
|
|
187
|
2
|
|
|
|
|
22
|
closedir $dh; # we're so nice |
188
|
2
|
|
|
|
|
28
|
@res; |
189
|
|
|
|
|
|
|
} |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
sub get_dir_dot_entries { |
192
|
1
|
|
|
1
|
1
|
4
|
my ($dir) = @_; |
193
|
1
|
|
50
|
|
|
6
|
$dir //= "."; |
194
|
1
|
50
|
|
|
|
30
|
opendir my($dh), $dir or die "Can't opendir $dir: $!"; |
195
|
1
|
100
|
100
|
|
|
29
|
my @res = grep { $_ ne '.' && $_ ne '..' && /\A\./ } readdir $dh; |
|
6
|
|
|
|
|
37
|
|
196
|
1
|
|
|
|
|
13
|
closedir $dh; # we're so nice |
197
|
1
|
|
|
|
|
14
|
@res; |
198
|
|
|
|
|
|
|
} |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
sub get_dir_files { |
201
|
1
|
|
|
1
|
1
|
3
|
my ($dir) = @_; |
202
|
1
|
|
50
|
|
|
7
|
$dir //= "."; |
203
|
1
|
50
|
|
|
|
28
|
opendir my($dh), $dir or die "Can't opendir $dir: $!"; |
204
|
1
|
100
|
100
|
|
|
23
|
my @res = grep { $_ ne '.' && $_ ne '..' && -f } readdir $dh; |
|
6
|
|
|
|
|
69
|
|
205
|
1
|
|
|
|
|
11
|
closedir $dh; # we're so nice |
206
|
1
|
|
|
|
|
12
|
@res; |
207
|
|
|
|
|
|
|
} |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
sub get_dir_dot_files { |
210
|
1
|
|
|
1
|
1
|
4
|
my ($dir) = @_; |
211
|
1
|
|
50
|
|
|
6
|
$dir //= "."; |
212
|
1
|
50
|
|
|
|
29
|
opendir my($dh), $dir or die "Can't opendir $dir: $!"; |
213
|
1
|
100
|
100
|
|
|
23
|
my @res = grep { $_ ne '.' && $_ ne '..' && /\A\./ && -f } readdir $dh; |
|
6
|
|
100
|
|
|
61
|
|
214
|
1
|
|
|
|
|
11
|
closedir $dh; # we're so nice |
215
|
1
|
|
|
|
|
13
|
@res; |
216
|
|
|
|
|
|
|
} |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
sub get_dir_non_dot_files { |
219
|
1
|
|
|
1
|
1
|
3
|
my ($dir) = @_; |
220
|
1
|
|
50
|
|
|
6
|
$dir //= "."; |
221
|
1
|
50
|
|
|
|
31
|
opendir my($dh), $dir or die "Can't opendir $dir: $!"; |
222
|
1
|
100
|
100
|
|
|
28
|
my @res = grep { $_ ne '.' && $_ ne '..' && !/\A\./ && -f } readdir $dh; |
|
6
|
|
100
|
|
|
62
|
|
223
|
1
|
|
|
|
|
12
|
closedir $dh; # we're so nice |
224
|
1
|
|
|
|
|
14
|
@res; |
225
|
|
|
|
|
|
|
} |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
sub get_dir_subdirs { |
228
|
2
|
|
|
2
|
1
|
5
|
my ($dir) = @_; |
229
|
2
|
|
100
|
|
|
9
|
$dir //= "."; |
230
|
2
|
50
|
|
|
|
56
|
opendir my($dh), $dir or die "Can't opendir $dir: $!"; |
231
|
2
|
100
|
100
|
|
|
41
|
my @res = grep { $_ ne '.' && $_ ne '..' && !(-l) && (-d _) } readdir $dh; |
|
9
|
|
66
|
|
|
107
|
|
232
|
2
|
|
|
|
|
23
|
closedir $dh; # we're so nice |
233
|
2
|
|
|
|
|
27
|
@res; |
234
|
|
|
|
|
|
|
} |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
sub get_dir_non_subdirs { |
237
|
2
|
|
|
2
|
1
|
5
|
my ($dir) = @_; |
238
|
2
|
|
100
|
|
|
8
|
$dir //= "."; |
239
|
2
|
50
|
|
|
|
72
|
opendir my($dh), $dir or die "Can't opendir $dir: $!"; |
240
|
2
|
100
|
66
|
|
|
41
|
my @res = grep { $_ ne '.' && $_ ne '..' && ((-l) || !(-d _)) } readdir $dh; |
|
9
|
|
100
|
|
|
95
|
|
241
|
2
|
|
|
|
|
21
|
closedir $dh; # we're so nice |
242
|
2
|
|
|
|
|
25
|
@res; |
243
|
|
|
|
|
|
|
} |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
sub get_dir_dot_subdirs { |
246
|
1
|
|
|
1
|
1
|
3
|
my ($dir) = @_; |
247
|
1
|
|
50
|
|
|
7
|
$dir //= "."; |
248
|
1
|
50
|
|
|
|
28
|
opendir my($dh), $dir or die "Can't opendir $dir: $!"; |
249
|
1
|
100
|
100
|
|
|
24
|
my @res = grep { $_ ne '.' && $_ ne '..' && /\A\./ && !(-l) && (-d _) } readdir $dh; |
|
6
|
|
100
|
|
|
65
|
|
|
|
|
66
|
|
|
|
|
250
|
1
|
|
|
|
|
12
|
closedir $dh; # we're so nice |
251
|
1
|
|
|
|
|
11
|
@res; |
252
|
|
|
|
|
|
|
} |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
sub get_dir_non_dot_subdirs { |
255
|
1
|
|
|
1
|
1
|
2
|
my ($dir) = @_; |
256
|
1
|
|
50
|
|
|
8
|
$dir //= "."; |
257
|
1
|
50
|
|
|
|
28
|
opendir my($dh), $dir or die "Can't opendir $dir: $!"; |
258
|
1
|
100
|
100
|
|
|
23
|
my @res = grep { $_ ne '.' && $_ ne '..' && !/\A\./ && !(-l) && (-d _) } readdir $dh; |
|
6
|
|
100
|
|
|
64
|
|
|
|
|
66
|
|
|
|
|
259
|
1
|
|
|
|
|
12
|
closedir $dh; # we're so nice |
260
|
1
|
|
|
|
|
10
|
@res; |
261
|
|
|
|
|
|
|
} |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
1; |
264
|
|
|
|
|
|
|
# ABSTRACT: File-related utilities |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
__END__ |