Elive/Util.pm

filter: Statementstmt 76.0% 92 / 121 Branchbran 59.7% 43 / 72 Conditioncond 31.5% 6 / 19 MC/DCmcdc 25.0% 3 / 12 Subroutinesub 91.3% 21 / 23 Podpod 100% 4 / 4 totaltotal 67.3% 169 / 251 CC 47 SCAR 27.8
CC
47
Cov
67%
prompt
72.0
next_quarter_hour
20.0
_thaw
13.4
CRAP 16.1
« Elive/Entity/ServerParameters.pm ↑ Summary Elive/Entity/ParticipantList.pm »
1 package Elive::Util;
2 37 ▶use warnings; use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 37 ▶use Term::ReadKey;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 37 ▶use Term::ReadLine;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 37 ▶use IO::Interactive;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 37 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 37 ▶use Clone;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 37 ▶use YAML::Syck;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 37 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 37 our $VERSION = '1.36';
13  
14 37 ▶use Elive::Util::Type;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 =head1 NAME
17  
18 Elive::Util - Utility functions for Elive
19  
20 =cut
21  
22 =head1 METHODS
23  
24 =cut
25  
26 =head2 inspect_type
27  
28 $type = Elive::Util::inspect_type('Elive::Entity::Participants');
29 if ($type->is_array) {
30 # ...
31 }
32  
33 Returns an object of type L<Elive::Util::Type>.
34  
35 =cut
36  
37 sub inspect_type {
38 3059 ▶ my $type_union = shift;
Subroutine inspect_type: called CC=1 SCAR=0.0
Pod documented
39  
40 3059 my @types = split(/\|/, $type_union);
41  
42 3059 return Elive::Util::Type->new($types[0])
43 }
44  
45 sub _freeze {
46 4206 ▶ my ($val, $type) = @_;
Subroutine _freeze: called CC=12 SCAR=25.8
47  
48 4206 for ($val) {
49  
50 4206 ▶ if (!defined) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $_) { } 0 4206 4206
51 0 warn "undefined value of type $type\n"
52 }
53 else {
54 4206 $_ = string($_, $type);
55 4206 my $raw_val = $_;
56  
57 4206 ▶ if ($type =~ m{^Bool}ix) {
Branch: 4 branches
BranchTrueFalseTotal
if ($type =~ /^Bool/xi) { } 213 3993 4206
elsif ($type =~ /^(Str|enum)/xi) { } 2361 1632 3993
elsif ($type =~ /^(Int|HiResDate)/xi) { } 1495 137 1632
elsif ($type =~ /^Ref|Any/xi) { } 0 137 137
58  
59 #
60 # DBize boolean flags..
61 #
62 213 ▶ $_ = $_ ? 'true' : 'false';
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 170 43 213
63 }
64 elsif ($type =~ m{^(Str|enum)}ix) {
65  
66 #
67 # low level check for taintness. Only applicible when
68 # perl program is running in taint mode
69 #
70 2361 ▶ die "attempt to freeze tainted data (type $type): $_"
Branch: 1 branch
BranchTrueFalseTotal
if _tainted($_) 0 2361 2361
71 if _tainted($_);
72 #
73 # l-r trim
74 #
75 2361 ▶ $_ = $1
Branch: 1 branch
BranchTrueFalseTotal
if /^ \s* (.*?) \s* $/x 2361 0 2361
76 if m{^ \s* (.*?) \s* $}x;
77 2361 ▶ $_ = lc if $type =~ m{^enum};
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /^enum/ 24 2337 2361
78 }
79 elsif ($type =~ m{^(Int|HiResDate)}ix) {
80 1495 $_ = _tidy_decimal("$_");
81 }
82 elsif ($type =~ m{^Ref|Any}ix) {
83 0 $_ = undef;
84 }
85 else {
86 137 ▶ die "unable to convert $raw_val to $type\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 137 137
87 unless defined;
88 }
89 }
90 };
91  
92 4206 return $val;
93 }
94  
95 #
96 # thawing of elementry datatypes
97 #
98  
99 sub _thaw {
100 479 ▶ my ($val, $type) = @_;
Subroutine _thaw: called CC=12 SCAR=25.9
101  
102 479 ▶ return $val if $type =~ m{Ref}i
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /Ref/i or ref $val 5 474 479
Condition100%3/3
$type =~ /Ref/i or ref $val
l!l&&r!l&&!r
32474
MC/DC100%2/2
$type =~ /Ref/i or ref $val
$type =~ /Ref/i ref $val
Truth table100%3/3
A or B
A= $type =~ /Ref/iB= ref $val
ABresult
1X1
011
000
103 || ref( $val);
104  
105 474 ▶ return unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 474 474
106  
107 474 for ($val) {
108  
109 474 ▶ if ($type =~ m{^Bool}i) {
Branch: 4 branches
BranchTrueFalseTotal
if ($type =~ /^Bool/i) { } 39 435 474
elsif ($type =~ /^(Str|enum)/i) { } 337 98 435
elsif ($type =~ /^Int|HiResDate/i) { } 98 0 98
elsif ($type eq 'Any') { } 0 0 0
110 #
111 # Perlise boolean flags..
112 #
113 39 ▶ $_ = m{^(true|1)$}i ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
/^(true|1)$/i ? : 34 5 39
114 }
115 elsif ($type =~ m{^(Str|enum)}i) {
116 #
117 # l-r trim
118 #
119 337 ▶ $_ = $1
Branch: 1 branch
BranchTrueFalseTotal
if /^ \s* (.*?) \s* $/x 337 0 337
120 if m{^ \s* (.*?) \s* $}x;
121 337 ▶ $_ = lc if $type =~ m{^enum}i;
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /^enum/i 11 326 337
122 }
123 elsif ($type =~ m{^Int|HiResDate}i) {
124  
125 98 $_ = _tidy_decimal("$_");
126  
127 }
128 elsif ($type eq 'Any') {
129 # more or less a placeholder type
130 0 $_ = string($_);
131 }
132 else {
133 0 die "unknown type: $type";
134 }
135 };
136  
137 474 return $val;
138 }
139  
140 #
141 # _tidy_decimal(): general cleanup and normalisation of an integer.
142 # used to clean up numbers for data storage or comparison
143  
144 sub _tidy_decimal {
145 1593 ▶ my ($i) = @_;
Subroutine _tidy_decimal: called CC=4 SCAR=13.9
146 #
147 # well a number really. don't convert or sprintf etc
148 # to avoid overflow. Just normalise it for potential
149 # string comparisons
150 #
151 # l-r trim, also untaint
152 #
153 1593 ▶ if ($i =~ m{^ [\s\+]* (-?\d+) \s* $}x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i =~ /^ [\s\+]* (-?\d+) \s* $/x) { } 1592 1 1593
154 1592 $i = $1;
155 }
156 else {
157 1 return;
158 }
159  
160 #
161 # remove any leading zeros:
162 # 000123 => 123
163 # -00045 => -45
164 # -000 => 0
165 #
166  
167 1592 $i =~ s{^
168 (-?) # leading minus retained (for now)
169 0* # leading zeros discarded
170 (\d+?) # number - retained
171 $}
172 {$1$2}x;
173  
174 #
175 # reduce -0 => 0
176 1592 ▶ $i = 0 if ($i eq '-0');
Branch: 1 branch
BranchTrueFalseTotal
if $i eq "-0" 2 1590 1592
177  
178 #
179 # sanity check.
180 #
181 1592 ▶ die "bad integer: $_[0]"
Branch: 1 branch
BranchTrueFalseTotal
unless $i =~ /^[+-]?\d+$/ 0 1592 1592
182 unless $i =~ m{^[+-]?\d+$};
183  
184 1592 return $i;
185 }
186  
187 =head2 prompt
188  
189 my $password = Elive::Util::prompt('Password: ', password => 1)
190  
191 Prompt for user input
192  
193 =cut
194  
195 sub prompt {
196 0 ▶ my ($prompt,%opt) = @_;
Subroutine prompt: not called CC=8 SCAR=42.8
Pod documented
197  
198 0 ▶ chomp($prompt ||= 'input:');
Condition0%0/2
$prompt ||= "input:"
l!l
00
MC/DC0%0/1
$prompt ||= "input:"
$prompt
Truth table0%0/2
A
A= $prompt
Aresult
11
00
199  
200 0 ▶ ReadMode $opt{password}? 2: 1; # Turn off controls keys
Branch: 1 branch
BranchTrueFalseTotal
$opt{'password'} ? : 0 0 0
201  
202 0 my $input;
203 0 my $n = 0;
204  
205 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
defined $input and length $input or do { do { die "giving up on input of $prompt" if ++$n > 100; print $prompt if IO::Interactive::is_interactive(); $input = ReadLine(0); return unless defined $input; chomp $input } } 0 0 0
Condition0%0/3
defined $input and length $input
!ll&&!rl&&r
000
MC/DC0%0/2
defined $input and length $input
defined $input length $input
Truth table0%0/3
A and B
A= defined $inputB= length $input
ABresult
0X0
100
111
206 0 ▶ die "giving up on input of $prompt" if ++$n > 100;
Branch: 1 branch
BranchTrueFalseTotal
if ++$n > 100 0 0 0
207 0 ▶ print $prompt if IO::Interactive::is_interactive();
Branch: 1 branch
BranchTrueFalseTotal
if IO::Interactive::is_interactive() 0 0 0
208 0 $input = ReadLine(0);
209 return
210 0 ▶ unless (defined $input);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 0 0 0
211 0 chomp($input);
212 } until (defined($input) && length($input));
213  
214 0 ReadMode 0; # Reset tty mode before exiting
215  
216 0 return $input;
217 }
218  
219 sub _reftype {
220 10073 ▶ return Scalar::Util::reftype( shift() ) || '';
Subroutine _reftype: called CC=2 SCAR=6.9
Condition100%2/2
builtin::reftype(shift()) || ''
l!l
39186155
MC/DC100%1/1
builtin::reftype(shift()) || ''
builtin::reftype(shift())
Truth table100%2/2
A
A= builtin::reftype(shift())
Aresult
11
00
221 }
222  
223 sub _clone {
224 536 ▶ return Clone::clone(shift);
Subroutine _clone: called CC=1 SCAR=0.0
225 }
226  
227 sub _tainted {
228 8044 ▶ return grep { Scalar::Util::tainted($_) } @_;
Subroutine _tainted: called CC=1 SCAR=0.0
229 }
230  
231 #
232 # Hex encoding/decoding. Use for data streaming. E.g. upload & download
233 # of preload data.
234 #
235  
236 sub _hex_decode {
237 2 ▶ my $data = shift;
Subroutine _hex_decode: called CC=4 SCAR=14.2
238  
239 return
240 2 ▶ unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 2 2
241  
242 2 ▶ $data = '0'.$data
Branch: 1 branch
BranchTrueFalseTotal
unless length($data) % 2 == 0 0 2 2
243 unless length($data) % 2 == 0;
244  
245 2 my ($non_hex_char) = ($data =~ m{([^0-9a-f])}ix);
246  
247 2 ▶ die "non hex character in data: ".$non_hex_char
Branch: 1 branch
BranchTrueFalseTotal
if defined $non_hex_char 0 2 2
248 if (defined $non_hex_char);
249 #
250 # Works for simple ascii
251 2 $data =~ s{(..)}{chr(hex($1))}gex;
252  
253 2 return $data;
254 }
255  
256 sub _hex_encode {
257 2 ▶ my $data = shift;
Subroutine _hex_encode: called CC=1 SCAR=0.0
258  
259 2 $data =~ s{(.)}{sprintf("%02x", ord($1))}gesx;
260  
261 2 return $data;
262 }
263  
264 =head2 string
265  
266 print Elive::Util::string($myscalar);
267 print Elive::Util::string($myobj);
268 print Elive::Util::string($myref, $datatype);
269  
270 Return a string for an object. This method is widely used for casting
271 objects to ids.
272  
273 =over 4
274  
275 =item
276  
277 If it's a simple scalar, just pass the value back.
278  
279 =item
280  
281 If it's an object use the C<stringify> method.
282  
283 =item
284  
285 If it's a reference, resolve datatype to a class, and use its
286 C<stringify> method.
287  
288 =back
289  
290 =cut
291  
292 sub string {
293 5099 ▶ my $obj = shift;
Subroutine string: called CC=8 SCAR=23.4
Pod documented
294 5099 my $data_type = shift;
295  
296 5099 for ($obj) {
297  
298 5099 ▶ if ($data_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data_type) 4740 359 5099
299 4740 my ($dt) = ($data_type =~ m{(.*)});
300  
301 return $dt->stringify($_)
302 4740 ▶ if try {$dt->can('stringify')};
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &try(sub { $dt->can("stringify"); } ) 427 4313 4740
303 }
304  
305 4672 my $reftype = _reftype($_);
306  
307 4672 ▶ return $_
Branch: 1 branch
BranchTrueFalseTotal
unless $reftype 4656 16 4672
308 unless $reftype;
309  
310 16 ▶ return $_->stringify
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($_) and $_->can("stringify") 16 0 16
Condition33%1/3
builtin::blessed($_) and $_->can("stringify")
!ll&&!rl&&r
0016
MC/DC0%0/2
builtin::blessed($_) and $_->can("stringify")
builtin::blessed($_) $_->can("stringify")
Truth table33%1/3
A and B
A= builtin::blessed($_)B= $_->can("stringify")
ABresult
0X0
100
111
311 if (Scalar::Util::blessed($_) && $_->can('stringify'));
312  
313 0 ▶ if ($reftype eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($reftype eq "ARRAY") 0 0 0
314 0 return join(',', sort map {string($_ => $data_type)} @$_)
315 }
316 }
317  
318 #
319 # Nothing else worked; dump it.
320 #
321 0 return YAML::Syck::Dump($obj);
322 }
323  
324 =head2 next_quarter_hour
325  
326 Quarter hour advancement for the Time Module impoverished.
327  
328 my $start = Elive::Util::next_quarter_hour();
329 my $end = Elive::Util::next_quarter_hour($start);
330  
331 Advance to the next quarter hour without the use of any supporting
332 time modules. We just simply increment in seconds until C<localtime>
333 indicates that we're exactly on a quarter hour and ahead of the start time.
334  
335 A small initial increment is added to ensure that the date remains
336 in the future, allowing for minor gotchas such as leap seconds, general
337 latency and smallish time drifts between the client and server.
338  
339 =cut
340  
341 sub next_quarter_hour {
342 0 ▶ my $time = shift || time();
Subroutine next_quarter_hour: not called CC=4 SCAR=30.0
Pod documented
Condition0%0/3
shift() || time
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || time
shift() time
Truth table0%0/3
A || B
A= shift()B= time
ABresult
1X1
011
000
343  
344 0 $time += 30;
345  
346 0 for (;;) {
347 0 my @t = localtime(++$time);
348 0 my $sec = $t[0];
349 0 my $min = $t[1];
350  
351 0 ▶ last unless $min % 15 || $sec;
Branch: 1 branch
BranchTrueFalseTotal
unless $min % 15 or $sec 0 0 0
Condition0%0/3
$min % 15 or $sec
l!l&&r!l&&!r
000
MC/DC0%0/2
$min % 15 or $sec
$min % 15 $sec
Truth table0%0/3
A or B
A= $min % 15B= $sec
ABresult
1X1
011
000
352 }
353  
354 0 return $time;
355 }
356  
357 37 1;
« Elive/Entity/ServerParameters.pm ↑ Summary Elive/Entity/ParticipantList.pm »