blib/lib/Lazy/Utils.pm

filter: Statementstmt 14.3% 32 / 223 Branchbran 0.0% 0 / 108 Conditioncond 0.0% 0 / 42 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 33.3% 10 / 30 Podpod 68.4% 13 / 19 totaltotal 12.3% 55 / 447 CC 73 SCAR 53.7
CC
73
Cov
9%
file_cache
506.0
cmdargs
380.0
get_pod_text
210.0
CRAP 215.1
↑ Summary
1 package Lazy::Utils;
2 =head1 NAME
3  
4 Lazy::Utils - Utility functions
5  
6 =head1 VERSION
7  
8 version 1.21
9  
10 =head1 SYNOPSIS
11  
12 use Lazy::Utils;
13
14 trim($str);
15 ltrim($str);
16 rtrim($str);
17 file_get_contents($path, $prefs);
18 file_put_contents($path, $contents, $prefs);
19 shellmeta($s, $nonquoted);
20 system2($cmd, @argv);
21 bash_readline($prompt);
22 cmdargs($prefs, @argv);
23 whereis($name, $path);
24 file_cache($tag, $expiry, $coderef);
25 get_pod_text($file_name, $section, $exclude_section);
26 array_to_hash(@array);
27  
28 =head1 DESCRIPTION
29  
30 Collection of utility functions all of exported by default.
31  
32 =cut
33 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶use v5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use feature qw(switch);
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 ▶no if ($] >= 5.018), 'warnings' => 'experimental';
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶use Pod::Simple::Text;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42  
43 BEGIN
44 {
45 1 ▶ require Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 our $VERSION = '1.21';
47 1 our @ISA = qw(Exporter);
48 1 our @EXPORT = qw(trim ltrim rtrim file_get_contents file_put_contents shellmeta system2 _system
49 bash_readline bashReadLine cmdargs commandArgs cmdArgs whereis whereisBin file_cache fileCache
50 get_pod_text getPodText array_to_hash);
51 1 our @EXPORT_OK = qw();
52 }
53  
54  
55 =head1 FUNCTIONS
56  
57 =head2 trim($str)
58  
59 trims given string
60  
61 $str: I<string will be trimmed>
62  
63 return value: I<trimmed string>
64  
65 =cut
66 sub trim
67 {
68 0 ▶ my ($s) = @_;
Subroutine trim: not called CC=1 SCAR=6.9
Pod documented
69 0 $s =~ s/^\s+|\s+$//g;
70 0 return $s
71 }
72  
73 =head2 ltrim($str)
74  
75 trims left given string
76  
77 $str: I<string will be trimmed>
78  
79 return value: I<trimmed string>
80  
81 =cut
82 sub ltrim
83 {
84 0 ▶ my ($s) = @_;
Subroutine ltrim: not called CC=1 SCAR=6.9
Pod documented
85 0 $s =~ s/^\s+//;
86 0 return $s
87 }
88  
89 =head2 rtrim($str)
90  
91 trims right given string
92  
93 $str: I<string will be trimmed>
94  
95 return value: I<trimmed string>
96  
97 =cut
98 sub rtrim
99 {
100 0 ▶ my ($s) = @_;
Subroutine rtrim: not called CC=1 SCAR=6.9
Pod documented
101 0 $s =~ s/\s+$//;
102 0 return $s
103 }
104  
105 =head2 file_get_contents($path, $prefs)
106  
107 gets all contents of file in string type
108  
109 $path: I<path of file>
110  
111 $prefs: I<preferences in HashRef, by default undef>
112  
113 =over
114  
115 utf8: I<opens file-handle as :utf8 mode, by default 0>
116  
117 =back
118  
119 return value: I<file contents in string type, otherwise undef because of errors>
120  
121 =cut
122 sub file_get_contents
123 {
124 0 ▶ my ($path, $prefs) = @_;
Subroutine file_get_contents: not called CC=4 SCAR=30.0
Pod documented
125 0 ▶ $prefs = {} unless ref($prefs) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $prefs eq "HASH" 0 0 0
126 my $result = do
127 0 {
128 0 local $/ = undef;
129 0 my $mode = "";
130 0 ▶ $mode .= " :utf8" if $prefs->{utf8};
Branch: 1 branch
BranchTrueFalseTotal
if $prefs->{"utf8"} 0 0 0
131 0 ▶ open my $fh, "<$mode", $path or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<$mode", $path or return 0 0 0
132 0 my $result = <$fh>;
133 0 close $fh;
134 0 $result;
135 };
136 0 return $result;
137 }
138  
139 =head2 file_put_contents($path, $contents, $prefs)
140  
141 puts all contents of file in string type
142  
143 $path: I<path of file>
144  
145 $contents: I<file contents in string type>
146  
147 $prefs: I<preferences in HashRef, by default undef>
148  
149 =over
150  
151 utf8: I<opens file-handle as :utf8 mode, by default 0>
152  
153 =back
154  
155 return value: I<success 1, otherwise undef>
156  
157 =cut
158 sub file_put_contents
159 {
160 0 ▶ my ($path, $contents, $prefs) = @_;
Subroutine file_put_contents: not called CC=6 SCAR=37.4
Pod documented
161 0 ▶ return if not defined($contents) or ref($contents);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $contents or ref $contents 0 0 0
Condition0%0/3
not defined $contents or ref $contents
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $contents or ref $contents
not defined $contents ref $contents
Truth table0%0/3
not(A) or B
A= not defined $contentsB= ref $contents
ABresult
1X1
011
000
162 0 ▶ $prefs = {} unless ref($prefs) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $prefs eq "HASH" 0 0 0
163 my $result = do
164 0 {
165 0 local $\ = undef;
166 0 my $mode = "";
167 0 ▶ $mode .= " :utf8" if $prefs->{utf8};
Branch: 1 branch
BranchTrueFalseTotal
if $prefs->{"utf8"} 0 0 0
168 0 ▶ open my $fh, ">$mode", $path or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">$mode", $path or return 0 0 0
169 0 my $result = print $fh $contents;
170 0 close $fh;
171 0 $result;
172 };
173 0 return $result;
174 }
175  
176 =head2 shellmeta($s, $nonquoted)
177  
178 escapes metacharacters of interpolated shell string
179  
180 $s: I<interpolated shell string>
181  
182 $nonquoted: I<also escapes whitespaces and * character for non-quoted interpolated shell string, by default 0>
183  
184 return value: I<escaped string>
185  
186 =cut
187 sub shellmeta
188 {
189 0 ▶ my ($s, $nonquoted) = @_;
Subroutine shellmeta: not called CC=3 SCAR=24.8
Pod documented
190 0 ▶ return unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 0 0
191 0 $s =~ s/(\\|\"|\$)/\\$1/g;
192 0 ▶ $s =~ s/(\s|\*)/\\$1/g if $nonquoted;
Branch: 1 branch
BranchTrueFalseTotal
if $nonquoted 0 0 0
193 0 return $s;
194 }
195  
196 =head2 system2($cmd, @argv)
197  
198 B<_system($cmd, @argv)> I<OBSOLETE>
199  
200 alternative implementation of perls core system subroutine that executes a system command
201  
202 $cmd: I<command>
203  
204 @argv: I<command line arguments>
205  
206 return value: I<exit code of command. -1 if fatal error occurs>
207  
208 returned $!: I<system error message>
209  
210 returned $?: I<return code of wait call like on perls system call>
211  
212 =cut
213 sub system2
214 {
215 0 ▶ my $pid;
Subroutine system2: not called CC=4 SCAR=25.3
Pod documented
216 0 ▶ return -1 unless defined($pid = fork);
Branch: 1 branch
BranchTrueFalseTotal
unless defined($pid = fork) 0 0 0
217 0 ▶ unless ($pid)
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 0 0 0
218 {
219 1 ▶ no warnings FATAL => 'exec';
Subroutine BEGIN: called CC=1 SCAR=0.0
220 0 exec(@_);
221 0 exit 255;
222 }
223 0 ▶ return -1 unless waitpid($pid, 0) > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless waitpid($pid, 0) > 0 0 0 0
224 0 return $? >> 8;
225 }
226 sub _system
227 {
228 0 ▶ return system2(@_);
Subroutine _system: not called CC=1 SCAR=6.9
229 }
230  
231 =head2 bash_readline($prompt)
232  
233 B<bashReadLine($prompt)> I<OBSOLETE>
234  
235 reads a line from STDIN using Bash
236  
237 $prompt: I<prompt, by default ''>
238  
239 return value: I<line>
240  
241 =cut
242 sub bash_readline
243 {
244 0 ▶ my ($prompt) = @_;
Subroutine bash_readline: not called CC=5 SCAR=34.0
Pod documented
245 0 ▶ $prompt = "" unless defined($prompt);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prompt 0 0 0
246 0 my $in = \*STDIN;
247 0 ▶ unless (-t $in)
Branch: 1 branch
BranchTrueFalseTotal
unless (-t $in) 0 0 0
248 {
249 0 my $line = <$in>;
250 0 ▶ chomp $line if defined $line;
Branch: 1 branch
BranchTrueFalseTotal
if defined $line 0 0 0
251 0 return $line;
252 }
253 0 local $/ = "\n";
254 0 my $cmd = '/usr/bin/env bash -c "read -p \"'.shellmeta(shellmeta($prompt)).'\" -r -e && echo -n \"\$REPLY\" 2>/dev/null"';
255 0 $_ = `$cmd`;
256 0 ▶ return (not $?)? $_: undef;
Branch: 1 branch
BranchTrueFalseTotal
!$? ? : 0 0 0
257 }
258 sub bashReadLine
259 {
260 0 ▶ return bash_readline(@_);
Subroutine bashReadLine: not called CC=1 SCAR=6.9
Pod undocumented
261 }
262  
263 =head2 cmdargs([$prefs, ]@argv)
264  
265 B<commandArgs([$prefs, ]@argv)> I<OBSOLETE>
266  
267 B<cmdArgs([$prefs, ]@argv)> I<OBSOLETE>
268  
269 resolves command line arguments
270  
271 $prefs: I<preferences in HashRef, optional>
272  
273 =over
274  
275 valuableArgs: I<accepts option value after option if next argument is not an option, by default 0>
276  
277 noCommand: I<use first parameter instead of command, by default 0>
278  
279 optionAtAll: I<accepts options after command or first parameter otherwise evaluates as parameter, by default 1>
280  
281 =back
282  
283 @argv: I<command line arguments>
284  
285 -a -b=c -d e --f g --h --i=j k l -- m n
286  
287 by default, return value:
288  
289 { -a => '', -b => 'c', -d => '', --f => '', --h => '', --i => 'j', command => 'e', parameters => ['g', 'k', 'l'], late_parameters => ['m', 'n'] }
290  
291 if valuableArgs is on, return value;
292  
293 { -a => '', -b => 'c', -d => 'e', --f => 'g', --h => '', --i => 'j', command => 'k', parameters => ['l'], late_parameters => ['m', 'n'] }
294  
295 if noCommand is on, return value:
296  
297 { -a => '', -b => 'c', -d => '', --f => '', --h => '', --i => 'j', command => undef, parameters => ['e', 'g', 'k', 'l'], late_parameters => ['m', 'n'] }
298  
299 if optionAtAll is off, return value:
300  
301 { -a => '', -b => 'c', -d => '', command => 'e', parameters => ['--f', 'g', '--h', '--i=j', 'k', 'l', '--','m', 'n'], late_parameters => [] }
302  
303 =cut
304 sub cmdargs
305 {
306 0 ▶ my $prefs = {};
Subroutine cmdargs: not called CC=19 SCAR=59.4
Pod documented
307 0 ▶ $prefs = shift if @_ >= 1 and ref($_[0]) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if @_ >= 1 and ref $_[0] eq "HASH" 0 0 0
Condition0%0/3
@_ >= 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@_ >= 1 and ref $_[0] eq "HASH"
@_ >= 1 ref $_[0] eq "HASH"
Truth table0%0/3
A and B
A= @_ >= 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
308 0 my @argv = @_;
309 0 my %result;
310 0 $result{command} = undef;
311 0 $result{parameters} = undef;
312  
313 0 my @parameters;
314 my @late_parameters;
315 0 my $late;
316 0 my $opt;
317 0 ▶ while (@argv)
Branch: 1 branch
BranchTrueFalseTotal
if (@argv) 0 0 0
318 {
319 0 my $argv = shift @argv;
320 0 ▶ next unless defined($argv) and not ref($argv);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $argv and not ref $argv 0 0 0
Condition0%0/3
defined $argv and not ref $argv
!ll&&!rl&&r
000
MC/DC0%0/2
defined $argv and not ref $argv
defined $argv not ref $argv
Truth table0%0/3
A and not(B)
A= defined $argvB= not ref $argv
ABresult
0X0
100
111
321  
322 0 ▶ if (not (not defined($prefs->{optionAtAll}) or $prefs->{optionAtAll}) and @parameters)
Branch: 1 branch
BranchTrueFalseTotal
if (not !defined($prefs->{"optionAtAll"}) || $prefs->{"optionAtAll"} and @parameters) 0 0 0
Condition0%0/6
not !defined($prefs->{"optionAtAll"}) || $prefs->{"optionAtAll"} and @parameters
!ll&&!rl&&r
000
!defined($prefs->{'optionAtAll'}) || $prefs->{'optionAtAll'}
l!l&&r!l&&!r
000
MC/DC0%0/3
not !defined($prefs->{"optionAtAll"}) || $prefs->{"optionAtAll"} and @parameters
!defined($prefs->{'optionAtAll'}) $prefs->{'optionAtAll'} @parameters
Truth table0%0/4
not(not(A) || B) and C
A= !defined($prefs->{'optionAtAll'})B= $prefs->{'optionAtAll'}C= @parameters
ABCresult
1XX0
01X0
0000
0011
323 {
324 0 push @parameters, $argv;
325 0 next;
326 }
327  
328 0 ▶ if ($late)
Branch: 1 branch
BranchTrueFalseTotal
if ($late) 0 0 0
329 {
330 0 push @late_parameters, $argv;
331 0 next;
332 }
333  
334 0 ▶ if (substr($argv, 0, 2) eq '--')
Branch: 1 branch
BranchTrueFalseTotal
if (substr($argv, 0, 2) eq "--") 0 0 0
335 {
336 0 $opt = undef;
337 0 ▶ if (length($argv) == 2)
Branch: 1 branch
BranchTrueFalseTotal
if (length $argv == 2) 0 0 0
338 {
339 0 $late = 1;
340 0 next;
341 }
342 0 my @arg = split('=', $argv, 2);
343 0 $result{$arg[0]} = $arg[1];
344 0 ▶ unless (defined($result{$arg[0]}))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $result{$arg[0]}) 0 0 0
345 {
346 0 $result{$arg[0]} = "";
347 0 $opt = $arg[0];
348 }
349 0 next;
350 }
351  
352 0 ▶ if (substr($argv, 0, 1) eq '-' and length($argv) != 1)
Branch: 1 branch
BranchTrueFalseTotal
if (substr($argv, 0, 1) eq "-" and length $argv != 1) 0 0 0
Condition0%0/3
substr($argv, 0, 1) eq "-" and length $argv != 1
!ll&&!rl&&r
000
MC/DC0%0/2
substr($argv, 0, 1) eq "-" and length $argv != 1
substr($argv, 0, 1) eq "-" length $argv != 1
Truth table0%0/3
A and B
A= substr($argv, 0, 1) eq "-"B= length $argv != 1
ABresult
0X0
100
111
353 {
354 0 $opt = undef;
355 0 my @arg = split('=', $argv, 2);
356 0 $result{$arg[0]} = $arg[1];
357 0 ▶ unless (defined($result{$arg[0]}))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $result{$arg[0]}) 0 0 0
358 {
359 0 $result{$arg[0]} = "";
360 0 $opt = $arg[0];
361 }
362 0 next;
363 }
364  
365 0 ▶ if ($prefs->{valuableArgs} and $opt)
Branch: 1 branch
BranchTrueFalseTotal
if ($prefs->{"valuableArgs"} and $opt) 0 0 0
Condition0%0/3
$prefs->{"valuableArgs"} and $opt
!ll&&!rl&&r
000
MC/DC0%0/2
$prefs->{"valuableArgs"} and $opt
$prefs->{"valuableArgs"} $opt
Truth table0%0/3
A and B
A= $prefs->{"valuableArgs"}B= $opt
ABresult
0X0
100
111
366 {
367 0 $result{$opt} = $argv;
368 0 $opt = undef;
369 0 next;
370 }
371 0 $opt = undef;
372  
373 0 push @parameters, $argv;
374 }
375  
376 0 ▶ $result{command} = shift @parameters unless $prefs->{noCommand};
Branch: 1 branch
BranchTrueFalseTotal
unless $prefs->{"noCommand"} 0 0 0
377 0 $result{parameters} = \@parameters;
378 0 $result{late_parameters} = \@late_parameters;
379  
380 0 return \%result;
381 }
382 sub commandArgs
383 {
384 0 ▶ return cmdargs(@_);
Subroutine commandArgs: not called CC=1 SCAR=6.9
Pod undocumented
385 }
386 sub cmdArgs
387 {
388 0 ▶ return cmdargs(@_);
Subroutine cmdArgs: not called CC=1 SCAR=6.9
Pod undocumented
389 }
390  
391 =head2 whereis($name, $path)
392  
393 B<whereisBin($name, $path)> I<OBSOLETE>
394  
395 searches valid binary in search path
396  
397 $name: I<binary name>
398  
399 $path: I<search path, by default "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin">
400  
401 return value: I<array of binary path founded in search path>
402  
403 =cut
404 sub whereis
405 {
406 0 ▶ my ($name, $path) = @_;
Subroutine whereis: not called CC=3 SCAR=24.8
Pod documented
407 0 ▶ return () unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
408 0 ▶ $path = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" unless $path;
Branch: 1 branch
BranchTrueFalseTotal
unless $path 0 0 0
409 0 return grep(-x $_, map("$_/$name", split(":", $path)));
410 }
411 sub whereisBin
412 {
413 0 ▶ return whereis(@_);
Subroutine whereisBin: not called CC=1 SCAR=6.9
Pod undocumented
414 }
415  
416 =head2 file_cache($tag, $expiry, $coderef)
417  
418 B<fileCache($tag, $expiry, $coderef)> I<OBSOLETE>
419  
420 gets most recent cached value in file cache by given tag and caller function if there is cached value in expiry period. Otherwise tries to get current value using $coderef, puts value in cache and cleanups old cache values.
421  
422 $tag: I<tag for cache>
423  
424 $expiry: I<cache expiry period>
425  
426 =over
427  
428 E<lt>0: I<always gets most recent cached value if there is any cached value. Otherwise tries to get current value using $coderef, puts and cleanups.>
429  
430 =0: I<never gets cached value. Always tries to get current value using $coderef, puts and cleanups.>
431  
432 E<gt>0: I<gets most recent cached value in cache if there is cached value in expiry period. Otherwise tries to get current value using $coderef, puts and cleanups.>
433  
434 =back
435  
436 $coderef: I<code reference to get current value>
437  
438 return value: I<cached or current value, otherwise undef if there isn't cached value and current value doesn't get>
439  
440 =cut
441 sub file_cache
442 {
443 0 ▶ my ($tag, $expiry, $coderef) = @_;
Subroutine file_cache: not called CC=22 SCAR=62.3
Pod documented
444 0 my $result;
445 0 my $now = time();
446 0 my @cleanup;
447 0 my $caller = (caller(1))[3];
448 0 ▶ $caller = (caller(0))[0] unless $caller;
Branch: 1 branch
BranchTrueFalseTotal
unless $caller 0 0 0
449 0 $caller = (caller(0))[3].",$caller";
450 0 my $tag_encoded = "";
451 0 for (0..(bytes::length($tag)-1))
452 {
453 0 my $c = bytes::substr($tag, $_, 1);
454 0 ▶ if ($c =~ /\W/)
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ /\W/) 0 0 0
455 {
456 0 $c = uc(sprintf("%%%x", bytes::ord($c)));
457 }
458 0 $tag_encoded .= $c;
459 }
460 0 my $tmp_base = "/tmp/";
461 0 my $tmp_prefix = $caller;
462 0 $tmp_prefix =~ s/\Q::\E/-/g;
463 0 $tmp_prefix .= ".$tag_encoded,";
464 0 for my $tmp_path (sort {$b cmp $a} glob("${tmp_base}$tmp_prefix*"))
465 {
466 0 ▶ if (my ($epoch, $pid) = $tmp_path =~ /^\Q${tmp_base}$tmp_prefix\E(\d*)\.(\d*)/)
Branch: 1 branch
BranchTrueFalseTotal
if (() = $tmp_path =~ /^\Q$tmp_base$tmp_prefix\E(\d*)\.(\d*)/) 0 0 0
467 {
468 0 ▶ if ($expiry < 0 or ($expiry > 0 and $now-$epoch < $expiry))
Branch: 1 branch
BranchTrueFalseTotal
if ($expiry < 0 or $expiry > 0 and $now - $epoch < $expiry) 0 0 0
Condition0%0/6
$expiry < 0 or $expiry > 0 and $now - $epoch < $expiry
l!l&&r!l&&!r
000
$expiry > 0 and $now - $epoch < $expiry
!ll&&!rl&&r
000
MC/DC0%0/3
$expiry < 0 or $expiry > 0 and $now - $epoch < $expiry
$expiry < 0 $expiry > 0 $now - $epoch < $expiry
Truth table0%0/4
A or B and C
A= $expiry < 0B= $expiry > 0C= $now - $epoch < $expiry
ABCresult
1XX1
0111
00X0
0100
469 {
470 0 ▶ if (not defined($result))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $result) 0 0 0
471 {
472 0 my $tmp;
473 0 $tmp = file_get_contents($tmp_path);
474 0 ▶ if ($tmp)
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp) 0 0 0
475 {
476 0 ▶ if ($tmp =~ /^SCALAR\n(.*)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp =~ /^SCALAR\n(.*)/) { } 0 0 0
477 {
478 0 $result = $1;
479 } else
480 {
481 0 eval { $result = from_json($tmp, {utf8 => 1}) };
482 }
483 }
484 }
485 0 next;
486 }
487 }
488 0 unshift @cleanup, $tmp_path;
489 }
490 0 ▶ if (not defined($result))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $result) 0 0 0
491 {
492 0 ▶ $result = $coderef->() if ref($coderef) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $coderef eq "CODE" 0 0 0
493 0 ▶ if (defined($result))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $result) 0 0 0
494 {
495 0 my $tmp;
496 0 ▶ unless (ref($result))
Branch: 1 branch
BranchTrueFalseTotal
if (ref $result) { } 0 0 0
497 {
498 0 $tmp = "SCALAR\n$result";
499 } else
500 {
501 0 ▶ eval { $tmp = to_json($result, {utf8 => 1, pretty => 1}) } if ref($result) eq "ARRAY" or ref($result) eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
if ref $result eq "ARRAY" or ref $result eq "HASH" 0 0 0
Condition0%0/3
ref $result eq "ARRAY" or ref $result eq "HASH"
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $result eq "ARRAY" or ref $result eq "HASH"
ref $result eq "ARRAY" ref $result eq "HASH"
Truth table0%0/3
A or B
A= ref $result eq "ARRAY"B= ref $result eq "HASH"
ABresult
1X1
011
000
502 }
503 0 ▶ if ($tmp and file_put_contents("${tmp_base}tmp.$tmp_prefix$now.$$", $tmp) and rename("${tmp_base}tmp.$tmp_prefix$now.$$", "${tmp_base}$tmp_prefix$now.$$"))
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp and file_put_contents("${tmp_base}tmp.$tmp_prefix$now.${$}", $tmp) and rename "${tmp_base}tmp.$tmp_prefix$now.${$}", "$tmp_base$tmp_prefix$now.${$}") 0 0 0
Condition0%0/6
$tmp and file_put_contents("${tmp_base}tmp.$tmp_prefix$now.${$}", $tmp) and rename "${tmp_base}tmp.$tmp_prefix$now.${$}", "$tmp_base$tmp_prefix$now.${$}"
!ll&&!rl&&r
000
$tmp and file_put_contents("${tmp_base}tmp.$tmp_prefix$now.${$}", $tmp)
!ll&&!rl&&r
000
MC/DC0%0/3
$tmp and file_put_contents("${tmp_base}tmp.$tmp_prefix$now.${$}", $tmp) and rename "${tmp_base}tmp.$tmp_prefix$now.${$}", "$tmp_base$tmp_prefix$now.${$}"
$tmp file_put_contents("${tmp_base}tmp.$tmp_prefix$now.${$}", $tmp) rename "${tmp_base}tmp.$tmp_prefix$now.${$}", "$tmp_base$tmp_prefix$now.${$}"
Truth table0%0/4
A and B and C
A= $tmpB= file_put_contents("${tmp_base}tmp.$tmp_prefix$now.${$}", $tmp)C= rename "${tmp_base}tmp.$tmp_prefix$now.${$}", "$tmp_base$tmp_prefix$now.${$}"
ABCresult
0XX0
10X0
1100
1111
504 {
505 0 pop @cleanup;
506 0 for (@cleanup)
507 {
508 0 unlink($_);
509 }
510 }
511 }
512 }
513 0 return $result;
514 }
515 sub fileCache
516 {
517 0 ▶ return file_cache(@_);
Subroutine fileCache: not called CC=1 SCAR=6.9
Pod undocumented
518 }
519  
520 =head2 get_pod_text($file_name, $section, $exclude_section)
521  
522 B<getPodText($file_name, $section, $exclude_section)> I<OBSOLETE>
523  
524 gets a text of pod contents in given file
525  
526 $file_name: I<file name of searching pod, by default running file>
527  
528 $section: I<searching head1 section of pod, by default undef gets all of contents>
529  
530 $exclude_section: I<excludes section name, by default undef>
531  
532 return value: I<text of pod in string or array by line, otherwise undef if an error occurs>
533  
534 =cut
535 sub get_pod_text
536 {
537 0 ▶ my ($file_name, $section, $exclude_section) = @_;
Subroutine get_pod_text: not called CC=14 SCAR=53.5
Pod documented
538 0 ▶ $file_name = "$FindBin::Bin/$FindBin::Script" unless $file_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $file_name 0 0 0
539 0 ▶ return unless -e $file_name;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file_name 0 0 0
540 0 my $parser = Pod::Simple::Text->new();
541 0 my $text;
542 0 $parser->output_string(\$text);
543 0 eval { $parser->parse_file($file_name) };
544 0 ▶ return if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
545 0 utf8::decode($text);
546 0 ▶ $section = ltrim($section) if $section;
Branch: 1 branch
BranchTrueFalseTotal
if $section 0 0 0
547 0 my @text = split(/^/m, $text);
548 0 my $result;
549 my @result;
550 0 for my $line (@text)
551 {
552 0 chomp $line;
553 0 ▶ if (defined($section) and not defined($result))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $section and not defined $result) 0 0 0
Condition0%0/3
defined $section and not defined $result
!ll&&!rl&&r
000
MC/DC0%0/2
defined $section and not defined $result
defined $section not defined $result
Truth table0%0/3
A and not(B)
A= defined $sectionB= not defined $result
ABresult
0X0
100
111
554 {
555 0 ▶ if ($line eq $section)
Branch: 1 branch
BranchTrueFalseTotal
if ($line eq $section) 0 0 0
556 {
557 0 ▶ unless ($exclude_section)
Branch: 1 branch
BranchTrueFalseTotal
if ($exclude_section) { } 0 0 0
558 {
559 0 $result = "$line\n";
560 0 push @result, $line;
561 } else
562 {
563 0 $result = "";
564 }
565 }
566 0 next;
567 }
568 0 ▶ last if defined($section) and $line =~ /^\S+/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $section and $line =~ /^\S+/ 0 0 0
Condition0%0/3
defined $section and $line =~ /^\S+/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $section and $line =~ /^\S+/
defined $section $line =~ /^\S+/
Truth table0%0/3
A and B
A= defined $sectionB= $line =~ /^\S+/
ABresult
0X0
100
111
569 0 ▶ $result = "" unless defined($result);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 0 0 0
570 0 $result .= "$line\n";
571 0 push @result, $line;
572 }
573 0 ▶ return @result if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
574 0 return $result;
575 }
576 sub getPodText
577 {
578 0 ▶ return get_pod_text(@_);
Subroutine getPodText: not called CC=1 SCAR=6.9
Pod undocumented
579 }
580  
581 =head2 array_to_hash(@array)
582  
583 returns hash with indexes for given array
584  
585 @array: I<command line arguments>
586  
587 return value: I<Hash or HashRef by B<wantarray>>
588  
589 =cut
590 sub array_to_hash
591 {
592 0 ▶ my %h;
Subroutine array_to_hash: not called CC=2 SCAR=17.9
Pod documented
593 0 my $i = 0;
594 0 %h = map { $i++ => $_ } @_;
595 0 ▶ return \%h unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
596 0 return %h;
597 }
598  
599  
600 1 1;
601 __END__
↑ Summary