blib/lib/Params/Dry/Declare.pm

filter: Statementstmt 100% 35 / 35 Branchbran 90.0% 9 / 10 Conditioncond 60.0% 3 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 89.6% 52 / 58 CC 9 SCAR 18.8
CC
9
Cov
94%
__ANON__
9.1
BEGIN
1.0
BEGIN
1.0
CRAP 6.6
↑ Summary
1 #*
2 #* Name: Params::Dry::Declare (some magic for Params::Dry :)
3 #* Info: Extension to Params::Dry, which make possible declaration of the parameters
4 #* Author: Pawel Guspiel (neo77) <neo@cpan.org>
5 #* Details:
6 #* The module allow parameters declaration in following form:
7 #*
8 #* sub new (
9 #* ! name:; --- name of the user
10 #* ? second_name : name = 'unknown'; --- second name
11 #* ? details : String = 'yakusa'; --- details
12 #* ) {
13 #*
14 #* instead of using:
15 #*
16 #* sub table {
17 #* my $self = __@_;
18 #*
19 #* my $p_name = rq 'name', DEFAULT_TYPE; # name of the user
20 #* my $p_second_name = op 'second_name', 'name', 'unknown'; # second name
21 #* ...
22 #*
23 #* As a consequence of using this module you can use parameters in the function body as follows:
24 #* print "name: ".p_name;
25 #*
26 #* I'm suggesting you to use coloring in your text editor for p_\w+ to see function parameters everywhere
27  
28 3 ▶use Params::Dry qw(:short); # required to take care of parameters (and because is the best of course ;)
Subroutine BEGIN: called CC=1 SCAR=0.0
29 # for multi types 1.20 or higher is required
30  
31 package Params::Dry::Declare;
32 {
33 3 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 3 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 # --- version ---
37 3 our $VERSION = 0.9907;
38  
39 #=------------------------------------------------------------------------ { use, constants }
40  
41 3 ▶ use Filter::Simple; # extends subroutine definition
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 #=------------------------------------------------------------------------ { module magic }
44  
45 2 ▶ FILTER_ONLY code_no_comments => sub {
Subroutine __ANON__: called CC=9 SCAR=22.0
46 2 ▶ while ( my ( $orig_sub, $sub_name, $sub_declared_vars ) = $_ =~ /(sub\s+(\w+)\s*\(\s*(?:(.+?)\s*)?\)\s*{)/s ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $_ =~ /(sub\s+(\w+)\s*\(\s*(?:(.+?)\s*)?\)\s*{)/s) 6 2 8
47  
48 # --- clean
49 6 ▶ $sub_declared_vars //= '';
Condition50%1/2
$sub_declared_vars //= ""
l!l
60
MC/DC0%0/1
$sub_declared_vars //= ""
$sub_declared_vars
Truth table50%1/2
A
A= $sub_declared_vars
Aresult
11
00
50 6 $sub_declared_vars =~ s/\n//;
51  
52 # --- prepare variables string
53 6 my $variables_string = 'my $self = __@_;';
54  
55 # --- parse variables
56 6 for my $param ( split /\s*;\s*/, $sub_declared_vars ) {
57  
58 #+ remove comments
59 14 $param =~ s/---.+?([?!]|$)/$1/s;
60  
61 14 ▶ next unless $param;
Branch: 1 branch
BranchTrueFalseTotal
unless $param 5 9 14
62  
63 #+ parse
64 9 $param =~ /^(?<is_rq>[!?]) \s* (?<param_name>\w+) \s* : \s* (?<param_type>\w+ (?:\[.+?\])? (?:\|\w+ (?:\[.+?\])?)* )? \s* (?:= \s* (?<default>.+))? (?:[#].*)?$/x;
65  
66 9 my ( $is_rq, $param_name, $param_type, $default ) = ( '' ) x 4;
67  
68 9 $is_rq = $+{ 'is_rq' } eq '!';
69 9 ( $param_name, $param_type, $default ) = ( $+{ 'param_name' }, $+{ 'param_type' }, $+{ 'default' } );
70 9 ▶ $param_type ||= $param_name;
Condition66%2/3
$param_type ||= $param_name
l!l&&r!l&&!r
630
MC/DC0%0/2
$param_type ||= $param_name
$param_type $param_name
Truth table66%2/3
A ||= B
A= $param_typeB= $param_name
ABresult
1X1
011
000
71  
72 9 ▶ $variables_string .= "my \$p_$param_name = " . ( ( $is_rq ) ? 'rq' : 'op' ) . " '$param_name'";
Branch: 1 branch
BranchTrueFalseTotal
$is_rq ? : 7 2 9
73 9 ▶ $variables_string .= ", '$param_type'" if $param_type;
Branch: 1 branch
BranchTrueFalseTotal
if $param_type 9 0 9
74 9 ▶ $variables_string .= ", $default" if $default;
Branch: 1 branch
BranchTrueFalseTotal
if $default 7 2 9
75 9 $variables_string .= '; ';
76 } #+ end of: for my $param ( split /\s*;\s*/...)
77  
78 # --- for errors in correct lines
79 6 my $new_lines = "\n" x ( $orig_sub =~ s/\n/\n/gs );
80 6 s/\Q$orig_sub/sub $sub_name { $new_lines $variables_string no_more;/;
81  
82 } #+ end of: while ( my ( $orig_sub, $sub_name...))
83 2 $_;
84 3 };
85 };
86 3 0115 && 0x4d;
87  
88 #+ End of Params::Declare magic :)
89 # ABSTRACT: Declare extension for Params::Dry - Simple Global Params Management System which helps you to keep the DRY rule everywhere
90  
91 #+ End of Params::Dry
92 __END__
↑ Summary