| 1 | package Params::Smart; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.006; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; # ::register __PACKAGE__; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Regexp::Common qw( delimited ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | require Exporter; | |||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | our @ISA = qw( Exporter ); | |||||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | our @EXPORT = qw( Params ); | |||||||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | our @EXPORT_OK = qw( Params ParamsNC ); | |||||||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | our %EXPORT_TAGS = ( all => \@EXPORT_OK ); | |||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | our $VERSION = '0.09'; | |||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 71 | ▶ | sub parse_param { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_param: called CC=14 SCAR=29.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 71 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 21 | 71 | my $param = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 71 | local ($_); | |||||||||||||||||||||||||||||||||||||||||||||
| 24 | 71 | ▶ | if (ref($param) eq "HASH") { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 25 | # we only want to pass supported parameters | ||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 17 | my $info = { | |||||||||||||||||||||||||||||||||||||||||||||
| 27 | _parsed => 0, | ||||||||||||||||||||||||||||||||||||||||||||||
| 28 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 17 | foreach (qw( | |||||||||||||||||||||||||||||||||||||||||||||
| 30 | name type default required name_only slurp | ||||||||||||||||||||||||||||||||||||||||||||||
| 31 | callback comment needs | ||||||||||||||||||||||||||||||||||||||||||||||
| 32 | )) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 153 | $info->{$_} = $param->{$_}; | |||||||||||||||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 17 | return $info; | |||||||||||||||||||||||||||||||||||||||||||||
| 36 | } elsif (!ref($param)) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 54 | $param =~ /^([\?\+\*]+)?([\@\$\%\&])?([\w\|]+)(\=.+)?/; | |||||||||||||||||||||||||||||||||||||||||||||
| 38 | 54 | ▶ | my $mod = $1 || ""; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$1 || ''
MC/DC100%1/1
$1 || ''
$1
Truth table100%2/2
A
A= $1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 54 | my $type = $2; | |||||||||||||||||||||||||||||||||||||||||||||
| 40 | 54 | my $name = $3; | |||||||||||||||||||||||||||||||||||||||||||||
| 41 | 54 | ▶ | my $def = substr($4,1) if (defined $4); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 54 | ▶ | if ((defined $def) && | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $def and $def =~ /$Params::Smart::RE{"quoted"}{"-keep"}/
MC/DC0%0/2
defined $def and $def =~ /$Params::Smart::RE{"quoted"}{"-keep"}/
defined $def
$def =~ /$Params::Smart::RE{"quoted"}{"-keep"}/
Truth table33%1/3
A and B
A= defined $defB= $def =~ /$Params::Smart::RE{"quoted"}{"-keep"}/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 44 | ($def =~ /$RE{quoted}{-keep}/)) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | $def = $3; | |||||||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 54 | ▶ | unless (defined $name) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | croak "malformed parameter $param"; | |||||||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 54 | ▶ | if ($name =~ /^\_\w+/) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | croak "parameter $name cannot begin with an underscore"; | |||||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 54 | ▶ | if (exists $self->{names}->{$name}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | croak "parameter $name already specified"; | |||||||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 58 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 54 | ▶ | my $info = { | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%6/6
!($mod =~ /\?/) || 0
$mod =~ /\+/ || 0
$mod =~ /\*/ || 0
MC/DC100%1/1
!($mod =~ /\?/) || 0
!($mod =~ /\?/)
MC/DC100%1/1
$mod =~ /\+/ || 0
$mod =~ /\+/
MC/DC100%1/1
$mod =~ /\*/ || 0
$mod =~ /\*/
Truth table100%2/2
not(A)
A= !($mod =~ /\?/)
Truth table100%2/2
A
A= $mod =~ /\+/
Truth table100%2/2
A
A= $mod =~ /\*/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 60 | name => $name, | ||||||||||||||||||||||||||||||||||||||||||||||
| 61 | type => $type, | ||||||||||||||||||||||||||||||||||||||||||||||
| 62 | default => $def, | ||||||||||||||||||||||||||||||||||||||||||||||
| 63 | required => (($mod !~ /\?/) || 0), | ||||||||||||||||||||||||||||||||||||||||||||||
| 64 | name_only => (($mod =~ /\+/) || 0), | ||||||||||||||||||||||||||||||||||||||||||||||
| 65 | slurp => (($mod =~ /\*/) || 0), | ||||||||||||||||||||||||||||||||||||||||||||||
| 66 | callback => undef, # sub { return $_[2]; }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 67 | comment => $name, | ||||||||||||||||||||||||||||||||||||||||||||||
| 68 | needs => undef, | ||||||||||||||||||||||||||||||||||||||||||||||
| 69 | _parsed => 1, | ||||||||||||||||||||||||||||||||||||||||||||||
| 70 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 54 | return $info; | |||||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 73 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | croak "invalid parameter"; | |||||||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 73 | ▶ | sub set_param { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set_param: called CC=10 SCAR=23.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 73 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 81 | 73 | my $info = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 82 | 73 | ▶ | croak "invalid parameter" unless (ref($info) eq "HASH"); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||
| 84 | # TODO - name_only should be set if this is dynamic | ||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 73 | ▶ | $self->{dynamic} ||= ($self->{lock}); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"dynamic"} ||= $self->{"lock"}
MC/DC50%1/2
$self->{"dynamic"} ||= $self->{"lock"}
$self->{"dynamic"}
$self->{"lock"}
Truth table66%2/3
A ||= B
A= $self->{"dynamic"}B= $self->{"lock"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 73 | ▶ | $info->{name_only} ||= ($self->{dynamic}); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$info->{"name_only"} ||= $self->{"dynamic"}
MC/DC100%2/2
$info->{"name_only"} ||= $self->{"dynamic"}
$info->{"name_only"}
$self->{"dynamic"}
Truth table100%3/3
A ||= B
A= $info->{"name_only"}B= $self->{"dynamic"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 73 | my @names = split /\|/, $info->{name}; | |||||||||||||||||||||||||||||||||||||||||||||
| 90 | 73 | $info->{name} = undef; | |||||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 73 | ▶ | do { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 77 | my $name = shift @names; | |||||||||||||||||||||||||||||||||||||||||||||
| 94 | 77 | ▶ | $info->{name} = $name, unless (defined $info->{name}); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 77 | ▶ | if (exists $self->{names}->{$name}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | $self->{names}->{$name} = $info; | |||||||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 98 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 77 | my $index = scalar(@{$self->{order}}); | |||||||||||||||||||||||||||||||||||||||||||||
| 100 | 77 | ▶ | unless ($info->{name_only}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 67 | $info->{_index} = $index; | |||||||||||||||||||||||||||||||||||||||||||||
| 102 | 67 | $self->{order}->[$index] = $name; | |||||||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 77 | $self->{names}->{$name} = $info; | |||||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 77 | ▶ | if (@names) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 4 | ▶ | $info->{name_only} ||= 1; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$info->{"name_only"} ||= 1
MC/DC100%1/1
$info->{"name_only"} ||= 1
$info->{"name_only"}
Truth table100%2/2
A
A= $info->{"name_only"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 4 | $info->{required} = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 109 | 4 | delete $info->{default}; | |||||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 111 | } while (@names); | ||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 73 | return $info; | |||||||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 28 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=12 SCAR=26.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 28 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 117 | 28 | my $self = { | |||||||||||||||||||||||||||||||||||||||||||||
| 118 | names => { }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 119 | order => [ ], | ||||||||||||||||||||||||||||||||||||||||||||||
| 120 | lock => 0, | ||||||||||||||||||||||||||||||||||||||||||||||
| 121 | dynamic => 0, | ||||||||||||||||||||||||||||||||||||||||||||||
| 122 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 28 | bless $self, $class; | |||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 28 | my $index = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 126 | 28 | my $last; | |||||||||||||||||||||||||||||||||||||||||||||
| 127 | 28 | ▶ | SLURP: while (my $param = shift) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 71 | my $info = $self->parse_param($param); | |||||||||||||||||||||||||||||||||||||||||||||
| 130 | 71 | ▶ | if ($info) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 71 | ▶ | if ($info->{slurp}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 2 | ▶ | croak "no parameters can follow a slurp" if (@_); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 71 | ▶ | if ($last && $info->{required} && (!$last->{required})) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$last and $info->{"required"} and not $last->{"required"}
$last and $info->{"required"}
MC/DC0%0/3
$last and $info->{"required"} and not $last->{"required"}
$last
$info->{"required"}
not $last->{"required"}
Truth table75%3/4
A and B and not(C)
A= $lastB= $info->{"required"}C= not $last->{"required"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | croak "a required parameter cannot follow an optional parameter"; | |||||||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 71 | ▶ | if ($info->{name_only} && $info->{slurp}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$info->{"name_only"} and $info->{"slurp"}
MC/DC0%0/2
$info->{"name_only"} and $info->{"slurp"}
$info->{"name_only"}
$info->{"slurp"}
Truth table66%2/3
A and B
A= $info->{"name_only"}B= $info->{"slurp"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | croak "a parameter cannot be named_only and a slurp"; | |||||||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 71 | ▶ | if ($last && ($info->{_parsed} != $last->{_parsed})) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$last and $info->{"_parsed"} != $last->{"_parsed"}
MC/DC0%0/2
$last and $info->{"_parsed"} != $last->{"_parsed"}
$last
$info->{"_parsed"} != $last->{"_parsed"}
Truth table66%2/3
A and B
A= $lastB= $info->{"_parsed"} != $last->{"_parsed"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | croak "cannot mix parsed and non-parsed parameters"; | |||||||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 71 | $self->set_param($info); | |||||||||||||||||||||||||||||||||||||||||||||
| 144 | 71 | $last = $info; | |||||||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 146 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | croak "unknown error"; | |||||||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 71 | $index++; | |||||||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 28 | $self->{lock} = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 153 | 28 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||||||
| 156 | # We have the exported Params() function rather than requiring calls | ||||||||||||||||||||||||||||||||||||||||||||||
| 157 | # to Params::Smart->new() so that the code looks a lot cleaner. It's | ||||||||||||||||||||||||||||||||||||||||||||||
| 158 | # also a wrapper for a home-grown memoization function. (We cannot use | ||||||||||||||||||||||||||||||||||||||||||||||
| 159 | # Memoize because callbacks become problematic.) | ||||||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 1 | my %Memoization = ( ); | |||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 27 | ▶ | sub Params { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Params: called CC=3 SCAR=11.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 27 | ▶ | my $key = join $;, map { $_||""} (caller); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$_ || ''
MC/DC0%0/1
$_ || ''
$_
Truth table50%1/2
A
A= $_
| |||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 27 | ▶ | return $Memoization{$key} ||= __PACKAGE__->new(@_); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$Memoization{$key} ||= __PACKAGE__->new(@_)
MC/DC0%0/2
$Memoization{$key} ||= __PACKAGE__->new(@_)
$Memoization{$key}
__PACKAGE__->new(@_)
Truth table66%2/3
A ||= B
A= $Memoization{$key}B= __PACKAGE__->new(@_)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 2 | ▶ | sub ParamsNC { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ParamsNC: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 2 | return __PACKAGE__->new(@_); | |||||||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||||||
| 172 | # Note: usage does not display aliases, nor named_only parameters | ||||||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 4 | ▶ | sub _usage { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_usage: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 176 | 4 | my $error = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 177 | 4 | ▶ | my $named = shift || 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || 0
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 4 | local($_); | |||||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 4 | ▶ | my $caller = (caller(2))[3] || ""; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
(caller 2)[3] || ''
MC/DC0%0/1
(caller 2)[3] || ''
(caller 2)[3]
Truth table50%1/2
A
A= (caller 2)[3]
| |||||||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 4 | my $usage = $error . ";\nusage: $caller("; | |||||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||||||
| 185 | # TODO - handle named parameters etc. | ||||||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||||||
| 187 | $usage .= | ||||||||||||||||||||||||||||||||||||||||||||||
| 188 | join(", ", map { | ||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 8 | my $name = $_; | |||||||||||||||||||||||||||||||||||||||||||||
| 190 | 8 | ▶ | $name = "?$name", unless ($self->{names}->{$name}->{required}); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 8 | ▶ | $name = "*$name", if ($self->{names}->{$name}->{slurp}); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 8 | $name; | |||||||||||||||||||||||||||||||||||||||||||||
| 193 | 4 | } @{$self->{order}}) . ") "; | |||||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 4 | croak $usage; | |||||||||||||||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||||||
| 199 | # The callback is expected to coerce the data or return an error | ||||||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 2 | ▶ | sub _run_callback { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_run_callback: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 2 | my $self = $_[0]; | |||||||||||||||||||||||||||||||||||||||||||||
| 203 | 2 | my $name = $_[1]; | |||||||||||||||||||||||||||||||||||||||||||||
| 204 | 2 | my $callback = $_[0]->{names}->{$name}->{callback}; | |||||||||||||||||||||||||||||||||||||||||||||
| 205 | 2 | ▶ | if (ref($callback) eq "CODE") { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 2 | return &{$callback}(@_); | |||||||||||||||||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 208 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 0 | croak "expected code reference for callback"; | |||||||||||||||||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 29 | ▶ | sub args { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
args: called CC=39 SCAR=38.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 29 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||||||
| 216 | # TODO - return a reference to $self in the values | ||||||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 29 | my %vals = ( ); | |||||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||||||
| 220 | # $vals{_args} = [ @_ ]; | ||||||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 29 | my $named = !(@_ % 2); | |||||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||||||
| 224 | # For even number positional parameter with undef in them. | ||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 29 | ▶ | for (my $i=0; ($named && ($i < @_)); $i += 2) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$named and $i < @_
MC/DC100%2/2
$named and $i < @_
$named
$i < @_
Truth table100%3/3
A and B
A= $namedB= $i < @_
| |||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 41 | ▶ | if (!defined $_[$i]) { $named = 0 } | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 29 | ▶ | if ($named) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 20 | my %unknown = ( ); | |||||||||||||||||||||||||||||||||||||||||||||
| 231 | 20 | my $i = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 232 | 20 | ▶ | while ($named && ($i < @_)) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$named and $i < @_
MC/DC50%1/2
$named and $i < @_
$named
$i < @_
Truth table66%2/3
A and B
A= $namedB= $i < @_
| |||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 40 | my $n = $_[$i]; | |||||||||||||||||||||||||||||||||||||||||||||
| 234 | 40 | ▶ | $n = substr($n,1) if ($n =~ /^\-/); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 40 | ▶ | if (exists $self->{names}->{$n}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 35 | my $truename = $self->{names}->{$n}->{name}; | |||||||||||||||||||||||||||||||||||||||||||||
| 237 | 35 | $vals{$truename} = $_[$i+1]; | |||||||||||||||||||||||||||||||||||||||||||||
| 238 | 35 | ▶ | if ($self->{names}->{$truename}->{callback}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 2 | $@ = undef; | |||||||||||||||||||||||||||||||||||||||||||||
| 240 | 2 | eval { | |||||||||||||||||||||||||||||||||||||||||||||
| 241 | $vals{$truename} = | ||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 2 | $self->_run_callback($truename, $vals{$truename}, \%vals); | |||||||||||||||||||||||||||||||||||||||||||||
| 243 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 2 | ▶ | $self->_usage($@,$named) if ($@); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 246 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 5 | $unknown{$n} = $i; | |||||||||||||||||||||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 40 | $i += 2; | |||||||||||||||||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||||||
| 252 | # As long as there are unknown keys and dynamically-added | ||||||||||||||||||||||||||||||||||||||||||||||
| 253 | # parameters, we'll keep re-checking. | ||||||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 20 | ▶ | while ($self->{dynamic}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 256 | 2 | $self->{dynamic} = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 257 | 2 | ▶ | if ($named && (keys %unknown)) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$named and keys %unknown
MC/DC50%1/2
$named and keys %unknown
$named
keys %unknown
Truth table66%2/3
A and B
A= $namedB= keys %unknown
| |||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 1 | foreach my $n (keys %unknown) { | |||||||||||||||||||||||||||||||||||||||||||||
| 259 | 1 | ▶ | if (exists $self->{names}->{$n}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 1 | my $truename = $self->{names}->{$n}->{name}; | |||||||||||||||||||||||||||||||||||||||||||||
| 261 | 1 | $vals{$truename} = $_[$unknown{$n}+1]; | |||||||||||||||||||||||||||||||||||||||||||||
| 262 | 1 | ▶ | if ($self->{names}->{$truename}->{callback}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 0 | $@ = undef; | |||||||||||||||||||||||||||||||||||||||||||||
| 264 | 0 | eval { | |||||||||||||||||||||||||||||||||||||||||||||
| 265 | $vals{$truename} = | ||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 0 | $self->_run_callback($truename, $vals{$truename}, \%vals); | |||||||||||||||||||||||||||||||||||||||||||||
| 267 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 0 | ▶ | $self->_usage($@,$named) if ($@); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 1 | delete $unknown{$n}; | |||||||||||||||||||||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 20 | ▶ | if ($named && (keys %unknown) && (keys %vals)) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition83%5/6
$named and keys %unknown and keys %vals
$named and keys %unknown
MC/DC66%2/3
$named and keys %unknown and keys %vals
$named
keys %unknown
keys %vals
Truth table75%3/4
A and B and C
A= $namedB= keys %unknownC= keys %vals
| |||||||||||||||||||||||||||||||||||||||||||||||
| 277 | $self->_usage("unrecognized parameters: " . | ||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 2 | join(" ", map { "\"$_\"" } keys %unknown), $named); | |||||||||||||||||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 280 | ▶ | elsif ($named && (keys %unknown)) { | |||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$named and keys %unknown
MC/DC50%1/2
$named and keys %unknown
$named
keys %unknown
Truth table66%2/3
A and B
A= $namedB= keys %unknown
| |||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 2 | $named = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 282 | 2 | %vals = ( ); | |||||||||||||||||||||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 27 | ▶ | unless ($named) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 11 | my $i = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 288 | 11 | ▶ | while ($i < @_) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 21 | my $n = $self->{order}->[$i]; | |||||||||||||||||||||||||||||||||||||||||||||
| 290 | 21 | ▶ | unless (defined $n) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 291 | 0 | $self->_usage("too many arguments",$named); | |||||||||||||||||||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 21 | my $truename = $self->{names}->{$n}->{name}; | |||||||||||||||||||||||||||||||||||||||||||||
| 294 | 21 | ▶ | if ($self->{names}->{$truename}->{slurp}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 1 | $vals{$truename} = [ @_[$i..$#_] ]; | |||||||||||||||||||||||||||||||||||||||||||||
| 296 | 1 | $i = $#_; # we don't want to use 'last' | |||||||||||||||||||||||||||||||||||||||||||||
| 297 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 20 | $vals{$truename} = $_[$i]; | |||||||||||||||||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 21 | ▶ | if ($self->{names}->{$truename}->{callback}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 301 | 0 | $@ = undef; | |||||||||||||||||||||||||||||||||||||||||||||
| 302 | 0 | eval { | |||||||||||||||||||||||||||||||||||||||||||||
| 303 | $vals{$truename} = | ||||||||||||||||||||||||||||||||||||||||||||||
| 304 | 0 | $self->_run_callback($truename, $vals{$truename}, \%vals); | |||||||||||||||||||||||||||||||||||||||||||||
| 305 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 306 | 0 | ▶ | $self->_usage($@,$named) if ($@); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 308 | 21 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||||||
| 312 | # validation stage | ||||||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 27 | foreach my $name (keys %{ $self->{names} }) { | |||||||||||||||||||||||||||||||||||||||||||||
| 315 | 74 | my $info = $self->{names}->{$name}; | |||||||||||||||||||||||||||||||||||||||||||||
| 316 | 74 | ▶ | unless (exists($vals{$name})) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 317 | $vals{$name} = $info->{default}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 20 | ▶ | if (($name eq $info->{name}) && (defined $info->{default})); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$name eq $info->{"name"} and defined $info->{"default"}
MC/DC0%0/2
$name eq $info->{"name"} and defined $info->{"default"}
$name eq $info->{"name"}
defined $info->{"default"}
Truth table66%2/3
A and B
A= $name eq $info->{"name"}B= defined $info->{"default"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 74 | ▶ | if ($info->{required} && !exists($vals{$name})) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$info->{"required"} and not exists $vals{$name}
MC/DC0%0/2
$info->{"required"} and not exists $vals{$name}
$info->{"required"}
not exists $vals{$name}
Truth table66%2/3
A and not(B)
A= $info->{"required"}B= not exists $vals{$name}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 0 | $self->_usage("missing required parameter \"$name\"", $named); | |||||||||||||||||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 323 | 74 | ▶ | if (defined $info->{needs}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 324 | # convert a scalar into a list with one element | ||||||||||||||||||||||||||||||||||||||||||||||
| 325 | 4 | ▶ | if (!ref $info->{needs}) { $info->{needs} = [ $info->{needs} ] } | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||||||
| 327 | 4 | foreach my $dep (@{ $info->{needs} }) { | |||||||||||||||||||||||||||||||||||||||||||||
| 328 | 5 | ▶ | unless (exists($vals{$dep})) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 329 | 2 | $self->_usage("missing required parameter \"$dep\" (needed by \"$name\")", $named); | |||||||||||||||||||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||||||
| 336 | 25 | $vals{_named} = $named; | |||||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||||||
| 338 | 25 | return %vals; | |||||||||||||||||||||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||||||
| 342 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||||||
| 344 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||