| 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
| |||||||||||||||||||||||||||||
| 24 | 7 | ▶ | die "array form or module/class name must have 1 or 2 elements" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@$module_with_optional_args == 1 or @$module_with_optional_args == 2
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
| |||||||||||||||||||||||||||||
| 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] || []
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]
| |||||||||||||||||||||||||||||
| 28 | 6 | ▶ | $args = [%$args] if ref $args eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 6 | ▶ | die "In array form of module/class name, the 2nd element must be ". | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 46 | 20 | my $module = shift; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 20 | ▶ | my $do_load = defined $opts->{load} ? $opts->{load} : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 20 | ▶ | unless ($do_load) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 2 | ▶ | if ($opts->{ns_prefix}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | $module = $opts->{ns_prefix} . ($opts->{ns_prefix} =~ /::\z/ ? '' : '::') . $module; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | } elsif ($opts->{ns_prefixes} && @{ $opts->{ns_prefixes} }) { | ||||||||||||||||||||||||||
|
Condition33%1/3
$opts->{"ns_prefixes"} and @{$opts->{"ns_prefixes"};}
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"};}
| |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | $module = $opts->{ns_prefixes}[0] . ($opts->{ns_prefixes}[0] =~ /::\z/ ? '' : '::') . $module; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | 2 | return $module; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 1 | ▶ | my @ns_prefixes = $opts->{ns_prefixes} ? @{$opts->{ns_prefixes}} : | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 18 | ▶ | defined($opts->{ns_prefix}) ? ($opts->{ns_prefix}) : (''); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 18 | ▶ | my $try_all = $opts->{ns_prefixes} ? 1:0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 66 | 3 | ▶ | $module_with_prefix = | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 74 | 2 | ▶ | eval { require $module_with_prefix_pm }; last unless $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 1 | ▶ | warn $@ if $@ !~ /\ACan't locate/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | } else { | ||||||||||||||||||||||||||||
| 77 | 17 | require $module_with_prefix_pm; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | 14 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 103 | 14 | ▶ | if ($do_import) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 9 | eval "package $target_package; $module->import(\@{\$args});"; ## no critic: BuiltinFunctions::ProhibitStringyEval | |||||||||||||||||||||||||||
| 105 | 9 | ▶ | die if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 123 | 4 | ▶ | if ($do_construct) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | my $constructor = defined $opts->{constructor} ? | ||||||||||||||||||||||||||||
| 125 | 3 | ▶ | $opts->{constructor} : 'new'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||