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