File Coverage

blib/lib/Params/Dry/Declare.pm
Criterion Covered Total %
statement 15 15 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 20 20 100.0


line stmt bran cond sub pod time code
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)
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 2     2   124392 use Params::Dry qw(:short); # required to take care of parameters (and because is the best of course ;)
  2         23422  
  2         327  
29             # for multi types 1.20 or higher is required
30              
31             package Params::Dry::Declare;
32             {
33 2     2   17 use strict;
  2         4  
  2         51  
34 2     2   9 use warnings;
  2         9  
  2         65  
35              
36             # --- version ---
37             our $VERSION = 0.9907;
38              
39             #=------------------------------------------------------------------------ { use, constants }
40              
41 2     2   146113 use Filter::Simple; # extends subroutine definition
  2         446386  
  2         16  
42              
43             #=------------------------------------------------------------------------ { module magic }
44              
45             FILTER_ONLY code_no_comments => sub {
46             while ( my ( $orig_sub, $sub_name, $sub_declared_vars ) = $_ =~ /(sub\s+(\w+)\s*\(\s*(?:(.+?)\s*)?\)\s*{)/s ) {
47              
48             # --- clean
49             $sub_declared_vars //= '';
50             $sub_declared_vars =~ s/\n//;
51              
52             # --- prepare variables string
53             my $variables_string = 'my $self = __@_;';
54              
55             # --- parse variables
56             for my $param ( split /\s*;\s*/, $sub_declared_vars ) {
57              
58             #+ remove comments
59             $param =~ s/---.+?([?!]|$)/$1/s;
60              
61             next unless $param;
62              
63             #+ parse
64             $param =~ /^(?[!?]) \s* (?\w+) \s* : \s* (?\w+ (?:\[.+?\])? (?:\|\w+ (?:\[.+?\])?)* )? \s* (?:= \s* (?.+))? (?:[#].*)?$/x;
65              
66             my ( $is_rq, $param_name, $param_type, $default ) = ( '' ) x 4;
67              
68 2     2   8844 $is_rq = $+{ 'is_rq' } eq '!';
  2         1301  
  2         444  
69             ( $param_name, $param_type, $default ) = ( $+{ 'param_name' }, $+{ 'param_type' }, $+{ 'default' } );
70             $param_type ||= $param_name;
71              
72             $variables_string .= "my \$p_$param_name = " . ( ( $is_rq ) ? 'rq' : 'op' ) . " '$param_name'";
73             $variables_string .= ", '$param_type'" if $param_type;
74             $variables_string .= ", $default" if $default;
75             $variables_string .= '; ';
76             } #+ end of: for my $param ( split /\s*;\s*/...)
77              
78             # --- for errors in correct lines
79             my $new_lines = "\n" x ( $orig_sub =~ s/\n/\n/gs );
80             s/\Q$orig_sub/sub $sub_name { $new_lines $variables_string no_more;/;
81              
82             } #+ end of: while ( my ( $orig_sub, $sub_name...))
83             $_;
84             };
85             };
86             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__