blib/lib/Script/Toolbox/Util/Menues.pm

filter: Statementstmt 5.6% 10 / 177 Branchbran 0.0% 0 / 78 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 6.9% 2 / 29 Podpod 0.0% 0 / 19 totaltotal 3.9% 12 / 308 CC 48 SCAR 31.9
CC
48
Cov
4%
run
56.0
_invalidParam
56.0
getMatching
56.0
CRAP 24.4
« blib/lib/Script/Toolbox/Util/Menus.pm ↑ Summary blib/lib/Script/Toolbox/Util.pm »
1 package Script::Toolbox::Util::Menues;
2  
3 # THIS IS DEPRECATED CODE.
4 # DON'T USE IT.
5 # USE Script::Toolbox::Util::Menus
6  
7 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 21 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 21 require Exporter;
11  
12 21 @ISA = qw(Exporter);
13 # Items to export into callers namespace by default. Note: do not export
14 # names by default without a very good reason. Use EXPORT_OK instead.
15 # Do not simply export all your public functions/methods/constants.
16 21 @EXPORT = qw(
17
18 );
19  
20  
21 # Preloaded methods go here.
22  
23 #-----------------------------------------------------------------------------
24 # {'menueName>' =>[{label=>,value=>,jump=>,argv=>},...]}
25 #-----------------------------------------------------------------------------
26 sub new
27 0 ▶{
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
28 0 my $classname = shift;
29 0 my $self = {};
30 0 bless( $self, $classname );
31 0 $self->_init( @_ );
32 0 return $self;
33 }
34  
35 #-----------------------------------------------------------------------------
36 # {'<menueName>' =>[{label=>,value=>,jump=>,argv=>},...]}
37 #-----------------------------------------------------------------------------
38 sub _init
39 0 ▶{
Subroutine _init: not called CC=2 SCAR=17.9
40 0 my ($self, $newDef) = @_;
41  
42 0 $self->{'def'} = {};
43 0 ▶ return if( ref $newDef ne 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $newDef ne "HASH" 0 0 0
44 0 $self->addMenue($newDef);
45 }
46  
47 #------------------------------------------------------------------------------
48 #------------------------------------------------------------------------------
49 0 ▶sub _getHead($){
Subroutine _getHead: not called CC=4 SCAR=30.0
50 0 my ($self,$def) = @_;
51 0 my $s = '';
52 0 foreach my $k ( @{$def} ) {
53 0 ▶ next if( ! defined $k->{'header'} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k->{"header"} 0 0 0
54 0 $s .= sprintf "%s", $k->{'header'};
55 }
56 0 ▶ return $s ne '' ? $s : undef;
Branch: 1 branch
BranchTrueFalseTotal
$s ne '' ? : 0 0 0
57 }
58 #------------------------------------------------------------------------------
59 #------------------------------------------------------------------------------
60 0 ▶sub _getFoot($){
Subroutine _getFoot: not called CC=4 SCAR=30.0
61 0 my ($self,$def) = @_;
62 0 my $s = '';
63 0 foreach my $k ( @{$def} ) {
64 0 ▶ next if( ! defined $k->{'footer'} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k->{"footer"} 0 0 0
65 0 $s .= sprintf "%s", $k->{'footer'};
66 }
67 0 ▶ return $s ne '' ? $s : undef;
Branch: 1 branch
BranchTrueFalseTotal
$s ne '' ? : 0 0 0
68 }
69  
70 #------------------------------------------------------------------------------
71 # ...
72 # {'label'=>'Call the submenue 1','jump'=>'SubMenue1'}
73 # SubMenue1: is the name of a previous defined menue in the same menues container
74 #------------------------------------------------------------------------------
75 0 ▶sub _resolveSubmenue($){
Subroutine _resolveSubmenue: not called CC=3 SCAR=24.8
76 0 my ($self,$opt) = @_;
77  
78 0 ▶ return if( !defined $opt->{'jump'} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt->{"jump"} 0 0 0
79 0 ▶ return if( ref \$opt->{'jump'} ne 'SCALAR' );
Branch: 1 branch
BranchTrueFalseTotal
if ref \$opt->{"jump"} ne "SCALAR" 0 0 0
80  
81 0 my $subName = $opt->{'jump'};
82 0 $opt->{'jump'} = \&Script::Toolbox::Util::Menues::run;
83 0 $opt->{'argv'} = [$self,$subName];
84 0 return;
85 }
86  
87 #------------------------------------------------------------------------------
88 # [{label=>,value=>,jump=>,argv=>},...]}
89 #------------------------------------------------------------------------------
90 0 ▶sub _getOpts($){
Subroutine _getOpts: not called CC=3 SCAR=24.8
91 0 my ($self,$def) = @_;
92 0 my @s;
93 0 foreach my $k ( @{$def} ) {
94 0 ▶ next if( ! defined $k->{'label'} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k->{"label"} 0 0 0
95 0 $self->_resolveSubmenue($k);
96 0 push @s, $k;
97 }
98 0 return \@s;
99 }
100  
101 #------------------------------------------------------------------------------
102 # <menueName>, [{label=>,value=>,jump=>,argv=>},...]}
103 #------------------------------------------------------------------------------
104 0 ▶sub addMenue($){
Subroutine addMenue: not called CC=3 SCAR=24.8
Pod undocumented
105 0 my ($self,$newDef) = @_;
106  
107 0 ▶ return if( ref $newDef ne 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $newDef ne "HASH" 0 0 0
108  
109 0 foreach my $name ( keys %{$newDef} ){
110 0 my $def = $newDef->{$name};
111 0 $self->{'def'}{$name}{'head'} = $self->_getHead($def);
112 0 $self->{'def'}{$name}{'foot'} = $self->_getFoot($def);
113 0 $self->{'def'}{$name}{'opts'} = $self->_getOpts($def);
114 }
115 0 return;
116 }
117  
118 #------------------------------------------------------------------------------
119 # <menueName>, <HeaderString>
120 #------------------------------------------------------------------------------
121 0 ▶sub setHeader($$){
Subroutine setHeader: not called CC=1 SCAR=6.9
Pod undocumented
122 0 my ($self,$name,$head) = @_;
123  
124 0 $self->{'def'}{$name}{'head'} = $head;
125 0 return;
126 }
127  
128  
129 #------------------------------------------------------------------------------
130 # <menueName>, <HeaderString>
131 #------------------------------------------------------------------------------
132 0 ▶sub setAutoHeader($){
Subroutine setAutoHeader: not called CC=3 SCAR=24.8
Pod undocumented
133 0 my ($self,$name) = @_;
134
135 0 ▶ if( defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) 0 0 0
136 0 $self->{'def'}{$name}{'autohead'} = 1;
137 0 return;
138 }
139 0 foreach my $n (keys %{$self->{'def'}} ){
140 0 $self->{'def'}{$n}{'autohead'} = 1;
141 }
142 }
143  
144 #------------------------------------------------------------------------------
145 #------------------------------------------------------------------------------
146 0 ▶sub _delAutoHead($){
Subroutine _delAutoHead: not called CC=2 SCAR=17.9
147 0 my ($ah) = @_;
148 0 ▶ delete $ah->{'autohead'} if( defined $ah->{'autohead'} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $ah->{"autohead"} 0 0 0
149 }
150  
151 #------------------------------------------------------------------------------
152 # <menueName>, <HeaderString>
153 #------------------------------------------------------------------------------
154 0 ▶sub delAutoHeader($){
Subroutine delAutoHeader: not called CC=3 SCAR=24.8
Pod undocumented
155 0 my ($self,$name) = @_;
156  
157 0 ▶ if( defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) 0 0 0
158 0 _delAutoHead( $self->{'def'}{$name} );
159 0 return;
160 }
161 0 foreach my $n (keys %{$self->{'def'}} ){
162 0 _delAutoHead( $self->{'def'}{$n} );
163 }
164 }
165  
166 #------------------------------------------------------------------------------
167 # <menueName>, <HeaderString>
168 #------------------------------------------------------------------------------
169 0 ▶sub getHeader($){
Subroutine getHeader: not called CC=4 SCAR=30.0
Pod undocumented
170 0 my ($self,$name) = @_;
171  
172 0 my $autoHead = $self->{'def'}{$name}{'autohead'};
173 0 my $H;
174 0 my $h = $self->{'def'}{$name}{'head'};
175 0 ▶ $h = "Menue: $name" if(!defined $h && defined $autoHead);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $h and defined $autoHead 0 0 0
Condition0%0/3
not defined $h and defined $autoHead
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $h and defined $autoHead
not defined $h defined $autoHead
Truth table0%0/3
not(A) and B
A= not defined $hB= defined $autoHead
ABresult
0X0
100
111
176 0 ▶ $H = {'header' => $h} if( defined $h );
Branch: 1 branch
BranchTrueFalseTotal
if defined $h 0 0 0
177  
178 0 return $H;
179 }
180  
181 #------------------------------------------------------------------------------
182 # <menueName>, <FooterString>
183 #------------------------------------------------------------------------------
184 0 ▶sub setFooter($$){
Subroutine setFooter: not called CC=1 SCAR=6.9
Pod undocumented
185 0 my ($self,$name,$foot) = @_;
186  
187 0 $self->{'def'}{$name}{'foot'} = $foot;
188 0 return;
189 }
190  
191 #------------------------------------------------------------------------------
192 # <menueName>, <FooterString>
193 #------------------------------------------------------------------------------
194 0 ▶sub getFooter($){
Subroutine getFooter: not called CC=2 SCAR=17.9
Pod undocumented
195 0 my ($self,$name) = @_;
196  
197 0 my $foot = $self->{'def'}{$name}{'foot'} ;
198 0 ▶ return {'footer'=> $foot} if(defined $foot);
Branch: 1 branch
BranchTrueFalseTotal
if defined $foot 0 0 0
199 0 return undef;
200 }
201  
202 #------------------------------------------------------------------------------
203 # <menueName>, {label=>,value=>,jump=>,argv=>}
204 #------------------------------------------------------------------------------
205 0 ▶sub addOption($$){
Subroutine addOption: not called CC=2 SCAR=17.9
Pod undocumented
206 0 my ($self,$name,$opt) = @_;
207  
208 0 ▶ $self->{'def'}{$name}{'opts'} = [] if( ! defined $self->{'def'}{$name}{'opts'} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"def"}{$name}{"opts"} 0 0 0
209  
210 0 $self->_resolveSubmenue($opt);
211 0 push @{$self->{'def'}{$name}{'opts'}}, $opt;
212 0 return;
213 }
214  
215 #------------------------------------------------------------------------------
216 #------------------------------------------------------------------------------
217 0 ▶sub _getParams($){
Subroutine _getParams: not called CC=3 SCAR=24.8
218 0 my ($self,$name) = @_;
219  
220 0 my @p;
221 0 ▶ my $s = $self->getHeader($name); push @p, $s if( defined $s );
Branch: 1 branch
BranchTrueFalseTotal
if defined $s 0 0 0
222 0 ▶ $s = $self->getFooter($name); push @p, $s if( defined $s );
Branch: 1 branch
BranchTrueFalseTotal
if defined $s 0 0 0
223 0 push @p, {'label'=>'RETURN'};
224 map {
225 0 push @p, $_;
226 0 } @{$self->{'def'}{$name}{'opts'}};
227  
228 0 return \@p;
229 }
230  
231 #------------------------------------------------------------------------------
232 # Validate parameters and rearrange parameters in case of internal menue call
233 # ( submenue call by name).
234 # Return 0 if parameters invalid.
235 #------------------------------------------------------------------------------
236 0 ▶sub validateParams($$){
Subroutine validateParams: not called CC=5 SCAR=34.0
Pod undocumented
237 0 my ($self,$name) = @_;
238 0 ▶ return 0 if( ! defined $$self );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$self 0 0 0
239 0 ▶ if( ref $$self eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $$self eq "ARRAY") 0 0 0
240 0 ▶ return 0 if( ref $$self->[0] ne 'Script::Toolbox::Util::Menues' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $$self->[0] ne "Script::Toolbox::Util::Menues" 0 0 0
241 0 $$name = $$self->[1];
242 0 $$self = $$self->[0];
243 }
244 0 ▶ return 1 if( defined $$self->{'def'}{$$name} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $$self->{"def"}{$$name} 0 0 0
245 0 Script::Toolbox::Util::Log("\nWARNING: Submenue $$name is not defined!");
246 0 sleep 5;
247 0 return 0;
248 }
249  
250 #------------------------------------------------------------------------------
251 # Run the named menue as long as $cnt is true. $cnt will be decremented by each
252 # loop. That means if $cnt starts with 0 we have an endless loop.
253 # Return the number of the last selected option.
254 # The option 'RETURN' will be created automaticly and has option number 0 by
255 # default.
256 #------------------------------------------------------------------------------
257 0 ▶sub run($$){
Subroutine run: not called CC=7 SCAR=40.3
Pod undocumented
258 0 my ($self,$name,$cnt) = @_;
259  
260 0 ▶ return if( ! validateParams(\$self,\$name) );
Branch: 1 branch
BranchTrueFalseTotal
unless &validateParams(\$self, \$name) 0 0 0
261 0 ▶ $cnt = 1 if( ! defined $cnt);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cnt 0 0 0
262 0 ▶ $cnt = 1 if( $cnt !~ /^[-]?\d+$/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $cnt =~ /^[-]?\d+$/ 0 0 0
263 0 ▶ $cnt =-1 if( $cnt == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $cnt == 0 0 0 0
264 0 my $o; my $m;
265 0 ▶ while($cnt--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cnt--) 0 0 0
266 0 my $p = $self->_getParams($name);
267 0 ($o,$m) = Script::Toolbox::Util::Menue($p);
268 0 $self->{'def'}{$name}{'selected'}{'num'} = $o;
269 0 $self->{'def'}{$name}{'selected'}{'opt'} = $m->[$o];
270 0 ▶ return $o if( $o == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $o == 0 0 0 0
271 }
272 0 return $o;
273 }
274  
275 #------------------------------------------------------------------------------
276 # Return current nmber of selected option.
277 #------------------------------------------------------------------------------
278 0 ▶sub currNumber($){
Subroutine currNumber: not called CC=1 SCAR=6.9
Pod undocumented
279 0 my ($self,$name) = @_;
280 0 return $self->{'def'}{$name}{'selected'}{'num'};
281 }
282  
283 #------------------------------------------------------------------------------
284 # Return current label of selected option.
285 #------------------------------------------------------------------------------
286 0 ▶sub currLabel($){
Subroutine currLabel: not called CC=1 SCAR=6.9
Pod undocumented
287 0 my ($self,$name) = @_;
288 0 return $self->{'def'}{$name}{'selected'}{'opt'}{'label'};
289 }
290  
291 #------------------------------------------------------------------------------
292 # Return current value of selected option.
293 #------------------------------------------------------------------------------
294 0 ▶sub currValue($){
Subroutine currValue: not called CC=1 SCAR=6.9
Pod undocumented
295 0 my ($self,$name) = @_;
296 0 return $self->{'def'}{$name}{'selected'}{'opt'}{'value'};
297 }
298  
299 #------------------------------------------------------------------------------
300 # Return the callback address and argv address of selected option.
301 #------------------------------------------------------------------------------
302 0 ▶sub currJump($){
Subroutine currJump: not called CC=1 SCAR=6.9
Pod undocumented
303 0 my ($self,$name) = @_;
304  
305 0 my $call = $self->{'def'}{$name}{'selected'}{'opt'}{'jump'};
306 0 my $args = $self->{'def'}{$name}{'selected'}{'opt'}{'argv'};
307
308 0 return $call,$args;
309 }
310  
311 #------------------------------------------------------------------------------
312 # Set a new label for current selected option. Return old label.
313 #------------------------------------------------------------------------------
314 0 ▶sub setCurrLabel($$){
Subroutine setCurrLabel: not called CC=1 SCAR=6.9
Pod undocumented
315 0 my ($self,$name,$newLabel) = @_;
316  
317 0 my $cn = $self->currNumber($name) -1;
318 0 my $ol = $self->{'def'}{$name}{'opts'}[$cn]{'label'};
319 0 $self->{'def'}{$name}{'opts'}[$cn]{'label'} = $newLabel;
320 0 return $ol;
321 }
322  
323 #------------------------------------------------------------------------------
324 # Set a new value for current selected option. Return old value.
325 #------------------------------------------------------------------------------
326 0 ▶sub setCurrValue($$){
Subroutine setCurrValue: not called CC=1 SCAR=6.9
Pod undocumented
327 0 my ($self,$name,$newValue) = @_;
328  
329 0 my $cn = $self->currNumber($name) -1;
330 0 my $ov = $self->{'def'}{$name}{'opts'}[$cn]{'value'};
331 0 $self->{'def'}{$name}{'opts'}[$cn]{'value'} = $newValue;
332 0 return $ov;
333 }
334  
335 #------------------------------------------------------------------------------
336 # Set new callback address und argv for the current selected option.
337 #------------------------------------------------------------------------------
338 0 ▶sub setCurrJump($$$){
Subroutine setCurrJump: not called CC=1 SCAR=6.9
Pod undocumented
339 0 my ($self,$name,$callBack,$argv) = @_;
340  
341 0 my $cn = $self->currNumber($name) -1;
342 0 $self->{'def'}{$name}{'opts'}[$cn]{'jump'} = $callBack;
343 0 $self->{'def'}{$name}{'opts'}[$cn]{'argv'} = $argv;
344 0 return $callBack,$argv;
345 }
346  
347 #------------------------------------------------------------------------------
348 #------------------------------------------------------------------------------
349 0 ▶sub _invalidParam($$$$$){
Subroutine _invalidParam: not called CC=7 SCAR=40.3
350 0 my ($self,$name,$pattern,$search,$return) = @_;
351 0 ▶ return 1 if( !defined $pattern );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pattern 0 0 0
352 0 ▶ return 1 if( !defined $search );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $search 0 0 0
353 0 ▶ return 1 if( !defined $return );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $return 0 0 0
354 0 ▶ return 1 if( !defined $self->{'def'}{$name}{'opts'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"def"}{$name}{"opts"} 0 0 0
355 0 ▶ return 1 if( $search !~ /(number|value|label)/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $search =~ /(number|value|label)/ 0 0 0
356 0 ▶ return 1 if( $return !~ /(number|value|label)/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $return =~ /(number|value|label)/ 0 0 0
357 0 return 0;
358 }
359  
360 #------------------------------------------------------------------------------
361 # Search the labels array for $pattern matching in $search. If matching return
362 # value of type return.
363 # $pattern='[Mm]ax' $search='value' $return='label'
364 # => returns all labels where value column matching Max or max.
365 # search: /(label,number,value)/
366 # return: /(label,number,value)/
367 #------------------------------------------------------------------------------
368 0 ▶sub getMatching($$$$){
Subroutine getMatching: not called CC=7 SCAR=40.3
Pod undocumented
369 0 my ($self,$name,$pattern,$search,$return) = @_;
370 0 ▶ return '' if( _invalidParam($self,$name,$pattern,$search,$return));
Branch: 1 branch
BranchTrueFalseTotal
if &_invalidParam($self, $name, $pattern, $search, $return) 0 0 0
371  
372 0 my $L = $self->{'def'}{$name}{'opts'};
373 0 my @R;
374 0 my $i=1;
375 0 foreach my $l ( @{$L} ){
376 0 ▶ if( $search eq 'number' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($search eq 'number') { } 0 0 0
377 0 ▶ push @R, $l->{$return} if( $i =~ /$pattern/);
Branch: 1 branch
BranchTrueFalseTotal
if $i =~ /$pattern/ 0 0 0
378 0 $i++;
379 }else{
380 0 ▶ next if( !defined $l->{$search} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $l->{$search} 0 0 0
381 0 ▶ next if( $l->{$search} !~ /$pattern/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $l->{$search} =~ /$pattern/ 0 0 0
382 0 push @R, $l->{$return};
383 }
384 }
385 0 return \@R;
386 }
387  
388  
389 21 1;
390 __END__
« blib/lib/Script/Toolbox/Util/Menus.pm ↑ Summary blib/lib/Script/Toolbox/Util.pm »