blib/lib/Getopt/Module.pm

filter: Statementstmt 94.7% 72 / 76 Branchbran 73.5% 25 / 34 Conditioncond 62.5% 5 / 8 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 100% 11 / 11 Podpod 100% 1 / 1 totaltotal 85.9% 116 / 135 CC 21 SCAR 22.8
CC
21
Cov
86%
__ANON__
16.5
GetModule
5.2
_isa
2.1
CRAP 9.8
↑ Summary
1 package Getopt::Module;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use vars qw($VERSION @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # XXX this declaration must be on a single line
13 # https://metacpan.org/pod/version#How-to-declare()-a-dotted-decimal-version
14 1 ▶use version; our $VERSION = version->declare('v1.0.0');
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 @EXPORT_OK = qw(GetModule);
17  
18 1 my $MODULE_RE = qr{
19 ^ # match the beginning of the string
20 (-)? # optional: leading hyphen: use 'no' instead of 'use'
21 (\w+(?:::\w+)*) # required: Module::Name
22 (?:(=|\s+) (.+))? # optional: args prefixed by '=' e.g. 'Module=arg1,arg2' or \s+ e.g. 'Module qw(foo bar)'
23 $ # match the end of the string
24 }x;
25  
26 # return true if $ref ISA $class - works with non-references, unblessed references and objects
27 230 ▶sub _isa($$) {
Subroutine _isa: called CC=2 SCAR=7.2
28 230 my ($ref, $class) = @_;
29 230 ▶ return Scalar::Util::blessed(ref) ? $ref->isa($class) : ref($ref) eq $class;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed(ref $_) ? : 0 230 230
30 }
31  
32 # dump value like Data::Dump/Data::Dumper::Concise
33 1096 ▶sub _pp($) {
Subroutine _pp: called CC=1 SCAR=0.0
34 1096 my $value = shift;
35 1096 require Data::Dumper;
36 1096 local $Data::Dumper::Deepcopy = 1;
37 1096 local $Data::Dumper::Indent = 0;
38 1096 local $Data::Dumper::Purity = 0;
39 1096 local $Data::Dumper::Terse = 1;
40 1096 local $Data::Dumper::Useqq = 1;
41 1096 return Data::Dumper::Dumper($value);
42 }
43  
44 72 ▶sub GetModule($@) {
Subroutine GetModule: called CC=5 SCAR=16.4
Pod documented
45 72 my $target = shift;
46 72 my $params;
47  
48 72 ▶ if (@_ == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1) { } 72 0 72
elsif (@_ % 2 == 0) { } 0 0 0
49 72 $params = shift;
50  
51 72 ▶ unless (_isa($params, 'HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&_isa($params, "HASH")) 0 72 72
52 0 confess 'invalid parameter; expected HASH or HASHREF, got ', _pp(ref($params));
53 }
54 } elsif ((@_ % 2) == 0) {
55 0 $params = { @_ };
56 } else {
57 0 confess 'invalid parameters; expected hash or hashref, got odd number of arguments > 1';
58 }
59  
60 72 my $no_import = $params->{no_import};
61 72 ▶ my $separator = defined($params->{separator}) ? $params->{separator} : ' ';
Branch: 1 branch
BranchTrueFalseTotal
defined $params->{'separator'} ? : 2 70 72
62  
63 72 ▶ return sub {
Subroutine __ANON__: called CC=16 SCAR=28.1
64 72 my $name = shift;
65 72 my $value = shift;
66  
67 72 ▶ confess 'invalid option definition: option must target a scalar ("foo=s") or array ("foo=@")'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and @_ == 0 0 72 72
Condition33%1/3
defined $value and @_ == 0
!ll&&!rl&&r
0072
MC/DC0%0/2
defined $value and @_ == 0
defined $value @_ == 0
Truth table33%1/3
A and B
A= defined $valueB= @_ == 0
ABresult
0X0
100
111
68 unless (defined($value) && (@_ == 0));
69 72 ▶ confess sprintf('invalid value for %s option: %s', $name, _pp($value))
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /$MODULE_RE/x 0 72 72
70 unless ($value =~ $MODULE_RE);
71  
72 72 my ($hyphen, $module, $args_start, $args) = ($1, $2, $3, $4);
73 72 my ($statement, $method, $eval);
74  
75 72 ▶ if ($hyphen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hyphen) { } 31 41 72
76 31 $statement = 'no';
77 31 $method = 'unimport';
78 } else {
79 41 $statement = 'use';
80 41 $method = 'import';
81 }
82  
83 72 ▶ if ($args_start) { # this takes precedence over no_import - see perlrun
Branch: 2 branches
BranchTrueFalseTotal
if ($args_start) { } 31 41 72
elsif ($no_import) { } 11 30 41
84 31 ▶ $args = '' unless (defined $args);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args 0 31 31
85  
86 31 ▶ if ($args_start eq '=') {
Branch: 1 branch
BranchTrueFalseTotal
if ($args_start eq '=') { } 23 8 31
87 23 $eval = "$statement $module split(/,/,q\0$args\0);"; # see perl.c
88 } else { # space: arbitrary expression
89 8 $eval = "$statement $module $args;";
90 }
91 } elsif ($no_import) {
92 11 $eval = "$statement $module ();";
93 } else {
94 30 $eval = "$statement $module;";
95 }
96  
97 72 my $parsed = {
98 args => $args,
99 eval => $eval,
100 method => $method,
101 module => $module,
102 name => $name,
103 statement => $statement,
104 value => $value,
105 };
106  
107 72 ▶ if (_isa($target, 'ARRAY')) {
Branch: 4 branches
BranchTrueFalseTotal
if (&_isa($target, 'ARRAY')) { } 24 48 72
elsif (&_isa($target, 'SCALAR')) { } 20 28 48
elsif (&_isa($target, 'HASH')) { } 18 10 28
elsif (&_isa($target, 'CODE')) { } 10 0 10
108 24 push @$target, $eval;
109 } elsif (_isa($target, 'SCALAR')) { # SCALAR ref
110 20 ▶ if (defined($$target) && length($$target)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$target and length $$target) { } 4 16 20
Condition66%2/3
defined $$target and length $$target
!ll&&!rl&&r
1604
MC/DC50%1/2
defined $$target and length $$target
defined $$target length $$target
Truth table66%2/3
A and B
A= defined $$targetB= length $$target
ABresult
0X0
100
111
111 4 $$target .= "$separator$eval";
112 } else {
113 16 $$target = $eval;
114 }
115 } elsif (_isa($target, 'HASH')) {
116 18 ▶ $target->{$module} ||= [];
Condition100%2/2
$target->{$module} ||= []
l!l
315
MC/DC100%1/1
$target->{$module} ||= []
$target->{$module}
Truth table100%2/2
A
A= $target->{$module}
Aresult
11
00
117 18 push @{ $target->{$module} }, $eval;
118 } elsif (_isa($target, 'CODE')) {
119 10 $target->($name, $eval, $parsed);
120 } else {
121 0 confess 'invalid target type - expected array ref, code ref, hash ref or scalar ref, got: ', ref($target);
122 }
123  
124 72 return $parsed; # ignored by Getopt::Long, but useful for testing
125 72 };
126 }
127  
128 1 1;
↑ Summary