blib/lib/Module/Load/Util.pm

filter: Statementstmt 93.2% 96 / 103 Branchbran 75.6% 59 / 78 Conditioncond 50.0% 4 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 9 / 9 Podpod 100% 4 / 4 totaltotal 83.5% 173 / 207 CC 44 SCAR 25.7
CC
44
Cov
84%
_load_module
24.9
_normalize_module_with_optional_args
9.2
load_module_with_optional_args
8.0
CRAP 13.1
↑ Summary
1 package Module::Load::Util;
2  
3 2 ▶use strict 'subs', 'vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Regexp::Pattern::Perl::Module ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
7 2 our $DATE = '2024-05-13'; # DATE
8 2 our $DIST = 'Module-Load-Util'; # DIST
9 2 our $VERSION = '0.012'; # VERSION
10  
11 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 our @EXPORT_OK = qw(
13 load_module_with_optional_args
14 instantiate_class_with_optional_args
15 call_module_function_with_optional_args
16 call_module_method_with_optional_args
17 );
18  
19 22 ▶sub _normalize_module_with_optional_args {
Subroutine _normalize_module_with_optional_args: called CC=9 SCAR=22.2
20 22 my $module_with_optional_args = shift;
21  
22 22 my ($module, $args);
23 22 ▶ if (ref $module_with_optional_args eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $module_with_optional_args eq 'ARRAY') { } 7 15 22
elsif (ref $module_with_optional_args) { } 0 15 15
elsif ($module_with_optional_args =~ /(.+?)[=,](.*)/) { } 10 5 15
24 7 ▶ die "array form or module/class name must have 1 or 2 elements"
Branch: 1 branch
BranchTrueFalseTotal
unless @$module_with_optional_args == 1 or @$module_with_optional_args == 2 1 6 7
Condition66%2/3
@$module_with_optional_args == 1 or @$module_with_optional_args == 2
l!l&&r!l&&!r
061
MC/DC50%1/2
@$module_with_optional_args == 1 or @$module_with_optional_args == 2
@$module_with_optional_args == 1 @$module_with_optional_args == 2
Truth table66%2/3
A or B
A= @$module_with_optional_args == 1B= @$module_with_optional_args == 2
ABresult
1X1
011
000
25 unless @$module_with_optional_args == 1 || @$module_with_optional_args == 2;
26 6 $module = $module_with_optional_args->[0];
27 6 ▶ $args = $module_with_optional_args->[1] || [];
Condition50%1/2
$module_with_optional_args->[1] || []
l!l
60
MC/DC0%0/1
$module_with_optional_args->[1] || []
$module_with_optional_args->[1]
Truth table50%1/2
A
A= $module_with_optional_args->[1]
Aresult
11
00
28 6 ▶ $args = [%$args] if ref $args eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $args eq "HASH" 3 3 6
29 6 ▶ die "In array form of module/class name, the 2nd element must be ".
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args eq "ARRAY" 1 5 6
30 "arrayref or hashref" unless ref $args eq 'ARRAY';
31 } elsif (ref $module_with_optional_args) {
32 0 die "module/class name must be string or 2-element array, not ".
33 $module_with_optional_args;
34 } elsif ($module_with_optional_args =~ /(.+?)[=,](.*)/) {
35 10 $module = $1;
36 10 $args = [split /,/, $2];
37 } else {
38 5 $module = $module_with_optional_args;
39 5 $args = [];
40 }
41 20 ($module, $args);
42 }
43  
44 20 ▶sub _load_module {
Subroutine _load_module: called CC=19 SCAR=32.2
45 20 ▶ my $opts = ref $_[0] eq 'HASH' ? shift : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 20 0 20
46 20 my $module = shift;
47  
48 20 ▶ my $do_load = defined $opts->{load} ? $opts->{load} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $opts->{'load'} ? : 2 18 20
49  
50 20 ▶ unless ($do_load) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($do_load) 2 18 20
51 2 ▶ if ($opts->{ns_prefix}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{'ns_prefix'}) { } 0 2 2
elsif ($opts->{'ns_prefixes'} and @{$opts->{'ns_prefixes'};}) { } 0 4 4
52 0 ▶ $module = $opts->{ns_prefix} . ($opts->{ns_prefix} =~ /::\z/ ? '' : '::') . $module;
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'ns_prefix'} =~ /::\z/ ? : 0 0 0
53 0 ▶ } elsif ($opts->{ns_prefixes} && @{ $opts->{ns_prefixes} }) {
Condition33%1/3
$opts->{"ns_prefixes"} and @{$opts->{"ns_prefixes"};}
!ll&&!rl&&r
200
MC/DC0%0/2
$opts->{"ns_prefixes"} and @{$opts->{"ns_prefixes"};}
$opts->{"ns_prefixes"} @{$opts->{"ns_prefixes"};}
Truth table33%1/3
A and B
A= $opts->{"ns_prefixes"}B= @{$opts->{"ns_prefixes"};}
ABresult
0X0
100
111
54 0 ▶ $module = $opts->{ns_prefixes}[0] . ($opts->{ns_prefixes}[0] =~ /::\z/ ? '' : '::') . $module;
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'ns_prefixes'}[0] =~ /::\z/ ? : 0 0 0
55 }
56 2 return $module;
57 }
58  
59 1 ▶ my @ns_prefixes = $opts->{ns_prefixes} ? @{$opts->{ns_prefixes}} :
Branch: 1 branch
BranchTrueFalseTotal
defined $opts->{'ns_prefix'} ? : 1 16 17
60 18 ▶ defined($opts->{ns_prefix}) ? ($opts->{ns_prefix}) : ('');
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'ns_prefixes'} ? : 1 17 18
61 18 ▶ my $try_all = $opts->{ns_prefixes} ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'ns_prefixes'} ? : 1 17 18
62 18 my $module_with_prefix;
63 18 for my $i (0 .. $#ns_prefixes) {
64 19 my $ns_prefix = $ns_prefixes[$i];
65 19 ▶ if (length $ns_prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $ns_prefix) { } 3 16 19
66 3 ▶ $module_with_prefix =
Branch: 1 branch
BranchTrueFalseTotal
$ns_prefix =~ /::\z/ ? : 0 3 3
67 $ns_prefix . ($ns_prefix =~ /::\z/ ? '':'::') . $module;
68 } else {
69 16 $module_with_prefix = $module;
70 }
71  
72 19 (my $module_with_prefix_pm = "$module_with_prefix.pm") =~ s!::!/!g;
73 19 ▶ if ($try_all) {
Branch: 1 branch
BranchTrueFalseTotal
if ($try_all) { } 2 17 19
74 2 ▶ eval { require $module_with_prefix_pm }; last unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 1 1 2
75 1 ▶ warn $@ if $@ !~ /\ACan't locate/;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ =~ /\ACan't locate/ 0 1 1
76 } else {
77 17 require $module_with_prefix_pm;
78 }
79 }
80 14 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 14 14
81 0 die "load_module_with_optional_args(): Failed to load module '$module' (all prefixes tried: ".join(", ", @ns_prefixes).")";
82 }
83 14 $module_with_prefix;
84 }
85  
86 20 ▶sub load_module_with_optional_args {
Subroutine load_module_with_optional_args: called CC=8 SCAR=20.8
Pod documented
87 20 ▶ my $opts = ref($_[0]) eq 'HASH' ? shift : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 10 10 20
88 20 my $module_with_optional_args = shift;
89  
90 my $target_package =
91 defined $opts->{target_package} ? $opts->{target_package} :
92 defined $opts->{caller} ? $opts->{caller} :
93 20 ▶ caller(0);
Branch: 2 branches
BranchTrueFalseTotal
defined $opts->{'target_package'} ? : 5 15 20
defined $opts->{'caller'} ? : 0 15 15
94 # check because we will use eval ""
95 $target_package =~ $Regexp::Pattern::Perl::Module::RE{perl_modname}{pat}
96 20 ▶ or die "Invalid syntax in target package '$target_package'";
Branch: 1 branch
BranchTrueFalseTotal
$target_package =~ /$Regexp::Pattern::Perl::Module::RE{"perl_modname"}{"pat"}/ or die "Invalid syntax in target package '${target_package}'" 20 0 20
97  
98 20 my ($module, $args) = _normalize_module_with_optional_args(
99 $module_with_optional_args);
100 18 $module = _load_module($opts, $module);
101  
102 14 ▶ my $do_import = defined $opts->{import} ? $opts->{import} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $opts->{'import'} ? : 5 9 14
103 14 ▶ if ($do_import) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_import) 9 5 14
104 9 eval "package $target_package; $module->import(\@{\$args});"; ## no critic: BuiltinFunctions::ProhibitStringyEval
105 9 ▶ die if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 8 9
106 }
107  
108 13 {module=>$module, args=>$args};
109 }
110  
111 4 ▶sub instantiate_class_with_optional_args {
Subroutine instantiate_class_with_optional_args: called CC=5 SCAR=16.1
Pod documented
112 4 ▶ my $opts = ref($_[0]) eq 'HASH' ? {%{shift()}} : {}; # shallow copy
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 3 1 4
113 4 my $class_with_optional_args = shift;
114  
115 4 $opts->{import} = 0;
116 4 $opts->{target_package} = caller(0);
117 4 my $res = load_module_with_optional_args($opts, $class_with_optional_args);
118 #use DD; print "Options: "; dd $opts; print "Result: "; dd $res;
119 4 my $class = $res->{module};
120 4 my $args = $res->{args};
121  
122 4 ▶ my $do_construct = defined $opts->{construct} ? $opts->{construct} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $opts->{'construct'} ? : 1 3 4
123 4 ▶ if ($do_construct) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_construct) { } 3 1 4
124 my $constructor = defined $opts->{constructor} ?
125 3 ▶ $opts->{constructor} : 'new';
Branch: 1 branch
BranchTrueFalseTotal
defined $opts->{'constructor'} ? : 1 2 3
126 3 my $obj = $class->$constructor(@$args);
127 3 return $obj;
128 } else {
129 1 return +{class=>$class, args=>$args};
130 }
131 }
132  
133 1 ▶sub call_module_function_with_optional_args {
Subroutine call_module_function_with_optional_args: called CC=4 SCAR=14.4
Pod documented
134 1 ▶ my $opts = ref($_[0]) eq 'HASH' ? shift : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 1 1
135 1 my $module_with_optional_args = shift;
136  
137 1 my ($module, $args) = _normalize_module_with_optional_args(
138 $module_with_optional_args);
139 1 my $function;
140 1 ▶ if (defined $opts->{function}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts->{'function'}) { } 0 1 1
141 0 $function = $opts->{function};
142 } else {
143 1 ▶ $module =~ s/\A(.+)::(\w+)\z/$1/ or die "Please specify MODULE::FUNCTION, not just module name '$module'";
Branch: 1 branch
BranchTrueFalseTotal
$module =~ s/\A(.+)::(\w+)\z/$1/ or die "Please specify MODULE::FUNCTION, not just module name '${module}'" 1 0 1
144 1 $function = $2;
145 }
146 1 $module = _load_module($opts, $module);
147  
148 1 &{"$module\::$function"}(@$args);
149 }
150  
151 1 ▶sub call_module_method_with_optional_args {
Subroutine call_module_method_with_optional_args: called CC=4 SCAR=14.5
Pod documented
152 1 ▶ my $opts = ref($_[0]) eq 'HASH' ? shift : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 1 1
153 1 my $module_with_optional_args = shift;
154  
155 1 my ($module, $args) = _normalize_module_with_optional_args(
156 $module_with_optional_args);
157 1 my $method;
158 1 ▶ if (defined $opts->{method}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts->{'method'}) { } 0 1 1
159 0 $method = $opts->{method};
160 } else {
161 1 ▶ $module =~ s/\A(.+)::(\w+)\z/$1/ or die "Please specify MODULE::FUNCTION, not just module name '$module'";
Branch: 1 branch
BranchTrueFalseTotal
$module =~ s/\A(.+)::(\w+)\z/$1/ or die "Please specify MODULE::FUNCTION, not just module name '${module}'" 1 0 1
162 1 $method = $2;
163 }
164  
165 1 $module = _load_module($opts, $module);
166  
167 1 $module->$method(@$args);
168 }
169  
170 2 1;
171 # ABSTRACT: Some utility routines related to module loading
172  
173 __END__
↑ Summary