Dry.pm

filter: Statementstmt 99.1% 117 / 118 Branchbran 83.3% 25 / 30 Conditioncond 60.0% 12 / 20 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 100% 27 / 27 Podpod 100% 4 / 4 totaltotal 89.9% 187 / 208 CC 26 SCAR 21.5
CC
26
Cov
92%
__check_parameter
20.4
_error
4.8
tdef
3.0
CRAP 8.6
↑ Summary Dry/Types/Number.pm »
1 #* Name: Params::Dry
2 #* Info: Simple Global Params Management System
3 #* Author: Pawel Guspiel (neo77) <neo@cpan.org>
4 #*
5 #* First. If you can use any function as in natural languague - you will use and understand it even after few months.
6 #*
7 #* Second. Your lazy life will be easy, and you will reduce a lot of errors if you will have guarancy that your parameter
8 #* for example ,,client'', in whole project means the same ( ex. is defined as string(32) ).
9 #*
10 #* Third. You are lazy, so to have this guarancy, you want to set it, in one and only in one place.
11 #*
12 #* Yes, DRY principle in its pure form!
13 #*
14 #* So all what you can find in this module.
15 #*
16 #* That's all. Easy to use. Easy to manage. Easy to understand.
17 #*
18 #* Additional informations
19 #* 1. I didn't wrote here any special extensions (callbacks, ordered parameter list, evals etc). Params module has to be fast.
20 #* If there will be any extension in future. It will be in separate module.
21 #* 2. Ordered parameters list or named parameter list? Named parameter list. For sure.
22 #* Majority of the time you are spending on READING code, not writing it. So for sure named parameter list is better.
23 #*
24  
25 package Params::Dry;
26 {
27  
28 3 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 3 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 3 ▶ use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 # --- version ---
34 3 our $VERSION = 1.20_03;
35  
36 #=------------------------------------------------------------------------ { use, constants }
37  
38 3 ▶ use Carp; # confess
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 3 ▶ use Params::Dry::Types; # to mark that will reserving this namespace (build in types)
Subroutine BEGIN: called CC=1 SCAR=0.0
41 3 ▶ use Params::Dry::Types; # to mark that will reserving this namespace (build in types)
Subroutine BEGIN: called CC=1 SCAR=0.0
42 3 ▶ use Params::Dry::Types::String; # string extended types
Subroutine BEGIN: called CC=1 SCAR=0.0
43 3 ▶ use Params::Dry::Types::Object; # object extended types
Subroutine BEGIN: called CC=1 SCAR=0.0
44 3 ▶ use Params::Dry::Types::DateTime; # datetime extended types
Subroutine BEGIN: called CC=1 SCAR=0.0
45 3 ▶ use Params::Dry::Types::Number; # number extended types
Subroutine BEGIN: called CC=1 SCAR=0.0
46 3 ▶ use Params::Dry::Types::Ref; # ref extended types
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 3 ▶ use constant DEFAULT_TYPE => 1; # default check (for param_op)
Subroutine BEGIN: called CC=1 SCAR=0.0
49 3 ▶ use constant TRUE => 1; # true
Subroutine BEGIN: called CC=1 SCAR=0.0
50 3 ▶ use constant FALSE => 0; # and false
Subroutine BEGIN: called CC=1 SCAR=0.0
51 3 ▶ use constant OK => TRUE; # true
Subroutine BEGIN: called CC=1 SCAR=0.0
52 3 ▶ use constant NO => FALSE; # false
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 3 our $Debug = FALSE; # use Debug mode or not
55  
56 #=------------------------------------------------------------------------ { export }
57  
58 # import strict params
59  
60 3 ▶ use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 3 our @EXPORT_OK = qw(__ rq op tdef typedef no_more DEFAULT_TYPE param_rq param_op);
63  
64 3 our %EXPORT_TAGS = (
65 shorten => [qw(__ rq op tdef no_more DEFAULT_TYPE)],
66 short => [qw(__ rq op typedef no_more DEFAULT_TYPE)],
67 long => [qw(__ param_rq param_op typedef no_more DEFAULT_TYPE)]
68 );
69  
70 #=------------------------------------------------------------------------ { module private functions }
71  
72 #=---------
73 # _error
74 #=---------
75 #* printing error message
76 # RETURN: dies (in case of Debug is making confess)
77 10 ▶ sub _error {
Subroutine _error: called CC=4 SCAR=15.6
78 10 my ( $package, $filename, $line, $subroutine, $evaltext ) = ( caller( 1 ) )[ 0 .. 3, 6 ];
79  
80 10 ▶ my $message = ' at ' . ( $subroutine || $evaltext || 'no sub' ) . " line $line\n";
Condition40%2/5
$subroutine || $evaltext || 'no sub'
l!l
100
$subroutine or $evaltext
l!l&&r!l&&!r
1000
MC/DC0%0/2
$subroutine || $evaltext || 'no sub'
$subroutine $evaltext
Truth table33%1/3
A or B
A= $subroutineB= $evaltext
ABresult
1X1
011
000
81 10 my $debug = "\nvim $filename +$line\n\n";
82  
83 10 ▶ ( $Params::Dry::Debug ) ? confess( @_, $message, $debug ) : die( @_, $message );
Branch: 1 branch
BranchTrueFalseTotal
$Params::Dry::Debug ? : 0 10 10
84 } #+ end of: sub _error
85  
86 #=-----------------------
87 # __get_effective_type
88 #=-----------------------
89 #* counts effective type of type (ex. for super_client base type is client and for client base type is String[20]
90 #* so for super_client final type will be String[20])
91 #* RETURN: final type string
92 55 ▶ sub __get_effective_type {
Subroutine __get_effective_type: called CC=2 SCAR=6.9
93 55 my $param_type = $Params::Dry::Internal::typedefs{ "$_[0]" };
94  
95 55 ▶ if ( $param_type ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param_type) { } 22 33 55
96  
97 22 my @effective_params_list = map { split /\s*\|\s*/, __get_effective_type( $_ ) } split /\s*\|\s*/, $param_type;
98  
99 22 return join '|', sort keys %{ { map { $_ => 1 } @effective_params_list } };
100  
101 } else {
102 33 return $_[0];
103 } #+ end of: else [ if ( $param_type ) ]
104  
105 } #+ end of: sub __get_effective_type
106  
107 #=--------------------
108 # __check_parameter
109 #=--------------------
110 #* checks validity of the parameter
111 #* RETURN: param value
112 18 ▶ sub __check_parameter {
Subroutine __check_parameter: called CC=19 SCAR=30.2
113 18 my ( $p_name, $p_type, $p_default, $p_is_required ) = @_;
114  
115 # --- check internal syntax ---
116 18 ▶ _error( "Name of the parameter has to be defined" ) unless $p_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $p_name 0 18 18
117  
118 # --- detect type (set explicite or get it from name?)
119 18 ▶ my $counted_param_type = ( !defined( $p_type ) or ( $p_type =~ /^\d+$/ and $p_type == DEFAULT_TYPE ) ) ? $p_name : $p_type;
Branch: 1 branch
BranchTrueFalseTotal
!defined($p_type) || $p_type =~ /^\d+$/ && $p_type == 1 ? : 1 17 18
Condition66%4/6
not defined $p_type or $p_type =~ /^\d+$/ and $p_type == 1
l!l&&r!l&&!r
0117
$p_type =~ /^\d+$/ and $p_type == 1
!ll&&!rl&&r
1701
MC/DC33%1/3
not defined $p_type or $p_type =~ /^\d+$/ and $p_type == 1
not defined $p_type $p_type =~ /^\d+$/ $p_type == 1
Truth table50%2/4
not(A) or B and C
A= not defined $p_typeB= $p_type =~ /^\d+$/C= $p_type == 1
ABCresult
1XX1
0111
00X0
0100
120  
121 # --- check effective parameter definition
122 18 my $effective_param_type = __get_effective_type( $counted_param_type );
123  
124 # --- check effective parameter definition for used name (if exists) and if user is not trying to replace name-type with new one (to keep clean naminigs)
125 18 ▶ if ( $Params::Dry::Internal::typedefs{ "$p_name" } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Params::Dry::Internal::typedefs{"$p_name"}) 3 15 18
126 3 my $effective_name_type = __get_effective_type( $p_name );
127 3 ▶ _error( "This variable $p_name is used before in code as $p_name type ($effective_name_type) and here you are trying to redefine it to $counted_param_type ($effective_param_type)" )
Branch: 1 branch
BranchTrueFalseTotal
if $effective_name_type ne $effective_param_type 1 2 3
128 if $effective_name_type ne $effective_param_type;
129 } #+ end of: if ( $Params::Dry::Internal::typedefs...)
130  
131 # --- getting final parameter value ---
132 17 ▶ my $param_value = ( $Params::Dry::Internal::current_params->{ "$p_name" } ) // $p_default // undef;
Condition60%3/5
($$Params::Dry::Internal::current_params{"$p_name"} // $p_default) // undef
l!l
170
$$Params::Dry::Internal::current_params{"$p_name"} // $p_default
l!l&&r!l&&!r
1430
MC/DC0%0/2
($$Params::Dry::Internal::current_params{"$p_name"} // $p_default) // undef
$$Params::Dry::Internal::current_params{"$p_name"} $p_default
Truth table66%2/3
A // B
A= $$Params::Dry::Internal::current_params{"$p_name"}B= $p_default
ABresult
1X1
011
000
133  
134 # --- required / optional
135 17 ▶ if ( !defined( $param_value ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $param_value) 0 17 17
136 0 ▶ ( $p_is_required ) ? _error( "Parameter '$p_name' is required)" ) : return;
Branch: 1 branch
BranchTrueFalseTotal
$p_is_required ? : 0 0 0
137 } #+ end of: if ( !defined( $param_value...))
138  
139 17 my @check_functions = ();
140  
141 # --- prepare all check functions names and its parameters
142 17 for my $effective_param_type ( split /\s*\|\s*/, $effective_param_type ) {
143  
144 # --- get package, function and parameters
145 22 my ( $type_package, $type_function, $parameters ) = $effective_param_type =~ /^(?:(.+)::)?([^\[]+)(?:\[(.+?)\])?/;
146  
147 22 ▶ my $final_type_package = ( $type_package ) ? 'Params::Dry::Types::' . $type_package : 'Params::Dry::Types';
Branch: 1 branch
BranchTrueFalseTotal
$type_package ? : 3 19 22
148  
149 22 ▶ my @type_parameters = split /\s*,\s*/, $parameters // '';
Condition100%2/2
$parameters // ""
l!l
166
MC/DC100%1/1
$parameters // ""
$parameters
Truth table100%2/2
A
A= $parameters
Aresult
11
00
150  
151 # --- set default type unless type ---
152 22 ▶ _error( "Type $counted_param_type ($effective_param_type) is not defined" ) unless $final_type_package->can( "$type_function" );
Branch: 1 branch
BranchTrueFalseTotal
unless $final_type_package->can("$type_function") 1 21 22
153  
154 21 push @check_functions, { check_function => $final_type_package . '::' . $type_function, type_parameters => \@type_parameters };
155 } #+ end of: for my $effective_param_type...
156  
157 # --- check if is valid
158 16 my $is_valid = NO;
159 16 for my $check_function_hash ( @check_functions ) {
160 21 my $check_function = $check_function_hash->{ 'check_function' };
161 21 ▶ my $type_parameters = $check_function_hash->{ 'type_parameters' } || [];
Condition50%1/2
$check_function_hash->{'type_parameters'} || []
l!l
210
MC/DC0%0/1
$check_function_hash->{'type_parameters'} || []
$check_function_hash->{'type_parameters'}
Truth table50%1/2
A
A= $check_function_hash->{'type_parameters'}
Aresult
11
00
162 {
163 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
164 21 ▶ &$check_function( $param_value, @$type_parameters ) and $is_valid = TRUE;
Branch: 1 branch
BranchTrueFalseTotal
&$check_function($param_value, @$type_parameters) and $is_valid = 1 9 12 21
165 }
166 } #+ end of: for my $check_function_hash...
167 16 ▶ _error( "Parameter '$p_name' is not '$counted_param_type' type (effective: $effective_param_type)" ) unless $is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_valid 7 9 16
168  
169 9 $param_value;
170 } #+ end of: sub __check_parameter
171  
172 #=------------------------------------------------------------------------ { module public functions }
173  
174 #=-----
175 # rq
176 #=-----
177 #* check if required parameter exists, if yes check it, if not report error
178 #* RETURN: param value
179 1 ▶ sub rq($;$$) {
Subroutine rq: called CC=1 SCAR=0.0
Pod documented
180 1 my ( $p_name, $p_type, $p_default ) = @_;
181  
182 1 return __check_parameter( $p_name, $p_type, $p_default, TRUE );
183 } #+ end of: sub rq($;$$)
184  
185 #=-----
186 # op
187 #=-----
188 #* check if required parameter exists, if yes check it, if not return undef
189 #* RETURN: param value
190 1 ▶ sub op($;$$) {
Subroutine op: called CC=1 SCAR=0.0
Pod documented
191 1 my ( $p_name, $p_type, $p_default ) = @_;
192  
193 1 return __check_parameter( $p_name, $p_type, $p_default, FALSE );
194 } #+ end of: sub op($;$$)
195  
196 #=------
197 # tdef
198 #=------
199 #* make relation between name and definition, which can be used to check param types
200 #* RETURN: name of the type
201 10 ▶ sub tdef($$) {
Subroutine tdef: called CC=3 SCAR=11.0
Pod documented
202 10 my ( $p_name, $p_definition ) = @_;
203  
204 10 ▶ if ( exists $Params::Dry::Internal::typedefs{ $p_name } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Params::Dry::Internal::typedefs{$p_name}) 3 7 10
205 _error( "Error parameter $p_name already defined as $p_definition" )
206 3 ▶ if __get_effective_type( $Params::Dry::Internal::typedefs{ $p_name } ) ne __get_effective_type( $p_definition );
Branch: 1 branch
BranchTrueFalseTotal
if __get_effective_type($Params::Dry::Internal::typedefs{$p_name}) ne __get_effective_type($p_definition) 1 2 3
207 } #+ end of: if ( exists $Params::Dry::Internal::typedefs...)
208  
209 # --- just add new definition
210 9 $Params::Dry::Internal::typedefs{ $p_name } = $p_definition;
211  
212 9 return $p_name;
213  
214 } #+ end of: sub tdef($$)
215  
216 #=-----
217 # __
218 #=-----
219 #* gets the parameters to internal use
220 # RETURN: first param if params like (object, %params) or undef otherwise
221 15 ▶ sub __ {
Subroutine __: called CC=2 SCAR=6.9
222 15 ▶ my $self = ( ( scalar @_ % 2 ) ? shift : undef );
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ % 2 ? : 1 14 15
223 15 push @Params::Dry::Internal::params_stack, { @_ };
224 15 $Params::Dry::Internal::current_params = $Params::Dry::Internal::params_stack[-1];
225  
226 15 return $self;
227 } #+ end of: sub __
228  
229 #=----------
230 # no_more
231 #=----------
232 #* mark end of param processing part
233 #* required in case param call during param checking
234 # RETURN: current params
235 3 ▶ sub no_more() {
Subroutine no_more: called CC=1 SCAR=0.0
Pod documented
236  
237 3 pop @Params::Dry::Internal::params_stack;
238 3 $Params::Dry::Internal::current_params = $Params::Dry::Internal::params_stack[-1];
239 } #+ end of: sub no_more
240  
241 # --- add additional names for funtions (long)
242  
243 {
244 3 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
245  
246 3 *param_rq = *rq;
247 3 *param_op = *op;
248 3 *typedef = *tdef;
249 };
250 };
251 3 0115 && 0x4d;
252  
253 # ABSTRACT: Simple Global Params Management System
254  
255 #+ End of Params::Dry
256 __END__
↑ Summary Dry/Types/Number.pm »