Suffit/Util.pm

filter: Statementstmt 35.4% 51 / 144 Branchbran 5.2% 4 / 76 Conditioncond 2.5% 1 / 40 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 48.1% 13 / 27 Podpod 100% 14 / 14 totaltotal 25.7% 83 / 323 CC 56 SCAR 34.4
CC
56
Cov
22%
humanize_duration
72.0
parse_expire
56.0
parse_time_offset
56.0
CRAP 31.1
« Suffit/RSA.pm ↑ Summary Suffit/Cache.pm »
1 package WWW::Suffit::Util;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =encoding utf8
6  
7 =head1 NAME
8  
9 WWW::Suffit::Util - The Suffit utilities
10  
11 =head1 SYNOPSIS
12  
13 use WWW::Suffit::Util;
14  
15 =head1 DESCRIPTION
16  
17 Exported utility functions
18  
19 =head2 color
20  
21 Deprecated in this module! See L<Acrux::Util/color>
22  
23 See also L<Term::ANSIColor>
24  
25 =head2 dformat
26  
27 Deprecated in this module! See L<Acrux::Util/dformat>
28  
29 See also L<CTK::Util/dformat>
30  
31 =head2 fbytes
32  
33 Deprecated in this module! See L<Acrux::Util/fbytes>
34  
35 =head2 fdate
36  
37 Deprecated in this module! See L<Acrux::Util/fdate>
38  
39 =head2 fdatetime
40  
41 Deprecated in this module! See L<Acrux::Util/fdatetime>
42  
43 =head2 fduration
44  
45 Deprecated in this module! See L<Acrux::Util/fduration>
46  
47 =head2 humanize_duration
48  
49 Deprecated in this module! See L<Acrux::Util/humanize_duration>
50  
51 =head2 human2bytes
52  
53 Deprecated in this module! See L<Acrux::Util/human2bytes>
54  
55 See also L<Mojo::Util/humanize_bytes>
56  
57 =head2 json_load
58  
59 my $hash = json_load( $file );
60  
61 Loads JSON file and returns data as perl struct
62  
63 =head2 json_save
64  
65 my $path = json_save( $file, {foo => [1, 2], bar => 'hello!', baz => \1} );
66  
67 Save perl struct to file as JSON document and returns the L<Mojo::File> object
68  
69 =head2 md5sum
70  
71 my $md5 = md5sum( $file );
72  
73 See L<Digest::MD5>
74  
75 =head2 parse_expire
76  
77 Deprecated in this module! See L<Acrux::Util/parse_expire>
78  
79 =head2 parse_time_offset
80  
81 Deprecated in this module! See L<Acrux::Util/parse_time_offset>
82  
83 =head2 randchars
84  
85 Deprecated in this module! See L<Acrux::Util/randchars>
86  
87 =head1 HISTORY
88  
89 See C<Changes> file
90  
91 =head1 TO DO
92  
93 See C<TODO> file
94  
95 =head1 SEE ALSO
96  
97 L<Acrux::Util>, L<Digest::MD5>
98  
99 =head1 AUTHOR
100  
101 Serż Minus (Sergey Lepenkov) L<https://www.serzik.com> E<lt>abalama@cpan.orgE<gt>
102  
103 =head1 COPYRIGHT
104  
105 Copyright (C) 1998-2026 D&D Corporation
106  
107 =head1 LICENSE
108  
109 This program is distributed under the terms of the Artistic License Version 2.0
110  
111 See the C<LICENSE> file or L<https://opensource.org/license/artistic-2-0> for details
112  
113 =cut
114  
115 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
116 1 ▶use Term::ANSIColor qw/colored/;
Subroutine BEGIN: called CC=1 SCAR=0.0
117 1 ▶use POSIX qw/ ceil strftime /;
Subroutine BEGIN: called CC=1 SCAR=0.0
118 1 ▶use Digest::MD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
119  
120 1 ▶use Mojo::Util qw/ trim monkey_patch /;
Subroutine BEGIN: called CC=1 SCAR=0.0
121 1 ▶use Mojo::JSON qw/ decode_json encode_json /;
Subroutine BEGIN: called CC=1 SCAR=0.0
122 1 ▶use Mojo::File qw/ path /;
Subroutine BEGIN: called CC=1 SCAR=0.0
123  
124 1 ▶use WWW::Suffit::Const qw/ IS_TTY DATE_FORMAT DATETIME_FORMAT /;
Subroutine BEGIN: called CC=1 SCAR=0.0
125  
126 1 ▶use base qw/Exporter/;
Subroutine BEGIN: called CC=1 SCAR=0.0
127 1 our @EXPORT = (qw/
128 parse_expire parse_time_offset
129 /);
130 1 our @EXPORT_OK = (qw/
131 fbytes fdate fdatetime fduration human2bytes humanize_duration
132 dformat
133 md5sum
134 json_load json_save
135 randchars
136 color
137 /, @EXPORT);
138  
139 1 ▶use constant HUMAN_SUFFIXES => {
Subroutine BEGIN: called CC=1 SCAR=0.0
140 'B' => 0,
141 'K' => 10, 'KB' => 10, 'KIB' => 10,
142 'M' => 20, 'MB' => 20, 'MIB' => 20,
143 'G' => 30, 'GB' => 30, 'GIB' => 30,
144 'T' => 40, 'TB' => 40, 'TIB' => 40,
145 'P' => 50, 'PB' => 50, 'PIB' => 50,
146 'E' => 60, 'EB' => 60, 'EIB' => 60,
147 'Z' => 70, 'ZB' => 70, 'ZIB' => 70,
148 'Y' => 80, 'YB' => 80, 'YIB' => 80,
149 1 };
150  
151 # Patches
152 0 ▶monkey_patch 'Mojo::File', spew => sub { goto &Mojo::File::spurt } unless Mojo::File->can('spew');
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
unless "Mojo::File"->can("spew") 0 1 1
153  
154 0 ▶sub fbytes {
Subroutine fbytes: not called CC=4 SCAR=30.0
Pod documented
155 0 my $n = int(shift);
156 0 ▶ if ($n >= 1024 ** 3) {
Branch: 3 branches
BranchTrueFalseTotal
if ($n >= 1073741824.0) { } 0 0 0
elsif ($n >= 1048576.0) { } 0 0 0
elsif ($n >= 1024) { } 0 0 0
157 0 return sprintf "%.3g GiB", $n / (1024 ** 3);
158 } elsif ($n >= 1024 ** 2) {
159 0 return sprintf "%.3g MiB", $n / (1024.0 * 1024);
160 } elsif ($n >= 1024) {
161 0 return sprintf "%.3g KiB", $n / 1024.0;
162 } else {
163 0 return "$n B"; # bytes
164 }
165 }
166 0 ▶sub human2bytes {
Subroutine human2bytes: not called CC=6 SCAR=37.4
Pod documented
167 0 ▶ my $h = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
168 0 ▶ return 0 unless $h;
Branch: 1 branch
BranchTrueFalseTotal
unless $h 0 0 0
169 0 my ($bts, $sfx) = $h =~ /([0-9.]+)\s*([a-zA-Z]*)/;
170 0 ▶ return 0 unless $bts;
Branch: 1 branch
BranchTrueFalseTotal
unless $bts 0 0 0
171 0 ▶ my $exp = HUMAN_SUFFIXES->{($sfx ? uc($sfx) : "B")} || 0;
Branch: 1 branch
BranchTrueFalseTotal
$sfx ? : 0 0 0
Condition0%0/2
{B => 0, E => 60, EB => 60, EIB => 60, G => 30, GB => 30, GIB => 30, K => 10, KB => 10, KIB => 10, M => 20, MB => 20, MIB => 20, P => 50, PB => 50, PIB => 50, T => 40, TB => 40, TIB => 40, Y => 80, YB => 80, YIB => 80, Z => 70, ZB => 70, ZIB => 70}->{$sfx ? uc $sfx : 'B'} || 0
l!l
00
MC/DC0%0/1
{B => 0, E => 60, EB => 60, EIB => 60, G => 30, GB => 30, GIB => 30, K => 10, KB => 10, KIB => 10, M => 20, MB => 20, MIB => 20, P => 50, PB => 50, PIB => 50, T => 40, TB => 40, TIB => 40, Y => 80, YB => 80, YIB => 80, Z => 70, ZB => 70, ZIB => 70}->{$sfx ? uc $sfx : 'B'} || 0
{B => 0, E => 60, EB => 60, EIB => 60, G => 30, GB => 30, GIB => 30, K => 10, KB => 10, KIB => 10, M => 20, MB => 20, MIB => 20, P => 50, PB => 50, PIB => 50, T => 40, TB => 40, TIB => 40, Y => 80, YB => 80, YIB => 80, Z => 70, ZB => 70, ZIB => 70}->{$sfx ? uc $sfx : 'B'}
Truth table0%0/2
A
A= {B => 0, E => 60, EB => 60, EIB => 60, G => 30, GB => 30, GIB => 30, K => 10, KB => 10, KIB => 10, M => 20, MB => 20, MIB => 20, P => 50, PB => 50, PIB => 50, T => 40, TB => 40, TIB => 40, Y => 80, YB => 80, YIB => 80, Z => 70, ZB => 70, ZIB => 70}->{$sfx ? uc $sfx : 'B'}
Aresult
11
00
172 0 return ceil($bts * (2 ** $exp));
173 }
174 0 ▶sub humanize_duration {
Subroutine humanize_duration: not called CC=8 SCAR=42.8
Pod documented
175 0 ▶ my $msecs = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
176 0 my $secs = int($msecs);
177 0 my $years = int($secs / (60*60*24*365));
178 0 $secs -= $years * 60*60*24*365;
179 0 my $days = int($secs / (60*60*24));
180 0 $secs -= $days * 60*60*24;
181 0 my $hours = int($secs / (60*60));
182 0 $secs -= $hours * 60*60;
183 0 my $mins = int($secs / 60);
184 0 $secs %= 60;
185 0 ▶ if ($years) { return sprintf("%d years %d days %s hours", $years, $days, $hours) }
Branch: 6 branches
BranchTrueFalseTotal
if ($years) { } 0 0 0
elsif ($days) { } 0 0 0
elsif ($hours) { } 0 0 0
elsif ($mins >= 2) { } 0 0 0
elsif ($secs > 5) { } 0 0 0
elsif ($msecs - $secs) { } 0 0 0
186 0 elsif ($days) { return sprintf("%d days %s hours %d minutes", $days, $hours, $mins) }
187 0 elsif ($hours) { return sprintf("%d hours %d minutes %d seconds", $hours, $mins, $secs) }
188 0 elsif ($mins >= 2) { return sprintf("%d minutes %d seconds", $mins, $secs) }
189 0 elsif ($secs > 5) { return sprintf("%d seconds", $secs + $mins * 60) }
190 0 elsif ($msecs - $secs) { return sprintf("%.4f seconds", $msecs) }
191 0 return sprintf("%d seconds", $secs);
192 }
193 0 ▶sub fduration {
Subroutine fduration: not called CC=5 SCAR=34.0
Pod documented
194 0 ▶ my $msecs = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
195 0 my $secs = int($msecs);
196 0 my $hours = int($secs / (60*60));
197 0 $secs -= $hours * 60*60;
198 0 my $mins = int($secs / 60);
199 0 $secs %= 60;
200 0 ▶ if ($hours) {
Branch: 3 branches
BranchTrueFalseTotal
if ($hours) { } 0 0 0
elsif ($mins >= 2) { } 0 0 0
elsif ($secs < 120) { } 0 0 0
201 0 return sprintf("%d hours %d minutes", $hours, $mins);
202 } elsif ($mins >= 2) {
203 0 return sprintf("%d minutes", $mins);
204 } elsif ($secs < 2*60) {
205 0 return sprintf("%.4f seconds", $msecs);
206 } else {
207 0 $secs += $mins * 60;
208 0 return sprintf("%d seconds", $secs);
209 }
210 }
211 0 ▶sub fdate {
Subroutine fdate: not called CC=2 SCAR=17.9
Pod documented
212 0 ▶ my $t = shift || time;
Condition0%0/2
shift() || time
l!l
00
MC/DC0%0/1
shift() || time
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
213 0 return strftime(DATE_FORMAT, localtime($t));
214 }
215 0 ▶sub fdatetime {
Subroutine fdatetime: not called CC=2 SCAR=17.9
Pod documented
216 0 ▶ my $t = shift || time;
Condition0%0/2
shift() || time
l!l
00
MC/DC0%0/1
shift() || time
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
217 0 return strftime(DATETIME_FORMAT, localtime($t));
218 }
219 0 ▶sub parse_expire {
Subroutine parse_expire: not called CC=7 SCAR=40.3
Pod documented
220 0 ▶ my $t = trim(shift(@_) // 0);
Condition0%0/2
shift @_ // 0
l!l
00
MC/DC0%0/1
shift @_ // 0
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
221 0 my %mult = (
222 's' => 1,
223 'm' => 60,
224 'h' => 60*60,
225 'd' => 60*60*24,
226 'w' => 60*60*24*7,
227 'M' => 60*60*24*30,
228 'y' => 60*60*24*365
229 );
230 0 ▶ if (!$t || (lc($t) eq 'now')) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $t or lc $t eq 'now') { } 0 0 0
elsif ($t =~ /^\d+$/) { } 0 0 0
elsif ($t =~ /^([+-]?(?:\d+|\d*\.\d*))([smhdwMy])/) { } 0 0 0
Condition0%0/3
not $t or lc $t eq "now"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $t or lc $t eq "now"
not $t lc $t eq "now"
Truth table0%0/3
not(A) or B
A= not $tB= lc $t eq "now"
ABresult
1X1
011
000
231 0 return 0;
232 } elsif ($t =~ /^\d+$/) {
233 0 return $t; # secs
234 } elsif ($t=~/^([+-]?(?:\d+|\d*\.\d*))([smhdwMy])/) {
235 0 ▶ return ($mult{$2} || 1) * $1;
Condition0%0/2
$mult{$2} || 1
l!l
00
MC/DC0%0/1
$mult{$2} || 1
$mult{$2}
Truth table0%0/2
A
A= $mult{$2}
Aresult
11
00
236 }
237 0 return $t;
238 }
239 0 ▶sub parse_time_offset {
Subroutine parse_time_offset: not called CC=7 SCAR=40.3
Pod documented
240 0 ▶ my $s = trim(shift(@_) // 0);
Condition0%0/2
shift @_ // 0
l!l
00
MC/DC0%0/1
shift @_ // 0
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
241 0 ▶ return $s if $s =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $s =~ /^\d+$/ 0 0 0
242 0 my $r = 0;
243 0 my $c = 0;
244 0 ▶ while ($s =~ s/([+-]?(?:\d+|\d*\.\d*)[smhdMy])//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ s/([+-]?(?:\d+|\d*\.\d*)[smhdMy])//) 0 0 0
245 0 my $i = parse_expire("$1");
246 0 ▶ $c++ if $i < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $i < 0 0 0 0
247 0 ▶ $r += $i < 0 ? $i*-1 : $i;
Branch: 1 branch
BranchTrueFalseTotal
$i < 0 ? : 0 0 0
248 }
249 0 ▶ return $c ? $r*-1 : $r;
Branch: 1 branch
BranchTrueFalseTotal
$c ? : 0 0 0
250 }
251 1 ▶sub md5sum {
Subroutine md5sum: called CC=5 SCAR=17.2
Pod documented
252 1 my $f = shift;
253 1 my $md5 = Digest::MD5->new;
254 1 my $sum = '';
255 1 ▶ return $sum unless -e $f;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $f 0 1 1
256 1 ▶ open( my $md5_fh, '<', $f) or (carp("Can't open '$f': $!") && return $sum);
Branch: 1 branch
BranchTrueFalseTotal
open my $md5_fh, "<", $f or carp("Can't open '${f}': $!") and return $sum 1 0 1
Condition25%1/4
open(my $md5_fh, '<', $f) || carp("Can't open '${f}': $!") && (return $sum)
l!l
10
carp("Can't open '${f}': $!") and return $sum
!ll
00
MC/DC0%0/2
open(my $md5_fh, '<', $f) || carp("Can't open '${f}': $!") && (return $sum)
open(my $md5_fh, '<', $f) carp("Can't open '${f}': $!")
Truth table33%1/3
A || B
A= open(my $md5_fh, '<', $f)B= carp("Can't open '${f}': $!")
ABresult
1X1
011
000
257 1 ▶ if ($md5_fh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($md5_fh) 1 0 1
258 1 binmode($md5_fh);
259 1 $md5->addfile($md5_fh);
260 1 $sum = $md5->hexdigest;
261 1 close($md5_fh);
262 }
263 1 return $sum;
264 }
265 0 ▶sub json_save {
Subroutine json_save: not called CC=5 SCAR=34.0
Pod documented
266 0 ▶ my $file = shift // '';
Condition0%0/2
shift() // ""
l!l
00
MC/DC0%0/1
shift() // ""
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
267 0 my $data = shift;
268 0 ▶ croak("No file specified") unless length $file;
Branch: 1 branch
BranchTrueFalseTotal
unless length $file 0 0 0
269 0 ▶ croak("No data (perl struct) specified") unless ref $data eq 'ARRAY' || ref $data eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "ARRAY" or ref $data eq "HASH" 0 0 0
Condition0%0/3
ref $data eq "ARRAY" or ref $data eq "HASH"
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $data eq "ARRAY" or ref $data eq "HASH"
ref $data eq "ARRAY" ref $data eq "HASH"
Truth table0%0/3
A or B
A= ref $data eq "ARRAY"B= ref $data eq "HASH"
ABresult
1X1
011
000
270  
271 # my $bytes = encode_json {foo => [1, 2], bar => 'hello!', baz => \1};
272 0 my $path = path($file)->spew( encode_json($data) ); # spurt
273 0 return $path;
274 }
275 0 ▶sub json_load {
Subroutine json_load: not called CC=4 SCAR=30.0
Pod documented
276 0 ▶ my $file = shift // '';
Condition0%0/2
shift() // ""
l!l
00
MC/DC0%0/1
shift() // ""
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
277 0 ▶ croak("No file specified") unless length $file;
Branch: 1 branch
BranchTrueFalseTotal
unless length $file 0 0 0
278 0 ▶ unless (-e $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file) 0 0 0
279 0 carp("JSON file not found: $file");
280 0 return undef;
281 }
282  
283 # my $hash = decode_json $bytes;
284 0 my $data = decode_json( path($file)->slurp );
285 0 return $data;
286 }
287 0 ▶sub dformat {
Subroutine dformat: not called CC=4 SCAR=30.0
Pod documented
288 0 my $f = shift;
289 0 ▶ my $d = @_ ? @_ > 1 ? {@_} : {%{$_[0]}} : {};
Branch: 2 branches
BranchTrueFalseTotal
@_ ? : 0 0 0
@_ > 1 ? : 0 0 0
290 0 ▶ $f =~ s/\[([A-Z0-9_\-.]+?)\]/(defined($d->{$1}) ? $d->{$1} : "[$1]")/eg;
Branch: 1 branch
BranchTrueFalseTotal
defined $d->{$1} ? : 0 0 0
291 0 return $f;
292 }
293 0 ▶sub randchars {
Subroutine randchars: not called CC=6 SCAR=37.4
Pod documented
294 0 ▶ my $l = shift || return '';
Condition0%0/2
shift() || (return '')
l!l
00
MC/DC0%0/1
shift() || (return '')
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
295 0 ▶ return '' unless $l =~/^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $l =~ /^\d+$/ 0 0 0
296 0 my $arr = shift;
297 0 my $r = '';
298 0 ▶ my @chars = ($arr && ref($arr) eq 'ARRAY') ? (@$arr) : (0..9,'a'..'z','A'..'Z');
Branch: 1 branch
BranchTrueFalseTotal
$arr && ref $arr eq 'ARRAY' ? : 0 0 0
Condition0%0/3
$arr and ref $arr eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$arr and ref $arr eq "ARRAY"
$arr ref $arr eq "ARRAY"
Truth table0%0/3
A and B
A= $arrB= ref $arr eq "ARRAY"
ABresult
0X0
100
111
299 0 $r .= $chars[(int(rand($#chars+1)))] for (1..$l);
300 0 return $r;
301 }
302  
303 # Colored helper function
304 0 ▶sub color {
Subroutine color: not called CC=4 SCAR=30.0
Pod documented
305 0 my $clr = shift;
306 0 ▶ my $txt = (scalar(@_) == 1) ? shift(@_) : sprintf(shift(@_), @_);
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ == 1 ? : 0 0 0
307 0 ▶ return $txt unless defined($clr) && length($clr);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $clr and length $clr 0 0 0
Condition0%0/3
defined $clr and length $clr
!ll&&!rl&&r
000
MC/DC0%0/2
defined $clr and length $clr
defined $clr length $clr
Truth table0%0/3
A and B
A= defined $clrB= length $clr
ABresult
0X0
100
111
308 0 return IS_TTY ? colored([$clr], $txt) : $txt;
309 }
310  
311 1 1;
312  
313 __END__
« Suffit/RSA.pm ↑ Summary Suffit/Cache.pm »