Dry.pm

filter: Statementstmt 99.1% 114 / 115 Branchbran 83.3% 25 / 30 Conditioncond 66.6% 10 / 15 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 100% 27 / 27 Podpod 100% 4 / 4 totaltotal 91.9% 182 / 198 CC 24 SCAR 21.6
CC
24
Cov
93%
__check_parameter
20.4
tdef
3.0
_error
2.1
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_01;
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 sub _error {
78 10 ▶ ( $Params::Dry::Debug ) ? confess( @_ ) : die( @_ );
Subroutine _error: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$Params::Dry::Debug ? : 0 10 10
79 } #+ end of: sub _error
80  
81 #=-----------------------
82 # __get_effective_type
83 #=-----------------------
84 #* counts effective type of type (ex. for super_client base type is client and for client base type is String[20]
85 #* so for super_client final type will be String[20])
86 #* RETURN: final type string
87 sub __get_effective_type {
88 55 ▶ my $param_type = $Params::Dry::Internal::typedefs{ "$_[0]" };
Subroutine __get_effective_type: called CC=2 SCAR=6.9
89  
90 55 ▶ if ( $param_type ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param_type) { } 22 33 55
91  
92 22 my @effective_params_list = map { split /\s*\|\s*/, __get_effective_type( $_ ) } split /\s*\|\s*/, $param_type;
93  
94 22 return join '|', sort keys %{ { map { $_ => 1 } @effective_params_list } };
95  
96 } else {
97 33 return $_[0];
98 } #+ end of: else [ if ( $param_type ) ]
99  
100 } #+ end of: sub __get_effective_type
101  
102 #=--------------------
103 # __check_parameter
104 #=--------------------
105 #* checks validity of the parameter
106 #* RETURN: param value
107 sub __check_parameter {
108 18 ▶ my ( $p_name, $p_type, $p_default, $p_is_required ) = @_;
Subroutine __check_parameter: called CC=19 SCAR=30.2
109  
110 # --- check internal syntax ---
111 18 ▶ _error( "Name of the parameter has to be defined" ) unless $p_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $p_name 0 18 18
112  
113 # --- detect type (set explicite or get it from name?)
114 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
115  
116 # --- check effective parameter definition
117 18 my $effective_param_type = __get_effective_type( $counted_param_type );
118  
119 # --- 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)
120 18 ▶ if ( $Params::Dry::Internal::typedefs{ "$p_name" } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Params::Dry::Internal::typedefs{"$p_name"}) 3 15 18
121 3 my $effective_name_type = __get_effective_type( $p_name );
122 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
123 if $effective_name_type ne $effective_param_type;
124 } #+ end of: if ( $Params::Dry::Internal::typedefs...)
125  
126 # --- getting final parameter value ---
127 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
128  
129 # --- required / optional
130 17 ▶ if ( !defined( $param_value ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $param_value) 0 17 17
131 0 ▶ ( $p_is_required ) ? _error( "Parameter '$p_name' is required)" ) : return;
Branch: 1 branch
BranchTrueFalseTotal
$p_is_required ? : 0 0 0
132 } #+ end of: if ( !defined( $param_value...))
133  
134 17 my @check_functions = ();
135  
136 # --- prepare all check functions names and its parameters
137 17 for my $effective_param_type ( split /\s*\|\s*/, $effective_param_type ) {
138  
139 # --- get package, function and parameters
140 22 my ( $type_package, $type_function, $parameters ) = $effective_param_type =~ /^(?:(.+)::)?([^\[]+)(?:\[(.+?)\])?/;
141  
142 22 ▶ my $final_type_package = ( $type_package ) ? 'Params::Dry::Types::' . $type_package : 'Params::Dry::Types';
Branch: 1 branch
BranchTrueFalseTotal
$type_package ? : 3 19 22
143  
144 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
145  
146 # --- set default type unless type ---
147 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
148  
149 21 push @check_functions, { check_function => $final_type_package . '::' . $type_function, type_parameters => \@type_parameters };
150 } #+ end of: for my $effective_param_type...
151  
152 # --- check if is valid
153 16 my $is_valid = NO;
154 16 for my $check_function_hash ( @check_functions ) {
155 21 my $check_function = $check_function_hash->{ 'check_function' };
156 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
157 {
158 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
159 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
160 }
161 } #+ end of: for my $check_function_hash...
162 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
163  
164 9 $param_value;
165 } #+ end of: sub __check_parameter
166  
167 #=------------------------------------------------------------------------ { module public functions }
168  
169 #=-----
170 # rq
171 #=-----
172 #* check if required parameter exists, if yes check it, if not report error
173 #* RETURN: param value
174 sub rq($;$$) {
175 1 ▶ my ( $p_name, $p_type, $p_default ) = @_;
Subroutine rq: called CC=1 SCAR=0.0
Pod documented
176  
177 1 return __check_parameter( $p_name, $p_type, $p_default, TRUE );
178 } #+ end of: sub rq($;$$)
179  
180 #=-----
181 # op
182 #=-----
183 #* check if required parameter exists, if yes check it, if not return undef
184 #* RETURN: param value
185 sub op($;$$) {
186 1 ▶ my ( $p_name, $p_type, $p_default ) = @_;
Subroutine op: called CC=1 SCAR=0.0
Pod documented
187  
188 1 return __check_parameter( $p_name, $p_type, $p_default, FALSE );
189 } #+ end of: sub op($;$$)
190  
191 #=------
192 # tdef
193 #=------
194 #* make relation between name and definition, which can be used to check param types
195 #* RETURN: name of the type
196 sub tdef($$) {
197 10 ▶ my ( $p_name, $p_definition ) = @_;
Subroutine tdef: called CC=3 SCAR=11.0
Pod documented
198  
199 10 ▶ if ( exists $Params::Dry::Internal::typedefs{ $p_name } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Params::Dry::Internal::typedefs{$p_name}) 3 7 10
200 _error( "Error parameter $p_name already defined as $p_definition" )
201 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
202 } #+ end of: if ( exists $Params::Dry::Internal::typedefs...)
203  
204 # --- just add new definition
205 9 $Params::Dry::Internal::typedefs{ $p_name } = $p_definition;
206  
207 9 return $p_name;
208  
209 } #+ end of: sub tdef($$)
210  
211 #=-----
212 # __
213 #=-----
214 #* gets the parameters to internal use
215 # RETURN: first param if params like (object, %params) or undef otherwise
216 sub __ {
217 15 ▶ my $self = ( ( scalar @_ % 2 ) ? shift : undef );
Subroutine __: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ % 2 ? : 1 14 15
218 15 push @Params::Dry::Internal::params_stack, { @_ };
219 15 $Params::Dry::Internal::current_params = $Params::Dry::Internal::params_stack[-1];
220  
221 15 return $self;
222 } #+ end of: sub __
223  
224 #=----------
225 # no_more
226 #=----------
227 #* mark end of param processing part
228 #* required in case param call during param checking
229 # RETURN: current params
230 sub no_more() {
231  
232 3 ▶ pop @Params::Dry::Internal::params_stack;
Subroutine no_more: called CC=1 SCAR=0.0
Pod documented
233 3 $Params::Dry::Internal::current_params = $Params::Dry::Internal::params_stack[-1];
234 } #+ end of: sub no_more
235  
236 # --- add additional names for funtions (long)
237  
238 {
239 3 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
240  
241 3 *param_rq = *rq;
242 3 *param_op = *op;
243 3 *typedef = *tdef;
244 };
245 };
246 3 0115 && 0x4d;
247  
248 # ABSTRACT: Simple Global Params Management System
249  
250 #+ End of Params::Dry
251 __END__
↑ Summary Dry/Types/Number.pm »