line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::WRT::Util; |
2
|
|
|
|
|
|
|
|
3
|
10
|
|
|
10
|
|
620
|
use strict; |
|
10
|
|
|
|
|
15
|
|
|
10
|
|
|
|
|
275
|
|
4
|
10
|
|
|
10
|
|
44
|
use warnings; |
|
10
|
|
|
|
|
15
|
|
|
10
|
|
|
|
|
247
|
|
5
|
10
|
|
|
10
|
|
949
|
use open qw(:std :utf8); |
|
10
|
|
|
|
|
2247
|
|
|
10
|
|
|
|
|
64
|
|
6
|
|
|
|
|
|
|
|
7
|
10
|
|
|
10
|
|
1881
|
use Carp; |
|
10
|
|
|
|
|
18
|
|
|
10
|
|
|
|
|
520
|
|
8
|
10
|
|
|
10
|
|
611
|
use Encode; |
|
10
|
|
|
|
|
10513
|
|
|
10
|
|
|
|
|
792
|
|
9
|
|
|
|
|
|
|
|
10
|
10
|
|
|
10
|
|
58
|
use base qw(Exporter); |
|
10
|
|
|
|
|
23
|
|
|
10
|
|
|
|
|
5562
|
|
11
|
|
|
|
|
|
|
our @EXPORT_OK = qw(dir_list file_put_contents file_get_contents); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=over |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=item dir_list($dir, $sort_order, $pattern) |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
Return a $sort_order sorted list of files matching regex $pattern in a |
18
|
|
|
|
|
|
|
directory. |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
Calls $sort_order, which can be one of: |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
alpha - alphabetical |
23
|
|
|
|
|
|
|
reverse_alpha - alphabetical, reversed |
24
|
|
|
|
|
|
|
high_to_low - numeric, high to low |
25
|
|
|
|
|
|
|
low_to_high - numeric, low to high |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=cut |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub dir_list { |
30
|
1
|
|
|
1
|
1
|
3
|
my ($dir, $sort_order, $pattern) = @_; |
31
|
|
|
|
|
|
|
|
32
|
1
|
|
33
|
|
|
4
|
$pattern //= qr/^[0-9]{1,2}$/; |
33
|
1
|
|
50
|
|
|
3
|
$sort_order //= 'high_to_low'; |
34
|
|
|
|
|
|
|
|
35
|
1
|
50
|
|
|
|
50
|
opendir my $list_dir, $dir |
36
|
|
|
|
|
|
|
or croak "Couldn't open $dir: $!"; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
my @files = sort $sort_order |
39
|
1
|
|
|
|
|
41
|
grep { m/$pattern/ } |
|
7
|
|
|
|
|
47
|
|
40
|
|
|
|
|
|
|
readdir $list_dir; |
41
|
|
|
|
|
|
|
|
42
|
1
|
|
|
|
|
20
|
closedir $list_dir; |
43
|
|
|
|
|
|
|
|
44
|
1
|
|
|
|
|
10
|
return @files; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
# Various named sorts for dir_list: |
48
|
0
|
|
|
0
|
0
|
0
|
sub alpha { $a cmp $b; } # alphabetical |
49
|
0
|
|
|
0
|
0
|
0
|
sub high_to_low { $b <=> $a; } # numeric, high to low |
50
|
0
|
|
|
0
|
0
|
0
|
sub low_to_high { $a <=> $b; } # numberic, low to high |
51
|
0
|
|
|
0
|
0
|
0
|
sub reverse_alpha { $b cmp $a; } # alphabetical, reversed |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=item file_put_contents($file, $contents) |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
Write $contents string to $file path. Because: |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
L |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=cut |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub file_put_contents { |
62
|
0
|
|
|
0
|
1
|
0
|
my ($file, $contents) = @_; |
63
|
0
|
0
|
|
|
|
0
|
open(my $fh, '>', $file) |
64
|
|
|
|
|
|
|
or croak "Unable to open $file for writing: $!"; |
65
|
0
|
|
|
|
|
0
|
print $fh $contents; |
66
|
0
|
|
|
|
|
0
|
close $fh; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=item file_get_contents($file) |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
Get contents string of $file path. Because: |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
L |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=cut |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub file_get_contents { |
78
|
225
|
|
|
225
|
1
|
1220
|
my ($file) = @_; |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
# Make warnings here fatal, and return some useful info about which file is |
81
|
|
|
|
|
|
|
# being opened: |
82
|
|
|
|
|
|
|
local $SIG{__WARN__} = sub { |
83
|
0
|
|
|
0
|
|
0
|
croak "$_[0] when opening $file\n"; |
84
|
225
|
|
|
|
|
1420
|
}; |
85
|
|
|
|
|
|
|
|
86
|
225
|
50
|
|
|
|
8636
|
open my $fh, '<', $file |
87
|
|
|
|
|
|
|
or croak "Couldn't open $file: $!\n"; |
88
|
|
|
|
|
|
|
|
89
|
225
|
|
|
|
|
652
|
my $contents; |
90
|
|
|
|
|
|
|
{ |
91
|
|
|
|
|
|
|
# line separator: |
92
|
225
|
|
|
|
|
304
|
local $/ = undef; |
|
225
|
|
|
|
|
880
|
|
93
|
225
|
|
|
|
|
5869
|
$contents = <$fh>; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
225
|
50
|
|
|
|
2249
|
close $fh or croak "Couldn't close $file: $!"; |
97
|
|
|
|
|
|
|
|
98
|
225
|
|
|
|
|
2481
|
return $contents; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=back |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
1; |