blib/lib/Iterator/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
« blib/lib/Iterator/Flex/Chunk.pm ↑ Summary blib/lib/Iterator/Flex/Role/Wrap/Return.pm »
1 package Iterator::Flex::Utils;
2  
3 # ABSTRACT: Internal utilities
4  
5 70 ▶use v5.28;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 70 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 70 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 70 ▶use experimental 'signatures', 'postderef';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 70 our $VERSION = '0.33';
12  
13 70 ▶use Scalar::Util qw( refaddr );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 70 ▶use Ref::Util qw( is_hashref );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 70 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 70 ▶use experimental 'declared_refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 70 ▶use Module::Runtime;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 70 our %REGISTRY;
20  
21 sub mk_indices {
22 420 ▶ my $idx = 0;
Subroutine mk_indices: called CC=1 SCAR=0.0
Pod undocumented
23 420 return { map { $_ => $idx++ } @_ };
24 }
25  
26 sub mk_lc {
27 350 ▶ return { map { $_ => lc $_ } @_ };
Subroutine mk_lc: called CC=1 SCAR=0.0
Pod undocumented
28 }
29  
30 70 use constant ITER_ATTRS => qw(
31 CLASS CURRENT FREEZE METHODS NEXT PREV RESET REWIND STATE _DEPENDS _NAME _ROLES _SELF
32 70 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
33 70 ▶use constant mk_lc ITER_ATTRS;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 70 ▶use constant REGISTRY_ITERATION_INDICES => map { 'REG_ITER_' . $_ } ITER_ATTRS, 'MAY_METHOD';
Subroutine BEGIN: called CC=1 SCAR=0.0
35 70 ▶use constant mk_indices REGISTRY_ITERATION_INDICES;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 70 our \%RegIterationIndexMap = mk_indices map { lc } ITER_ATTRS;
37  
38 70 ▶use constant EXHAUSTED_METHODS => qw( IS_EXHAUSTED SET_EXHAUSTED );
Subroutine BEGIN: called CC=1 SCAR=0.0
39 70 ▶use constant mk_lc EXHAUSTED_METHODS;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 70 ▶use constant ITER_STATES => qw( IterState_CLEAR IterState_EXHAUSTED IterState_ERROR );
Subroutine BEGIN: called CC=1 SCAR=0.0
42 70 ▶use constant mk_indices ITER_STATES;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 70 ▶use constant REGISTRY_INDICES => qw( REG_ITERATOR REG_GENERAL REG_METHODS );
Subroutine BEGIN: called CC=1 SCAR=0.0
45 70 ▶use constant mk_indices REGISTRY_INDICES;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 70 ▶use constant EXHAUSTION_ACTIONS => qw[ THROW RETURN PASSTHROUGH ];
Subroutine BEGIN: called CC=1 SCAR=0.0
48 70 ▶use constant mk_lc EXHAUSTION_ACTIONS;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 # these duplicate ITER_ATTRS. combine?
51 70 use constant INTERFACE_PARAMETERS =>
52 70 ▶ qw( CURRENT FREEZE METHODS NEXT PREV RESET REWIND STATE _DEPENDS _NAME _ROLES _SELF );
Subroutine BEGIN: called CC=1 SCAR=0.0
53 70 ▶use constant INTERFACE_PARAMETER_VALUES => map { lc $_ } INTERFACE_PARAMETERS;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 70 ▶use constant mk_lc INTERFACE_PARAMETERS;
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56  
57 70 ▶use constant SIGNAL_PARAMETERS => qw( INPUT_EXHAUSTION EXHAUSTION ERROR );
Subroutine BEGIN: called CC=1 SCAR=0.0
58 70 ▶use constant SIGNAL_PARAMETER_VALUES => map { lc $_ } SIGNAL_PARAMETERS;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 70 ▶use constant mk_lc SIGNAL_PARAMETERS;
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 70 ▶use constant GENERAL_PARAMETERS => ( INTERFACE_PARAMETERS, SIGNAL_PARAMETERS );
Subroutine BEGIN: called CC=1 SCAR=0.0
62 70 ▶use constant REGISTRY_GENPAR_INDICES => map { 'REG_GP_' . $_ } GENERAL_PARAMETERS;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 70 ▶use constant mk_indices REGISTRY_GENPAR_INDICES;
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 70 our \%RegGeneralParameterIndexMap = mk_indices map { lc } GENERAL_PARAMETERS;
66  
67 70 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 70 $EXPORT_TAGS{':all'} = [ map { $_->@* } values %EXPORT_TAGS ];
92  
93 ## no critic ( AutomaticExportation )
94 70 our @EXPORT = @{ $EXPORT_TAGS{default} }; # ??? is this needed?
95  
96 70 our @EXPORT_OK = ( map { $_->@* } values %EXPORT_TAGS, );
97  
98  
99 ▶sub throw_failure ( $failure, $msg ) {
Subroutine throw_failure: called CC=1 SCAR=0.0
Pod undocumented
100 10 require Iterator::Flex::Failure;
101 10 local @Iterator::Flex::Role::Utils::CARP_NOT = scalar caller;
102 10 my $type = join( q{::}, 'Iterator::Flex::Failure', $failure );
103 10 $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 ▶sub parse_pars ( @args ) {
Subroutine parse_pars: called CC=4 SCAR=16.1
Pod documented
119  
120 200 my %pars = do {
121  
122 200 ▶ if ( @args == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 1) { } 200 0 200
123 200 ▶ is_hashref( $args[0] )
Branch: 1 branch
BranchTrueFalseTotal
is_hashref($args[0]) or throw_failure("parameter", "expected a hashref") 0 200 200
124 or throw_failure( parameter => 'expected a hashref' );
125 200 $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 200 my %ipars = delete %pars{ grep exists $pars{$_}, INTERFACE_PARAMETER_VALUES };
136 200 my %spars = delete %pars{ grep exists $pars{$_}, SIGNAL_PARAMETER_VALUES };
137  
138 200 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 ▶sub can_meth ( $obj, @methods ) {
Subroutine can_meth: called CC=10 SCAR=23.0
Pod documented
175  
176 306 ▶ my $par = Ref::Util::is_hashref( $methods[-1] ) ? pop @methods : {};
Branch: 1 branch
BranchTrueFalseTotal
is_hashref($methods[-1]) ? : 3 303 306
177  
178 306 for my $method ( @methods ) {
179 306 ▶ throw_failure( parameter => q{'method' parameters must be a string} )
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_ref($method) 2 304 306
180 if Ref::Util::is_ref( $method );
181  
182 304 my $sub;
183 304 foreach ( "__${method}__", $method ) {
184 369 ▶ if ( defined( $sub = $obj->can( $_ ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($sub = $obj->can($_))) 284 85 369
185 284 ▶ my @ret = ( ( !!$par->{name} ? $_ : () ), ( !!$par->{code} ? $sub : () ) );
Branch: 2 branches
BranchTrueFalseTotal
!!$par->{'name'} ? : 2 282 284
!!$par->{'code'} ? : 2 282 284
186 284 ▶ push @ret, $sub unless @ret;
Branch: 1 branch
BranchTrueFalseTotal
unless @ret 281 3 284
187 284 ▶ return @ret > 1 ? @ret : $ret[0];
Branch: 1 branch
BranchTrueFalseTotal
@ret > 1 ? : 1 283 284
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 ▶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 ▶sub load_module ( $path, @namespaces ) {
Subroutine load_module: called CC=5 SCAR=17.3
Pod documented
247  
248 1764 ▶ if ( substr( $path, 0, 1 ) eq q{+} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($path, 0, 1) eq '+') { } 41 1723 1764
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 1723 for my $namespace ( @namespaces ) {
255 1723 my $module = $namespace . q{::} . $path;
256 1723 ▶ return $module if eval { Module::Runtime::require_module( $module ) };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { &Module::Runtime::require_module($module) } } 1723 0 1723
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 ▶sub load_role ( $role, @namespaces ) {
Subroutine load_role: called CC=1 SCAR=0.0
Pod documented
277 1764 load_module( $role, @namespaces );
278 }
279  
280  
281 70 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__
« blib/lib/Iterator/Flex/Chunk.pm ↑ Summary blib/lib/Iterator/Flex/Role/Wrap/Return.pm »