Flex/Utils.pm

filter: Statementstmt 96.0% 146 / 152 Branchbran 70.0% 21 / 30 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 38 / 38 Podpod 62.5% 5 / 8 totaltotal 90.1% 210 / 233 CC 20 SCAR 13.1
CC
20
Cov
90%
can_meth
10.0
resolve_meth
7.2
load_module
5.6
CRAP 3.7
« Flex/Chunk.pm ↑ Summary Flex/Role/Wrap/Return.pm »
1 package Iterator::Flex::Utils;
2  
3 # ABSTRACT: Internal utilities
4  
5 71 ▶use v5.28;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 71 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 71 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 71 ▶use experimental 'signatures', 'postderef';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 71 our $VERSION = '0.34';
12  
13 71 ▶use Scalar::Util qw( refaddr );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 71 ▶use Ref::Util qw( is_hashref );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 71 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 71 ▶use experimental 'declared_refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 71 ▶use Module::Runtime;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 71 our %REGISTRY;
20  
21 426 ▶sub mk_indices {
Subroutine mk_indices: called CC=1 SCAR=0.0
Pod undocumented
22 426 my $idx = 0;
23 426 return { map { $_ => $idx++ } @_ };
24 }
25  
26 355 ▶sub mk_lc {
Subroutine mk_lc: called CC=1 SCAR=0.0
Pod undocumented
27 355 return { map { $_ => lc $_ } @_ };
28 }
29  
30 71 ▶use constant ITER_ATTRS => qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
31 CLASS CURRENT FREEZE METHODS NEXT PREV RESET REWIND STATE _DEPENDS _NAME _ROLES _SELF
32 71 );
33 71 ▶use constant mk_lc ITER_ATTRS;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 71 ▶use constant REGISTRY_ITERATION_INDICES => map { 'REG_ITER_' . $_ } ITER_ATTRS, 'MAY_METHOD';
Subroutine BEGIN: called CC=1 SCAR=0.0
35 71 ▶use constant mk_indices REGISTRY_ITERATION_INDICES;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 71 our \%RegIterationIndexMap = mk_indices map { lc } ITER_ATTRS;
37  
38 71 ▶use constant EXHAUSTED_METHODS => qw( IS_EXHAUSTED SET_EXHAUSTED );
Subroutine BEGIN: called CC=1 SCAR=0.0
39 71 ▶use constant mk_lc EXHAUSTED_METHODS;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 71 ▶use constant ITER_STATES => qw( IterState_CLEAR IterState_EXHAUSTED IterState_ERROR );
Subroutine BEGIN: called CC=1 SCAR=0.0
42 71 ▶use constant mk_indices ITER_STATES;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 71 ▶use constant REGISTRY_INDICES => qw( REG_ITERATOR REG_GENERAL REG_METHODS );
Subroutine BEGIN: called CC=1 SCAR=0.0
45 71 ▶use constant mk_indices REGISTRY_INDICES;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 71 ▶use constant EXHAUSTION_ACTIONS => qw[ THROW RETURN PASSTHROUGH ];
Subroutine BEGIN: called CC=1 SCAR=0.0
48 71 ▶use constant mk_lc EXHAUSTION_ACTIONS;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 # these duplicate ITER_ATTRS. combine?
51 71 ▶use constant INTERFACE_PARAMETERS =>
Subroutine BEGIN: called CC=1 SCAR=0.0
52 71 qw( CURRENT FREEZE METHODS NEXT PREV RESET REWIND STATE _DEPENDS _NAME _ROLES _SELF );
53 71 ▶use constant INTERFACE_PARAMETER_VALUES => map { lc $_ } INTERFACE_PARAMETERS;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 71 ▶use constant mk_lc INTERFACE_PARAMETERS;
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56  
57 71 ▶use constant SIGNAL_PARAMETERS => qw( INPUT_EXHAUSTION EXHAUSTION ERROR );
Subroutine BEGIN: called CC=1 SCAR=0.0
58 71 ▶use constant SIGNAL_PARAMETER_VALUES => map { lc $_ } SIGNAL_PARAMETERS;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 71 ▶use constant mk_lc SIGNAL_PARAMETERS;
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 71 ▶use constant GENERAL_PARAMETERS => ( INTERFACE_PARAMETERS, SIGNAL_PARAMETERS );
Subroutine BEGIN: called CC=1 SCAR=0.0
62 71 ▶use constant REGISTRY_GENPAR_INDICES => map { 'REG_GP_' . $_ } GENERAL_PARAMETERS;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 71 ▶use constant mk_indices REGISTRY_GENPAR_INDICES;
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 71 our \%RegGeneralParameterIndexMap = mk_indices map { lc } GENERAL_PARAMETERS;
66  
67 71 our %EXPORT_TAGS = (
68 ExhaustionActions => [EXHAUSTION_ACTIONS],
69 ExhaustedMethods => [EXHAUSTED_METHODS],
70 RegistryIndices => [
71 REGISTRY_INDICES, REGISTRY_ITERATION_INDICES,
72 '%RegIterationIndexMap', REGISTRY_GENPAR_INDICES,
73 '%RegGeneralParameterIndexMap',
74 ],
75 IterAttrs => [ITER_ATTRS],
76 IterStates => [ITER_STATES],
77 SignalParameters => [ SIGNAL_PARAMETERS, 'SIGNAL_PARAMETER_VALUES' ],
78 InterfaceParameters => [ INTERFACE_PARAMETERS, 'INTERFACE_PARAMETER_VALUES' ],
79 GeneralParameters => [ GENERAL_PARAMETERS, ],
80 Functions => [ qw(
81 throw_failure
82 parse_pars
83 can_meth
84 resolve_meth
85 load_role
86 ),
87 ],
88 default => [qw( %REGISTRY refaddr )],
89 );
90  
91 71 $EXPORT_TAGS{':all'} = [ map { $_->@* } values %EXPORT_TAGS ];
92  
93 ## no critic ( AutomaticExportation )
94 71 our @EXPORT = @{ $EXPORT_TAGS{default} }; # ??? is this needed?
95  
96 71 our @EXPORT_OK = ( map { $_->@* } values %EXPORT_TAGS, );
97  
98  
99 26 ▶sub throw_failure ( $failure, $msg ) {
Subroutine throw_failure: called CC=1 SCAR=0.0
Pod undocumented
100 26 require Iterator::Flex::Failure;
101 26 local @Iterator::Flex::Role::Utils::CARP_NOT = scalar caller;
102 26 my $type = join( q{::}, 'Iterator::Flex::Failure', $failure );
103 26 $type->throw( { msg => $msg, trace => Iterator::Flex::Failure->croak_trace } );
104 }
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118 208 ▶sub parse_pars ( @args ) {
Subroutine parse_pars: called CC=4 SCAR=16.1
Pod documented
119  
120 208 my %pars = do {
121  
122 208 ▶ if ( @args == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 1) { } 208 0 208
123 208 ▶ is_hashref( $args[0] )
Branch: 1 branch
BranchTrueFalseTotal
is_hashref($args[0]) or throw_failure("parameter", "expected a hashref") 208 0 208
124 or throw_failure( parameter => 'expected a hashref' );
125 208 $args[0]->%*;
126 }
127  
128 else {
129 0 ▶ @args % 2
Branch: 1 branch
BranchTrueFalseTotal
@args % 2 and throw_failure("parameter", "expected an even number of arguments for hash") 0 0 0
130 and throw_failure( parameter => 'expected an even number of arguments for hash' );
131 0 @args;
132 }
133 };
134  
135 208 my %ipars = delete %pars{ grep exists $pars{$_}, INTERFACE_PARAMETER_VALUES };
136 208 my %spars = delete %pars{ grep exists $pars{$_}, SIGNAL_PARAMETER_VALUES };
137  
138 208 return ( \%pars, \%ipars, \%spars );
139 }
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174 318 ▶sub can_meth ( $obj, @methods ) {
Subroutine can_meth: called CC=10 SCAR=23.0
Pod documented
175  
176 318 ▶ my $par = Ref::Util::is_hashref( $methods[-1] ) ? pop @methods : {};
Branch: 1 branch
BranchTrueFalseTotal
is_hashref($methods[-1]) ? : 3 315 318
177  
178 318 for my $method ( @methods ) {
179 318 ▶ throw_failure( parameter => q{'method' parameters must be a string} )
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_ref($method) 2 316 318
180 if Ref::Util::is_ref( $method );
181  
182 316 my $sub;
183 316 foreach ( "__${method}__", $method ) {
184 383 ▶ if ( defined( $sub = $obj->can( $_ ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($sub = $obj->can($_))) 296 87 383
185 296 ▶ my @ret = ( ( !!$par->{name} ? $_ : () ), ( !!$par->{code} ? $sub : () ) );
Branch: 2 branches
BranchTrueFalseTotal
!!$par->{'name'} ? : 2 294 296
!!$par->{'code'} ? : 2 294 296
186 296 ▶ push @ret, $sub unless @ret;
Branch: 1 branch
BranchTrueFalseTotal
unless @ret 293 3 296
187 296 ▶ return @ret > 1 ? @ret : $ret[0];
Branch: 1 branch
BranchTrueFalseTotal
@ret > 1 ? : 1 295 296
188 }
189 }
190 }
191  
192 20 return undef;
193 }
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212 14 ▶sub resolve_meth ( $target, $method, @fallbacks ) {
Subroutine resolve_meth: called CC=4 SCAR=19.7
Pod documented
213  
214 14 my $code = do {
215  
216 14 ▶ if ( defined $method ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $method) { } 0 14 14
217 0 ▶ Ref::Util::is_coderef( $method )
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_coderef($method) ? : 0 0 0
Condition0%0/3
$target->can($method) // throw_failure("parameter", "method '${method}' is not provided by the object")
l!l&&r!l&&!r
000
MC/DC0%0/2
$target->can($method) // throw_failure("parameter", "method '${method}' is not provided by the object")
$target->can($method) throw_failure("parameter", "method '${method}' is not provided by the object")
Truth table0%0/3
A // B
A= $target->can($method)B= throw_failure("parameter", "method '${method}' is not provided by the object")
ABresult
1X1
011
000
218 ? $method
219 : $target->can( $method )
220 // throw_failure( parameter => qq{method '$method' is not provided by the object} );
221 }
222  
223 else {
224 14 can_meth( $target, @fallbacks );
225 }
226 };
227  
228 14 return $code;
229 }
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246 1824 ▶sub load_module ( $path, @namespaces ) {
Subroutine load_module: called CC=5 SCAR=17.3
Pod documented
247  
248 1824 ▶ if ( substr( $path, 0, 1 ) eq q{+} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($path, 0, 1) eq '+') { } 41 1783 1824
249 41 my $module = substr( $path, 1 );
250 41 ▶ return $module if eval { Module::Runtime::require_module( $module ) };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { &Module::Runtime::require_module($module) } } 41 0 41
251 0 throw_failure( class => "unable to load $module" );
252 }
253 else {
254 1783 for my $namespace ( @namespaces ) {
255 1783 my $module = $namespace . q{::} . $path;
256 1783 ▶ return $module if eval { Module::Runtime::require_module( $module ) };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { &Module::Runtime::require_module($module) } } 1783 0 1783
257 }
258 }
259  
260 0 throw_failure(
261 class => join q{ },
262 0 "unable to find a module for '$path' in @{[ join( ', ', @namespaces ) ]}",
263 );
264 }
265  
266  
267  
268  
269  
270  
271  
272  
273  
274  
275  
276 1824 ▶sub load_role ( $role, @namespaces ) {
Subroutine load_role: called CC=1 SCAR=0.0
Pod documented
277 1824 load_module( $role, @namespaces );
278 }
279  
280  
281 71 1;
282  
283 #
284 # This file is part of Iterator-Flex
285 #
286 # This software is Copyright (c) 2018 by Smithsonian Astrophysical Observatory.
287 #
288 # This is free software, licensed under:
289 #
290 # The GNU General Public License, Version 3, June 2007
291 #
292  
293 __END__
« Flex/Chunk.pm ↑ Summary Flex/Role/Wrap/Return.pm »