EX/LabeledParam.pm

filter: Statementstmt 67.6% 71 / 105 Branchbran 57.5% 23 / 40 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 70.5% 12 / 17 Podpod 40.0% 4 / 10 totaltotal 60.9% 111 / 182 CC 25 SCAR 24.9
CC
25
Cov
63%
append
30.0
push_list
20.0
load_params
16.3
CRAP 12.0
« EX/Loader.pm ↑ Summary EX/Func.pm »
1 package Getopt::EX::LabeledParam;
2  
3 11 our $VERSION = "3.05";
4  
5 11 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 11 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 our @EXPORT = qw();
11 11 our @EXPORT_OK = qw();
12 11 our %EXPORT_TAGS = ( all => [ @EXPORT_OK ] );
13  
14 11 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 11 ▶use Getopt::EX::Module;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 11 ▶use Getopt::EX::Func qw(parse_func);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
19 2 my $class = shift;
20  
21 2 my $obj = bless {
22 NEWLABEL => 0,
23 CONCAT => "",
24 HASH => {},
25 LIST => [],
26 RESET => undef,
27 }, $class;
28  
29 2 ▶ $obj->configure(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 2 2
30  
31 2 $obj;
32 }
33  
34 2 ▶sub configure {
Subroutine configure: called CC=4 SCAR=14.3
Pod documented
35 2 my $obj = shift;
36 2 ▶ while (@_ >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ >= 2) 6 2 8
37 6 my($k, $v) = splice @_, 0, 2;
38 6 ▶ if ($k =~ /^\w/ and exists $obj->{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k =~ /^\w/u and exists $obj->{$k}) 6 0 6
Condition33%1/3
$k =~ /^\w/u and exists $obj->{$k}
!ll&&!rl&&r
006
MC/DC0%0/2
$k =~ /^\w/u and exists $obj->{$k}
$k =~ /^\w/u exists $obj->{$k}
Truth table33%1/3
A and B
A= $k =~ /^\w/uB= exists $obj->{$k}
ABresult
0X0
100
111
39 6 $obj->{$k} = $v;
40 }
41 }
42 2 $obj;
43 }
44  
45 1 ▶sub get_hash { shift->{HASH} }
Subroutine get_hash: called CC=1 SCAR=0.0
Pod undocumented
46  
47 0 ▶sub set_hash {
Subroutine set_hash: not called CC=1 SCAR=6.9
Pod undocumented
48 0 my $obj = shift;
49 0 %{ $obj->{HASH} } = @_;
50 0 $obj;
51 }
52  
53 0 ▶sub list { @{ shift->{LIST} } }
Subroutine list: not called CC=1 SCAR=6.9
Pod undocumented
54  
55 0 ▶sub push_list {
Subroutine push_list: not called CC=4 SCAR=30.0
Pod undocumented
56 0 my $obj = shift;
57 0 for (@_) {
58 0 ▶ if (defined $obj->{RESET} and $_ eq $obj->{RESET}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $obj->{'RESET'} and $_ eq $obj->{'RESET'}) { } 0 0 0
Condition0%0/3
defined $obj->{"RESET"} and $_ eq $obj->{"RESET"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $obj->{"RESET"} and $_ eq $obj->{"RESET"}
defined $obj->{"RESET"} $_ eq $obj->{"RESET"}
Truth table0%0/3
A and B
A= defined $obj->{"RESET"}B= $_ eq $obj->{"RESET"}
ABresult
0X0
100
111
59 0 @{ $obj->{LIST} } = ();
60 } else {
61 0 push @{ $obj->{LIST} }, $_;
62 }
63 }
64 0 $obj;
65 }
66  
67 0 ▶sub set_list {
Subroutine set_list: not called CC=1 SCAR=6.9
Pod undocumented
68 0 my $obj = shift;
69 0 @{ $obj->{LIST} } = @_;
70 0 $obj;
71 }
72  
73 0 ▶sub append {
Subroutine append: not called CC=5 SCAR=34.0
Pod documented
74 0 my $obj = shift;
75 0 for my $item (@_) {
76 0 ▶ if (ref $item eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $item eq 'ARRAY') { } 0 0 0
elsif (ref $item eq 'HASH') { } 0 0 0
77 0 push @{$obj->{LIST}}, @$item;
78 }
79 elsif (ref $item eq 'HASH') {
80 0 ▶ while (my($k, $v) = each %$item) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$item) 0 0 0
81 0 $obj->{HASH}->{$k} = $v;
82 }
83 }
84 else {
85 0 push @{$obj->{LIST}}, $item;
86 }
87 }
88 }
89  
90 6 ▶sub load_params {
Subroutine load_params: called CC=14 SCAR=27.9
Pod documented
91 6 my $obj = shift;
92  
93 6 my $re_field = qr/[\w\*\?]+/;
94 map {
95 11 my $spec = pop @$_;
96 11 my @spec;
97 11 ▶ while ($spec =~ s/\&([:\w]+ (?: \( [^)]* \) )? ) ;?//x) { # &func
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ s/\&([:\w]+ (?: \( [^)]* \) )? ) ;?//ux) 5 11 16
98 5 push @spec, parse_func({ PACKAGE => 'main' }, $1);
99 }
100 11 ▶ if ($spec =~ s/\b(sub\s*{.*)//) { # sub { ... }
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ s/\b(sub\s*{.*)//u) 2 9 11
101 2 push @spec, parse_func({ PACKAGE => 'main' }, $1);
102 }
103 11 ▶ push @spec, $spec if $spec ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $spec ne "" 7 4 11
104 11 ▶ my $c = @spec > 1 ? [ @spec ] : @spec == 1 ? $spec[0] : "";
Branch: 2 branches
BranchTrueFalseTotal
@spec > 1 ? : 2 9 11
@spec == 1 ? : 9 0 9
105 11 ▶ if (@$_ == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$_ == 0) { } 0 11 11
106 0 $obj->push_list($c);
107 }
108 else {
109 map {
110 22 ▶ if ($c =~ /^\++(.*)/) { # LABEL=+ATTR
Branch: 2 branches
BranchTrueFalseTotal
if ($c =~ /^\++(.*)/u) { } 9 13 22
elsif ($c =~ /^\-+(.*)$/iu) { } 2 11 13
111 9 $obj->{HASH}->{$_} .= $obj->{CONCAT} . "$1";
112 }
113 elsif ($c =~ /^\-+(.*)$/i) { # LABEL=-ATTR
114 2 my $chars = $1 =~ s/(?=\W)/\\/gr;
115 2 $obj->{HASH}->{$_} =~ s/[$chars]+//g;
116 }
117 else {
118 11 $obj->{HASH}->{$_} = $c;
119 }
120 }
121 map {
122 # plain label
123 11 ▶ if (not /\W/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not /\W/u) { } 5 6 11
124 5 ▶ if (exists $obj->{HASH}->{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $obj->{'HASH'}{$_}) { } 5 0 5
125 5 $_;
126 } else {
127 0 ▶ if ($obj->{NEWLABEL}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->{'NEWLABEL'}) { } 0 0 0
128 0 $_;
129 } else {
130 0 warn "$_: Unknown label\n";
131 0 ();
132 }
133 }
134 }
135 # wild card
136 else {
137 6 my @labels = match_glob($_, keys %{$obj->{HASH}});
138 6 ▶ if (@labels == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@labels == 0) 0 6 6
139 0 warn "$_: Unmatched label\n";
140 }
141 6 @labels;
142 }
143 }
144 @$_;
145 }
146 }
147 map {
148 11 ▶ if (my @field = /\G($re_field)=/gp) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@field) = /\G($re_field)=/gpu) { } 11 0 11
149 11 [ @field, ${^POSTMATCH} ];
150 } else {
151 0 [ $_ ];
152 }
153 }
154 map {
155 6 m/( (?: $re_field= )*
156 (?: .* \b sub \s* \{ .*
157 | (?: \([^)]*\) | [^,\s] )+
158 )
159 )/gx;
160 }
161 @_;
162  
163 6 $obj;
164 }
165  
166 6 ▶sub match_glob {
Subroutine match_glob: called CC=1 SCAR=0.0
Pod undocumented
167 6 local $_ = shift;
168 6 s/\?/./g;
169 6 s/\*/.*/g;
170 6 my $regex = qr/^$_$/;
171 6 grep { $_ =~ $regex } @_;
172 }
173  
174 11 1;
175  
176 =head1 NAME
177  
178 Getopt::EX::LabeledParam - Labeled parameter handling
179  
180  
181 =head1 SYNOPSIS
182  
183 GetOptions('colormap|cm:s' => @opt_colormap);
184  
185 # default values
186 my %colormap = ( FILE => 'DR', LINE => 'Y', TEXT => '' );
187 my @colors = qw( /544 /545 /445 /455 /545 /554 );
188  
189 require Getopt::EX::LabeledParam;
190 my $cmap = Getopt::EX::LabeledParam
191 ->new( NEWLABEL => 0,
192 HASH => \%colormap,
193 LIST => \@colors )
194 ->load_params(@opt_colormap);
195  
196  
197 =head1 DESCRIPTION
198  
199 This module implements the super class of L<Getopt::EX::Colormap>.
200  
201 Parameters can be given in two ways: one as a labeled table, and one as an
202 indexed list.
203  
204 The handler maintains hash and list objects, and labeled values are stored
205 in the hash, while non-label values are in the list automatically. Users can mix
206 both specifications.
207  
208 When the value field has a special form of a function call, a
209 L<Getopt::EX::Func> object is created and stored for that entry. See the
210 L<FUNCTION SPEC> section in L<Getopt::EX::Colormap> for more details.
211  
212 =head2 HASH
213  
214 Basically, labeled parameter is defined by B<LABEL>=I<VALUE> notation:
215  
216 FILE=R
217  
218 Definitions can be connected by commas (C<,>):
219  
220 FILE=R,LINE=G
221  
222 Multiple labels can be set for same value:
223  
224 FILE=LINE=TEXT=R
225  
226 Wildcards C<*> and C<?> can be used in label names, and they match
227 existing hash key names. If labels C<OLD_FILE> and C<NEW_FILE> exist
228 in the hash,
229  
230 *FILE=R
231  
232 and
233  
234 OLD_FILE=NEW_FILE=R
235  
236 produces the same result.
237  
238 If the B<VALUE> part starts with a plus (C<+>) character, it is appended to the
239 current value. At this time, the C<CONCAT> string is inserted before the
240 additional string. The default C<CONCAT> string is empty, so use the
241 configure method to set it. If the B<VALUE> part starts with a minus (C<->)
242 character, the following characters are deleted from the current value.
243  
244 =head2 LIST
245  
246 If the B<LABEL>= part is omitted, values are treated as an anonymous list and
247 stored in the list object. For example,
248  
249 R,G,B,C,M,Y
250  
251 makes six entries in the list. The list object is accessed by index
252 rather than by label.
253  
254 =head1 METHODS
255  
256 =over 4
257  
258 =item B<new>
259  
260 =item B<configure>
261  
262 =over 4
263  
264 =item B<HASH> =E<gt> I<hashref>
265  
266 =item B<LIST> =E<gt> I<listref>
267  
268 B<HASH> and B<LIST> references can be set by the B<new> or B<configure>
269 method. You can provide default settings for the hash and list, and it is
270 usually easier to access those values directly rather than through
271 class methods.
272  
273 =item B<NEWLABEL> =E<gt> 0/1
274  
275 By default, B<load_params> does not create a new entry in the hash table,
276 and absent labels are ignored. Setting the B<NEWLABEL> parameter to true makes
277 it possible to create a new hash entry.
278  
279 =item B<CONCAT> =E<gt> I<string>
280  
281 Set the concatenation string inserted before appending a string.
282  
283 =item B<RESET> =E<gt> I<string>
284  
285 Set the B<reset> mark. Undefined by default. If this reset string is
286 found in a list-type argument, the list is reset to empty.
287  
288 =back
289  
290 =item B<load_params> I<option>
291  
292 Load the option list into the object.
293  
294 =item B<append> HASHREF or LIST
295  
296 Provides a simple interface to append a colormap hash or color list. If a
297 hash reference is given, all entries of the hash are appended to the
298 colormap. Otherwise, they are appended to the anonymous color list.
299  
300 =back
301  
302 =head1 SEE ALSO
303  
304 L<Getopt::EX::Colormap>
305  
306 # LocalWords: CONCAT hashref listref NEWLABEL HASHREF colormap
« EX/Loader.pm ↑ Summary EX/Func.pm »