Yath/Util.pm

filter: Statementstmt 53.8% 42 / 78 Branchbran 32.1% 9 / 28 Conditioncond 11.1% 1 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 66.6% 8 / 12 Podpod 100% 6 / 6 totaltotal 47.4% 66 / 139 CC 19 SCAR 30.5
CC
19
Cov
45%
fqmod
39.9
decode_json_file
20.0
decode_json
6.0
CRAP 21.1
« Yath/Option.pm ↑ Summary Yath.pm »
1 package Getopt::Yath::Util;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use Carp qw/confess longmess croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Cpanel::JSON::XS();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Importer Importer => 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Temp qw/ tempfile /;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = '2.000007';
11  
12 1 our @EXPORT_OK = qw{
13 decode_json
14 encode_json
15 encode_json_file
16 decode_json_file
17  
18 fqmod
19 mod2file
20 };
21  
22 1 my $json = Cpanel::JSON::XS->new->utf8(1)->convert_blessed(1)->allow_nonref(1);
23 1 my $ascii = Cpanel::JSON::XS->new->ascii(1)->convert_blessed(1)->allow_nonref(1);
24  
25 0 ▶sub decode_json { my $out; eval { $out = $json->decode(@_); 1} // confess($@); $out }
Subroutine decode_json: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/3
eval { do { $out = $json->decode(@_); 1 } } // confess($@)
l!l&&r!l&&!r
000
MC/DC0%0/2
eval { do { $out = $json->decode(@_); 1 } } // confess($@)
eval { do { $out = $json->decode(@_); 1 } } confess($@)
Truth table0%0/3
A // B
A= eval { do { $out = $json->decode(@_); 1 } }B= confess($@)
ABresult
1X1
011
000
26 0 ▶sub encode_json { my $out; eval { $out = $ascii->encode(@_); 1} // confess($@); $out }
Subroutine encode_json: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/3
eval { do { $out = $ascii->encode(@_); 1 } } // confess($@)
l!l&&r!l&&!r
000
MC/DC0%0/2
eval { do { $out = $ascii->encode(@_); 1 } } // confess($@)
eval { do { $out = $ascii->encode(@_); 1 } } confess($@)
Truth table0%0/3
A // B
A= eval { do { $out = $ascii->encode(@_); 1 } }B= confess($@)
ABresult
1X1
011
000
27  
28 sub encode_json_file {
29 0 ▶ my ($data) = @_;
Subroutine encode_json_file: not called CC=1 SCAR=6.9
Pod documented
30 0 my $json = encode_json($data);
31  
32 0 my ($fh, $file) = tempfile("$$-XXXXXX", TMPDIR => 1, SUFFIX => '.json', UNLINK => 0);
33 0 print $fh $json;
34 0 close($fh);
35  
36 0 return $file;
37 }
38  
39 sub decode_json_file {
40 0 ▶ my ($file, %params) = @_;
Subroutine decode_json_file: not called CC=4 SCAR=30.0
Pod documented
41  
42 0 ▶ open(my $fh, '<', $file) or die "Could not open '$file': $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "Could not open '${file}': $!" 0 0 0
43 0 my $json = do { local $/; <$fh> };
44  
45 0 ▶ if ($params{unlink}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"unlink"}) 0 0 0
46 0 ▶ unlink($file) or warn "Could not unlink '$file': $!";
Branch: 1 branch
BranchTrueFalseTotal
unlink $file or warn "Could not unlink '${file}': $!" 0 0 0
47 }
48  
49 0 return decode_json($json);
50 }
51  
52 sub mod2file {
53 16 ▶ my ($mod) = @_;
Subroutine mod2file: called CC=2 SCAR=7.0
Pod documented
54 16 ▶ confess "No module name provided" unless $mod;
Branch: 1 branch
BranchTrueFalseTotal
unless $mod 0 16 16
55 16 my $file = $mod;
56 16 $file =~ s{::}{/}g;
57 16 $file .= ".pm";
58 16 return $file;
59 }
60  
61 sub fqmod {
62 16 ▶ my ($input, $prefixes, %options) = @_;
Subroutine fqmod: called CC=13 SCAR=36.9
Pod documented
63  
64 16 ▶ croak "At least 1 prefix is required" unless $prefixes;
Branch: 1 branch
BranchTrueFalseTotal
unless $prefixes 0 16 16
65  
66 16 ▶ $prefixes = [$prefixes] unless ref($prefixes) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $prefixes eq "ARRAY" 16 0 16
67  
68 16 ▶ croak "At least 1 prefix is required" unless @$prefixes;
Branch: 1 branch
BranchTrueFalseTotal
unless @$prefixes 0 16 16
69 16 ▶ croak "Cannot use no_require when providing multiple prefixes" if $options{no_require} && @$prefixes > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $options{"no_require"} and @$prefixes > 1 0 16 16
Condition33%1/3
$options{"no_require"} and @$prefixes > 1
!ll&&!rl&&r
1600
MC/DC0%0/2
$options{"no_require"} and @$prefixes > 1
$options{"no_require"} @$prefixes > 1
Truth table33%1/3
A and B
A= $options{"no_require"}B= @$prefixes > 1
ABresult
0X0
100
111
70  
71 16 ▶ if ($input =~ m/^\+(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input =~ /^\+(.*)$/) 0 16 16
72 0 my $mod = $1;
73 0 ▶ return $mod if $options{no_require};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"no_require"} 0 0 0
74 0 ▶ return $mod if eval { require(mod2file($mod)); 1 };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { require (mod2file($mod)); 1 } } 0 0 0
75 0 confess($@);
76 }
77  
78 16 my %tried;
79 16 for my $pre (@$prefixes) {
80 16 ▶ my $mod = $input =~ m/^\Q$pre\E/ ? $input : "$pre\::$input";
Branch: 1 branch
BranchTrueFalseTotal
$input =~ /^\Q$pre\E/ ? : 0 16 16
81  
82 16 ▶ if ($options{no_require}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{'no_require'}) { } 0 16 16
83 0 return $mod;
84 }
85 else {
86 16 ▶ return $mod if eval { require(mod2file($mod)); 1 };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { require (mod2file($mod)); 1 } } 16 0 16
87 0 ($tried{$mod}) = split /\n/, $@;
88 0 $tried{$mod} =~ s{^(Can't locate \S+ in \@INC).*$}{$1.};
89 }
90 }
91  
92 0 my @caller = caller;
93  
94 0 die "Could not locate a module matching '$input' at $caller[1] line $caller[2], the following were checked:\n" . join("\n", map { " * $_: $tried{$_}" } sort keys %tried) . "\n";
95 }
96  
97 1 1;
98  
99 __END__
« Yath/Option.pm ↑ Summary Yath.pm »