blib/lib/Lazy/Utils.pm

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