blib/lib/Getopt/EX/Hashed.pm

filter: Statementstmt 90.7% 117 / 129 Branchbran 76.1% 32 / 42 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 86.9% 20 / 23 Podpod 100% 7 / 7 totaltotal 86.1% 180 / 209 CC 29 SCAR 15.7
CC
29
Cov
86%
_compile
9.2
optspec
7.0
new
6.1
CRAP 4.8
↑ Summary Build »
1 package Getopt::EX::Hashed;
2  
3 6 our $VERSION = '0.9907';
4  
5 =head1 NAME
6  
7 Getopt::EX::Hashed - Hash store object automation
8  
9 =head1 VERSION
10  
11 Version 0.9907
12  
13 =head1 SYNOPSIS
14  
15 use App::foo;
16 App::foo->new->run();
17  
18 package App::foo;
19  
20 use Getopt::EX::Hashed;
21 has start => ( spec => "=i s begin", default => 1 );
22 has end => ( spec => "=i e" );
23 no Getopt::EX::Hashed;
24  
25 sub run {
26 my $app = shift;
27 use Getopt::Long;
28 $app->getopt or pod2usage();
29 if ($app->start) {
30 ...
31  
32 =head1 DESCRIPTION
33  
34 B<Getopt::EX::Hashed> is a module to automate a hash object to store
35 command line option values. Major objective of this module is
36 integrating initialization and specification into single place.
37 Module name shares B<Getopt::EX>, but it works independently from
38 other modules included in B<Getopt::EX>, so far.
39  
40 In the current implementation, using B<Getopt::Long>, or compatible
41 module such as B<Getopt::EX::Long> is assumed. It is configurable,
42 but no other module is supported now.
43  
44 Accessor methods are automatically generated.
45  
46 =head1 FUNCTION
47  
48 =head2 B<has>
49  
50 Declare option parameters in a form of:
51  
52 has option_name => ( param => value, ... );
53  
54 If array reference is given, multiple names can be declared at once.
55  
56 has [ 'left', 'right' ] => ( spec => "=i" );
57  
58 If the name start with plus (C<+>), given parameters are added to
59 current value.
60  
61 has '+left' => ( default => 1 );
62  
63 Following parameters are available.
64  
65 =over 7
66  
67 =item B<spec> => I<string>
68  
69 Give option specification. Option spec and alias names are separated
70 by white space, and can show up in any order.
71  
72 Declaration
73  
74 has start => ( spec => "=i s begin" );
75  
76 will be compiled into string:
77  
78 start|s|begin=i
79  
80 which conform to C<Getopt::Long> definition. Of course, you can write
81 as this:
82  
83 has start => ( spec => "s|begin=i" );
84  
85 If the name and aliases contain underscore (C<_>), another alias name
86 is defined with dash (C<->) in place of underscores. So
87  
88 has a_to_z => ( spec => "=s" );
89  
90 will be compiled into:
91  
92 a_to_z|a-to-z:s
93  
94 If nothing special is necessary, give empty (or white space only)
95 string as a value. Otherwise, it is not considered as an option.
96  
97 =item B<alias> => I<string>
98  
99 Additional alias names can be specified by B<alias> parameter too.
100 There is no difference with ones in B<spec> parameter.
101  
102 =item B<default> => I<value>
103  
104 Set default value. If no default is given, the member is initialized
105 as C<undef>.
106  
107 =item B<action> => I<coderef>
108  
109 Parameter B<action> takes code reference which called to process the
110 option. When called, hash object is passed through C<$_>.
111  
112 has [ qw(left right both) ] => spec => '=i';
113 has "+both" => action => sub {
114 $_->{left} = $_->{right} = $_[1];
115 };
116  
117 You can use this for C<< "<>" >> too. In that case, spec parameter
118 does not matter and not required.
119  
120 has ARGV => default => [];
121 has "<>" => action => sub {
122 push @{$_->{ARGV}}, $_[0];
123 };
124  
125 In fact, B<default> parameter takes code reference too. It is stored
126 in the hash object and the code works almost same. But the hash value
127 can not be used for option storage.
128  
129 Because B<action> function intercept the option assignment, it can be
130 used to verify the parameter.
131  
132 has age =>
133 spec => '=i',
134 action => sub {
135 my($name, $i) = @_;
136 (0 <= $i and $i <= 150) or
137 die "$name: have to be in 0 to 150 range.\n";
138 $_->{$name} = $i;
139 };
140  
141 =back
142  
143 =head1 METHOD
144  
145 =over 7
146  
147 =item B<new>
148  
149 Class method to get initialized hash object.
150  
151 =item B<configure>
152  
153 There should be some configurable variables, but not fixed yet.
154  
155 =item B<getopt>
156  
157 Call C<GetOptions> function defined in caller's context with
158 appropriate parameters.
159  
160 $obj->getopt
161  
162 is just a shortcut for:
163  
164 GetOptions($obj->optspec)
165  
166 =item B<optspec>
167  
168 Return option specification list which can be given to C<GetOptions>
169 function. GetOptions has a capability of storing values in a hash, by
170 giving the hash reference as a first argument, but it is not expected.
171  
172 =item B<use_keys>
173  
174 Because hash keys are protected by C<Hash::Util::lock_keys>, accessing
175 non-existing member causes an error. Use this function to declare new
176 member key before use.
177  
178 $obj->use_keys( qw(foo bar) );
179  
180 If you want to access arbitrary keys, unlock the object.
181  
182 use Hash::Util 'unlock_keys';
183 unlock_keys %{$obj};
184  
185 =item B<reset>
186  
187 Reset the class to original state. Because the hash object keeps all
188 information, this does not effect to the existing object. It returns
189 the object itself, so you can reset the class after creating a object
190 like this:
191  
192 my $obj = Getopt::EX::Hashed->new->reset;
193  
194 This is almost equivalent to the next code:
195  
196 my $obj = Getopt::EX::Hashed->new;
197 Getopt::EX::Hashed->reset;
198  
199 =back
200  
201 =head1 SEE ALSO
202  
203 L<Getopt::Long>
204  
205 L<Getopt::EX>, L<Getopt::EX::Long>
206  
207 =head1 AUTHOR
208  
209 Kazumasa Utashiro
210  
211 =head1 COPYRIGHT
212  
213 The following copyright notice applies to all the files provided in
214 this distribution, including binary files, unless explicitly noted
215 otherwise.
216  
217 Copyright 2021 Kazumasa Utashiro
218  
219 =head1 LICENSE
220  
221 This library is free software; you can redistribute it and/or modify
222 it under the same terms as Perl itself.
223  
224 =cut
225  
226 6 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
227 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
228 6 ▶use Hash::Util qw(lock_keys lock_keys_plus unlock_keys);
Subroutine BEGIN: called CC=1 SCAR=0.0
229 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
230 6 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
231  
232 6 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
233 6 our @EXPORT = qw(has new);
234  
235 6 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
236  
237 6 my @Member;
238  
239 6 my %Config = (
240 DEBUG_PRINT => 0,
241 LOCK_KEYS => 1,
242 REPLACE_UNDERSCORE => 1,
243 RESET_AFTER_NEW => 0,
244 GETOPT => 'GetOptions',
245 ACCESSOR => 1,
246 ACCESSOR_PREFIX => '',
247 );
248 6 lock_keys %Config;
249  
250 sub configure {
251 0 ▶ my $obj = shift;
Subroutine configure: not called CC=2 SCAR=17.9
Pod documented
252 0 ▶ while (my($key, $value) = splice @_, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 0 0 0
253 0 $Config{$key} = $value;
254 }
255 0 return $obj;
256 }
257  
258 sub unimport {
259 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
260 4 ▶ my $caller = caller;
Subroutine unimport: called CC=1 SCAR=0.0
261 4 delete ${"$caller\::"}{has};
262 }
263  
264 sub reset {
265 0 ▶ @Member = ();
Subroutine reset: not called CC=1 SCAR=6.9
Pod documented
266 0 return $_[0];
267 }
268  
269 sub has {
270 69 ▶ my($key, @param) = @_;
Subroutine has: called CC=6 SCAR=17.9
Pod documented
271 69 ▶ my @name = ref $key eq 'ARRAY' ? @$key : $key;
Branch: 1 branch
BranchTrueFalseTotal
ref $key eq 'ARRAY' ? : 5 64 69
272 69 for my $name (@name) {
273 78 my $append = $name =~ s/^\+//;
274 78 ▶ my $i = first { $Member[$_]->[0] eq $name } 0 .. $#Member;
Subroutine __ANON__: called CC=1 SCAR=0.0
275 78 ▶ if ($append) {
Branch: 1 branch
BranchTrueFalseTotal
if ($append) { } 5 73 78
276 5 ▶ defined $i or die "$name: Not found\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $i or die "${name}: Not found\n" 1 4 5
277 4 push @{$Member[$i]}, @param;
278 } else {
279 73 ▶ defined $i and die "$name: Duplicated\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $i and die "${name}: Duplicated\n" 0 73 73
280 73 push @Member, [ $name, @param ];
281 }
282 }
283 }
284  
285 sub new {
286 4 ▶ my $class = shift;
Subroutine new: called CC=6 SCAR=18.0
Pod documented
287 4 my $obj = bless {}, __PACKAGE__;
288 4 ▶ our @ISA = $class if $class ne __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if $class ne __PACKAGE__ 3 1 4
289 my $member = $obj->{__Hash__} = {
290 map {
291 4 my($key, %param) = @$_;
292 57 $key => \%param;
293 } @Member
294 };
295 4 my $order = $obj->{__Order__} = [ map $_->[0], @Member ];
296 4 for my $key (@{$order}) {
297 57 my $m = $member->{$key};
298 57 $obj->{$key} = $m->{default};
299 57 ▶ if ($Config{ACCESSOR}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Config{"ACCESSOR"}) 57 0 57
300 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
301 57 my $prefix = $Config{ACCESSOR_PREFIX};
302 57 *{"$class\::$prefix$key"} = sub {
303 $#_ ? $_[0]{$key} = $_[1]
304 13 ▶ : $_[0]{$key};
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$#_ ? : 0 13 13
305 57 };
306 }
307 }
308 4 ▶ lock_keys %{$obj} if $Config{LOCK_KEYS};
Branch: 1 branch
BranchTrueFalseTotal
if $Config{"LOCK_KEYS"} 4 0 4
309 4 ▶ __PACKAGE__->reset if $Config{RESET_AFTER_NEW};
Branch: 1 branch
BranchTrueFalseTotal
if $Config{"RESET_AFTER_NEW"} 0 4 4
310 4 $obj;
311 }
312  
313 sub optspec {
314 4 ▶ my $obj = shift;
Subroutine optspec: called CC=7 SCAR=19.5
Pod documented
315 4 my $member = $obj->{__Hash__};
316 4 my @optlist = do {
317 map {
318 52 my($name, $spec) = @$_;
319 52 my $compiled = _compile($obj, $name, $spec);
320 52 my $m = $member->{$name};
321 52 my $dest = do {
322 52 ▶ if (my $action = $m->{action}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $action = $m->{'action'}) { } 5 47 52
323 5 ▶ ref $action eq 'CODE' or
Branch: 1 branch
BranchTrueFalseTotal
ref $action eq "CODE" or die "${name}: action must be coderef.\n" 0 5 5
324 die "$name: action must be coderef.\n";
325 5 ▶ sub { &$action for $obj };
Subroutine __ANON__: called CC=2 SCAR=6.9
326 } else {
327 47 ▶ if (ref $obj->{$name} eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj->{$name} eq 'CODE') { } 5 42 47
328 5 ▶ sub { &{$obj->{$name}} for $obj };
Subroutine __ANON__: called CC=2 SCAR=6.9
329 } else {
330 42 \$obj->{$name};
331 }
332 }
333 };
334 52 $compiled => $dest;
335 }
336 # spec .= alias
337 map {
338 52 ▶ if (my $alias = $member->{$_->[0]}->{alias}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $alias = $$member{$_->[0]}{"alias"}) 3 49 52
339 3 $_->[1] .= " $alias";
340 }
341 52 $_;
342 }
343 # spec = '' if $name eq = '<>'
344 grep {
345 57 ▶ $_->[0] eq '<>' and $_->[1] //= '';
Branch: 1 branch
BranchTrueFalseTotal
$_->[0] eq "<>" and $_->[1] //= "" 2 55 57
Condition50%1/2
$_->[1] //= ""
l!l
02
MC/DC0%0/1
$_->[1] //= ""
$_->[1]
Truth table50%1/2
A
A= $_->[1]
Aresult
11
00
346 57 defined $_->[1];
347 }
348 # get spec
349 57 map { [ $_ => $member->{$_}->{spec} ] }
350 4 @{$obj->{__Order__}};
351 };
352 4 @optlist;
353 }
354  
355 6 my $spec_re = qr/[!+=:]/;
356  
357 sub _compile {
358 52 ▶ my $obj = shift;
Subroutine _compile: called CC=9 SCAR=22.1
359 52 my($name, $args) = @_;
360  
361 52 ▶ return $name if $name eq '<>';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "<>" 2 50 52
362  
363 50 my @args = split ' ', $args;
364 50 my @spec = grep /$spec_re/, @args;
365 50 my $spec = do {
366 50 ▶ if (@spec == 0) { '' }
Branch: 2 branches
BranchTrueFalseTotal
if (@spec == 0) { } 6 44 50
elsif (@spec == 1) { } 44 0 44
367 44 elsif (@spec == 1) { $spec[0] }
368 0 else { die }
369 };
370 50 my @alias = grep !/$spec_re/, @args;
371 50 my @names = ($name, @alias);
372 50 ▶ if ($Config{REPLACE_UNDERSCORE}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Config{"REPLACE_UNDERSCORE"}) 50 0 50
373 50 for ($name, @alias) {
374 65 ▶ push @names, tr[_][-]r if /_/;
Branch: 1 branch
BranchTrueFalseTotal
if /_/u 3 62 65
375 }
376 }
377 50 ▶ push @names, '' if @names and $spec !~ /^($spec_re|$)/;
Branch: 1 branch
BranchTrueFalseTotal
if @names and not $spec =~ /^($spec_re|$)/u 3 47 50
Condition66%2/3
@names and not $spec =~ /^($spec_re|$)/u
!ll&&!rl&&r
0473
MC/DC50%1/2
@names and not $spec =~ /^($spec_re|$)/u
@names not $spec =~ /^($spec_re|$)/u
Truth table66%2/3
A and not(B)
A= @namesB= not $spec =~ /^($spec_re|$)/u
ABresult
0X0
100
111
378 50 join('|', @names) . $spec;
379 }
380  
381 sub getopt {
382 3 ▶ my $obj = shift;
Subroutine getopt: called CC=1 SCAR=0.0
Pod documented
383 3 my $getopt = caller . "::" . $Config{GETOPT};
384 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
385 3 &{$getopt}($obj->optspec);
386 }
387  
388 sub use_keys {
389 0 ▶ my $obj = shift;
Subroutine use_keys: not called CC=1 SCAR=6.9
Pod documented
390 0 unlock_keys %{$obj};
391 0 lock_keys_plus %{$obj}, @_;
392 }
393  
394 6 1;
↑ Summary Build »