Quiq/Section/Object.pm

filter: Statementstmt 28.5% 58 / 203 Branchbran 18.2% 15 / 82 Conditioncond 3.1% 1 / 32 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 35.5% 16 / 45 Podpod 100% 36 / 36 totaltotal 30.2% 126 / 416 CC 58 SCAR 31.6
CC
58
Cov
23%
validate
90.0
getBool
72.0
getArray
56.0
CRAP 23.6
« Quiq/Option.pm ↑ Summary Quiq/Html/Widget/CheckBox.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Section::Object - Abschnitts-Objekt
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Object>
12  
13 =head1 DESCRIPTION
14  
15 Ein Objekt der Klasse repräsentiert einen "Abschnitt". Abschnitte werden
16 von C<< Quiq::Section::Parser >> geparst und instantiiert.
17  
18 Ein Abschnittsobjekt gibt Auskunft über den Inhalt des Abschnitts
19 und dessen "Ort" (Dateiname und Zeilennummer). Das Objekt ist readonly,
20 d.h. die Objekteigenschaften können gelesen aber nicht gesetzt werden.
21  
22 Abschnittsobjekte können in einer hierarchischen Beziehung stehen.
23 Ein Abschnitts-Objekt kann Unter-Abschnitte haben.
24  
25 =head1 ATTRIBUTES
26  
27 =over 4
28  
29 =item [0] type
30  
31 Abschnitts-Bezeichner.
32  
33 =item [1] brackets
34  
35 Klammerung des Abschnittsbezeichners.
36  
37 =item [2] keyA
38  
39 Liste der Schlüssel.
40  
41 =item [3] keyValH
42  
43 Attribut-Hash.
44  
45 =item [4] content
46  
47 Inhalt des Abschnitts.
48  
49 =item [5] source
50  
51 Quelltext des Abschnitts.
52  
53 =item [6] file
54  
55 Name der Quelldatei.
56  
57 =item [7] line
58  
59 Zeilennummer in Quelldatei.
60  
61 =back
62  
63 =cut
64  
65 # -----------------------------------------------------------------------------
66  
67 package Quiq::Section::Object;
68 6 ▶use base qw/Quiq::Object/;
Subroutine BEGIN: called CC=1 SCAR=0.0
69  
70 6 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 6 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
74  
75 6 our $VERSION = '1.240';
76  
77 6 ▶use Quiq::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
78  
79 # -----------------------------------------------------------------------------
80  
81 =head1 METHODS
82  
83 =head2 Konstruktor
84  
85 =head3 new() - Konstruktor
86  
87 =head4 Synopsis
88  
89 $sec = $class->new($type,$keyValH);
90 $sec = $class->new($type,$keyValH,$keyA,$content,$source,$file,$line);
91  
92 =head4 Arguments
93  
94 =over 4
95  
96 =item $type
97  
98 Abschnitts-Typ einschließlich Klammern (sofern vorhanden).
99  
100 =item $keyValH
101  
102 Referenz auf Schlüssel/Wert-Hash.
103  
104 =item $keyA
105  
106 Referenz auf Schlüssel-Array.
107  
108 =item $content
109  
110 Inhalt.
111  
112 =item $source
113  
114 Quelltext des Abschnitts.
115  
116 =item $file
117  
118 Name der Datei, die den Abschnitt enthält. Im Falle von STDIN ist
119 ist der Dateiname "-", im Falle einer In-Memory-Datei ist der
120 Dateiname "C<(source)>".
121  
122 =item $line
123  
124 Zeilennummer, an der der Abschnitt in der Datei beginnt.
125  
126 =back
127  
128 =head4 Returns
129  
130 Referenz auf Abschnitts-Objekt
131  
132 =head4 Description
133  
134 Instantiiere ein Abschnittsobjekt und liefere eine Referenz
135 auf das Objekt zurück.
136  
137 =cut
138  
139 # -----------------------------------------------------------------------------
140  
141 8 ▶sub new {
Subroutine new: called CC=7 SCAR=23.0
Pod documented
142 8 my ($class,$type,$keyValH,$keyA,$content,$source,$file,$line) = @_;
143  
144 # Abschnittsbezeichner in eigentlichen Bezeichner und
145 # Klammerung zerlegen
146  
147 8 my $brackets = '';
148 8 ▶ if ($type =~ /^(\W)(\w+)(\W)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /^(\W)(\w+)(\W)$/) 8 0 8
149 8 $type = $2;
150 8 $brackets = "$1$3";
151 }
152  
153 8 ▶ if (!defined $keyA) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $keyA) 0 8 8
154 0 $keyA = [keys %$keyValH];
155 }
156  
157 8 ▶ if (!defined $content) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $content) 0 8 8
158 0 $content = '';
159 }
160 8 ▶ if (!defined $source) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $source) 0 8 8
161 0 $source = '';
162 }
163 8 ▶ if (!defined $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 0 8 8
164 0 $file = '(internal)';
165 }
166 8 ▶ if (!defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 0 8 8
167 0 $line = 0;
168 }
169  
170 # Objekt instantiieren
171  
172 8 return bless [
173 $type, # [0]
174 $brackets, # [1]
175 $keyA, # [2]
176 $keyValH, # [3]
177 $content, # [4]
178 $source, # [5]
179 $file, # [6]
180 $line, # [7]
181 ],$class;
182 }
183  
184 # -----------------------------------------------------------------------------
185  
186 =head2 Abschnittsinformation
187  
188 =head3 type() - Liefere/Setze Abschnittsbezeichner
189  
190 =head4 Synopsis
191  
192 $type = $sec->type;
193 $type = $sec->type($type);
194  
195 =head4 Returns
196  
197 Abschnittsbezeichner (String)
198  
199 =head4 Description
200  
201 Liefere den Abschnittsbezeichner. Ist Parameter $type angegeben, setze
202 den Abschnittsbezeichner auf diesen Wert.
203  
204 =cut
205  
206 # -----------------------------------------------------------------------------
207  
208 4 ▶sub type {
Subroutine type: called CC=2 SCAR=7.6
Pod documented
209 4 my $self = shift;
210 # @_: $type
211  
212 4 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 4 4
213 0 $self->[0] = shift;
214 }
215
216 4 return $self->[0];
217 }
218  
219 # -----------------------------------------------------------------------------
220  
221 =head3 brackets() - Liefere/setze Klammerung
222  
223 =head4 Synopsis
224  
225 $brackets = $sec->brackets;
226 $brackets = $sec->brackets($brackets);
227  
228 =head4 Returns
229  
230 Klammerpaar (String)
231  
232 =head4 Description
233  
234 Liefere die Klammerung um den Abschnittsbezeichner, sofern vorhanden.
235 Ist der Abschnittsbezeichner nicht geklammert, liefere einen Leerstring.
236 Ist Parameter $brackets angegeben, setze die Klammerung auf diesen Wert.
237  
238 =head4 Details
239  
240 Die Klamerung um den Abschnitts-Bezeichner ist optional.
241 Sie besteht aus der öffnenden und schließenden Klammer,
242 ist also "[]", "<>" oder "()" oder "{}".
243  
244 =cut
245  
246 # -----------------------------------------------------------------------------
247  
248 4 ▶sub brackets {
Subroutine brackets: called CC=2 SCAR=7.6
Pod documented
249 4 my $self = shift;
250 # @_: $brackets
251  
252 4 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 4 4
253 0 $self->[1] = shift;
254 }
255
256 4 return $self->[1];
257 }
258  
259 # -----------------------------------------------------------------------------
260  
261 =head3 fullType() - Liefere Abschnittsbezeichner mit Klammerung
262  
263 =head4 Synopsis
264  
265 $fullType = $sec->fullType;
266  
267 =head4 Description
268  
269 Liefere den vollständigen Abschnittsbezeichner einschließlich
270 der Klammern.
271  
272 =cut
273  
274 # -----------------------------------------------------------------------------
275  
276 0 ▶sub fullType {
Subroutine fullType: not called CC=1 SCAR=6.9
Pod documented
277 0 my $self = shift;
278 0 return substr($self->[1],0,1).$self->[0].substr($self->[1],1,1);
279 }
280  
281 # -----------------------------------------------------------------------------
282  
283 =head3 keys() - Liefere die Liste der Schlüssel
284  
285 =head4 Synopsis
286  
287 $keyA|@keys = $sec->keys;
288  
289 =head4 Description
290  
291 Liefere die Liste der Schlüssel. Im Skalarkontext liefere eine
292 Referenz auf die Liste.
293  
294 =cut
295  
296 # -----------------------------------------------------------------------------
297  
298 0 ▶sub keys {
Subroutine keys: not called CC=2 SCAR=17.9
Pod documented
299 0 my $self = shift;
300 0 ▶ return wantarray? @{$self->[2]}: $self->[2];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
301 }
302  
303 # -----------------------------------------------------------------------------
304  
305 =head3 keyValHash() - Liefere den Attributhash
306  
307 =head4 Synopsis
308  
309 $hash|@arr = $sec->keyValHash;
310  
311 =head4 Returns
312  
313 Im Sklarkontext liefere eine Referenz auf den Attribut-Hash.
314 Im Arraykontext liefere die Liste der Attribut/Wert-Paare.
315  
316 =cut
317  
318 # -----------------------------------------------------------------------------
319  
320 1 ▶sub keyValHash {
Subroutine keyValHash: called CC=2 SCAR=8.1
Pod documented
321 1 my $self = shift;
322 0 ▶ return wantarray? %{$self->[3]}: $self->[3];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
323 }
324  
325 # -----------------------------------------------------------------------------
326  
327 =head3 content() - Liefere/Setze Inhalt
328  
329 =head4 Synopsis
330  
331 $content = $sec->content;
332 $content = $sec->content($content);
333  
334 =head4 Returns
335  
336 Inhalt (String)
337  
338 =cut
339  
340 # -----------------------------------------------------------------------------
341  
342 8 ▶sub content {
Subroutine content: called CC=2 SCAR=7.6
Pod documented
343 8 my $self = shift;
344  
345 8 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 8 8
346 0 $self->[4] = shift;
347 }
348  
349 8 return $self->[4];
350 }
351  
352 # -----------------------------------------------------------------------------
353  
354 =head3 contentRef() - Liefere Referenz auf den Inhalt
355  
356 =head4 Synopsis
357  
358 $ref = $sec->contentRef;
359  
360 =head4 Returns
361  
362 Referenz auf Inhalt (String-Referenz)
363  
364 =cut
365  
366 # -----------------------------------------------------------------------------
367  
368 0 ▶sub contentRef {
Subroutine contentRef: not called CC=1 SCAR=6.9
Pod documented
369 0 return \shift->[4];
370 }
371  
372 # -----------------------------------------------------------------------------
373  
374 =head3 contentNL() - Liefere Inhalt mit Newline
375  
376 =head4 Synopsis
377  
378 $contentN = $sec->contentNL;
379  
380 =head4 Returns
381  
382 Inhalt mit Newline (String)
383  
384 =cut
385  
386 # -----------------------------------------------------------------------------
387  
388 0 ▶sub contentNL {
Subroutine contentNL: not called CC=2 SCAR=17.9
Pod documented
389 0 my $self = shift;
390 0 ▶ return $self->[4] eq ''? '': $self->[4]."\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->[4] eq '' ? : 0 0 0
391 }
392  
393 # -----------------------------------------------------------------------------
394  
395 =head3 file() - Liefere Dateiname
396  
397 =head4 Synopsis
398  
399 $file = $sec->file;
400  
401 =head4 Returns
402  
403 Dateiname (String)
404  
405 =cut
406  
407 # -----------------------------------------------------------------------------
408  
409 0 ▶sub file {
Subroutine file: not called CC=1 SCAR=6.9
Pod documented
410 0 return shift->[6];
411 }
412  
413 # -----------------------------------------------------------------------------
414  
415 =head3 mtime() - Liefere Zeitpunkt der letzten Änderung der Datei
416  
417 =head4 Synopsis
418  
419 $mtime = $sec->mtime;
420  
421 =head4 Returns
422  
423 Integer (Epoch-Wert)
424  
425 =cut
426  
427 # -----------------------------------------------------------------------------
428  
429 0 ▶sub mtime {
Subroutine mtime: not called CC=3 SCAR=24.8
Pod documented
430 0 my $self = shift;
431  
432 0 my $file = $self->file;
433 0 ▶ if ($file eq '-' && $file eq '(source)') {
Branch: 1 branch
BranchTrueFalseTotal
if ($file eq "-" and $file eq "(source)") 0 0 0
Condition0%0/3
$file eq "-" and $file eq "(source)"
!ll&&!rl&&r
000
MC/DC0%0/2
$file eq "-" and $file eq "(source)"
$file eq "-" $file eq "(source)"
Truth table0%0/3
A and B
A= $file eq "-"B= $file eq "(source)"
ABresult
0X0
100
111
434 0 return 0;
435 }
436
437 0 return Quiq::Path->mtime($file);
438 }
439  
440 # -----------------------------------------------------------------------------
441  
442 =head3 line() - Liefere Zeilennummer
443  
444 =head4 Synopsis
445  
446 $n = $sec->line;
447  
448 =head4 Returns
449  
450 Zeilennummer (Integer)
451  
452 =cut
453  
454 # -----------------------------------------------------------------------------
455  
456 0 ▶sub line {
Subroutine line: not called CC=1 SCAR=6.9
Pod documented
457 0 return shift->[7];
458 }
459  
460 # -----------------------------------------------------------------------------
461  
462 =head3 fileInfo() - Liefere Dateiname und Zeilennummer in einem Aufruf
463  
464 =head4 Synopsis
465  
466 ($file,$line) = $sec->fileInfo;
467  
468 =head4 Returns
469  
470 Dateiname (String) und Zeilennummer (Integer)
471  
472 =cut
473  
474 # -----------------------------------------------------------------------------
475  
476 4 ▶sub fileInfo {
Subroutine fileInfo: called CC=1 SCAR=0.0
Pod documented
477 4 my $self = shift;
478 4 return ($self->[6],$self->[7]);
479 }
480  
481 # -----------------------------------------------------------------------------
482  
483 =head2 Quelltext
484  
485 =head3 source() - Liefere Quelltext
486  
487 =head4 Synopsis
488  
489 $source = $sec->source;
490  
491 =head4 Returns
492  
493 Quelltext (String)
494  
495 =cut
496  
497 # -----------------------------------------------------------------------------
498  
499 7 ▶sub source {
Subroutine source: called CC=1 SCAR=0.0
Pod documented
500 7 return shift->[5];
501 }
502  
503 # -----------------------------------------------------------------------------
504  
505 =head3 sourceRef() - Liefere Referenz auf Quelltext
506  
507 =head4 Synopsis
508  
509 $ref = $sec->sourceRef;
510  
511 =head4 Returns
512  
513 Skalar-Referenz
514  
515 =cut
516  
517 # -----------------------------------------------------------------------------
518  
519 0 ▶sub sourceRef {
Subroutine sourceRef: not called CC=1 SCAR=6.9
Pod documented
520 0 return \shift->[5];
521 }
522  
523 # -----------------------------------------------------------------------------
524  
525 =head3 deleteSource() - Lösche Quelltext
526  
527 =head4 Synopsis
528  
529 $sec->deleteSource;
530  
531 =head4 Returns
532  
533 nichts
534  
535 =cut
536  
537 # -----------------------------------------------------------------------------
538  
539 0 ▶sub deleteSource {
Subroutine deleteSource: not called CC=1 SCAR=6.9
Pod documented
540 0 shift->[5] = undef;
541 0 return;
542 }
543  
544 # -----------------------------------------------------------------------------
545  
546 =head3 transferSource() - Übertrage Quelltext von Sub-Abschnitten auf Abschnitt
547  
548 =head4 Synopsis
549  
550 $sec->transferSource;
551  
552 =head4 Returns
553  
554 nichts
555  
556 =head4 Description
557  
558 Füge die Quelltexte aller Sub-Abschnitte von Abschnitt $sec zum
559 Abschnitt hinzu. Die Quelltexte der Sub-Abschnitte werden von
560 diesen gelöscht.
561  
562 =cut
563  
564 # -----------------------------------------------------------------------------
565  
566 0 ▶sub transferSource {
Subroutine transferSource: not called CC=4 SCAR=30.0
Pod documented
567 0 my $self = shift;
568  
569 0 ▶ if (defined $self->[5]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->[5]) 0 0 0
570 0 for my $sec (@{$self->[9]}) {
571 0 ▶ if (@{$sec->[9]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$sec->[9];}) 0 0 0
572 0 $sec->transferSource;
573 }
574 0 $self->[5] .= $sec->[5];
575 0 $sec->[5] = undef;
576 }
577 }
578
579 0 return;
580 }
581  
582 # -----------------------------------------------------------------------------
583  
584 =head3 removeEofMarker() - Entferne "# eof" von Quelltext und Content
585  
586 =head4 Synopsis
587  
588 $sec->removeEofMarker;
589  
590 =head4 Returns
591  
592 nichts
593  
594 =cut
595  
596 # -----------------------------------------------------------------------------
597  
598 0 ▶sub removeEofMarker {
Subroutine removeEofMarker: not called CC=1 SCAR=6.9
Pod documented
599 0 my $self = shift;
600  
601 0 $self->[4] =~ s/\s*^# eof\n*$//m;
602 0 $self->[5] =~ s/\n+^# eof\n*$/\n\n/m;
603  
604 0 return;
605 }
606  
607 # -----------------------------------------------------------------------------
608  
609 =head2 Attribute
610  
611 =head3 append() - Füge Zeichenkette zu Wert hinzu
612  
613 =head4 Synopsis
614  
615 $val = $sec->append($key=>$str);
616  
617 =head4 Arguments
618  
619 =over 4
620  
621 =item $key
622  
623 Schlüssel, dessen Wert ergänzt wird.
624  
625 =back
626  
627 =head4 Returns
628  
629 Wert (String)
630  
631 =head4 Description
632  
633 Füge Zeichenkette $str zum Wert des Schlüssels $key hinzu
634 und liefere den resultierenden Wert zurück.
635  
636 =cut
637  
638 # -----------------------------------------------------------------------------
639  
640 0 ▶sub append {
Subroutine append: not called CC=1 SCAR=6.9
Pod documented
641 0 my ($self,$key,$str) = @_;
642 0 my $ref = $self->getRef($key);
643 0 $$ref .= $str;
644 0 return $$ref;
645 }
646  
647 # -----------------------------------------------------------------------------
648  
649 =head3 get() - Liefere Wert zu Schlüssel
650  
651 =head4 Synopsis
652  
653 $val = $sec->get($key);
654 @vals = $sec->get(@keys);
655  
656 =head4 Arguments
657  
658 =over 4
659  
660 =item $key bzw. @keys
661  
662 Schlüssel, deren Wert geliefert wird.
663  
664 =back
665  
666 =head4 Returns
667  
668 Wert (Skalar-Kontext) oder Wertliste (Array-Kontext)
669  
670 =head4 Description
671  
672 Liefere den Wert zu Schlüssel $key bzw. die liste der Werte zu den
673 Schlüsseln @keys. Beginnt der $key mit einem Großbuchstaben, ist
674 es ein fataler Fehler, wenn zu dem Schlüssel mehrere Werte existieren.
675 Solche Schlüssel müssen mit $obj->L<getArray|"getArray() - Liefere Wertliste zu Schlüssel">() abgefragt werden.
676  
677 =cut
678  
679 # -----------------------------------------------------------------------------
680  
681 9 ▶sub get {
Subroutine get: called CC=5 SCAR=17.1
Pod documented
682 9 my $self = shift;
683 # @_: $key -or- @keys
684  
685 9 my @arr;
686 9 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 9 9 18
687 9 my $key = shift;
688  
689 9 my $val = $self->[3]->{$key};
690 9 ▶ if (ref($val) && $key =~ /^[A-Z]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val and $key =~ /^[A-Z]/) 0 9 9
Condition33%1/3
ref $val and $key =~ /^[A-Z]/
!ll&&!rl&&r
900
MC/DC0%0/2
ref $val and $key =~ /^[A-Z]/
ref $val $key =~ /^[A-Z]/
Truth table33%1/3
A and B
A= ref $valB= $key =~ /^[A-Z]/
ABresult
0X0
100
111
691 0 $self->throw(
692 'SECOBJ-00001: Schlüssel besitzt mehrere Werte',
693 Key => $key,
694 Values => '['.join(',',@$val).']',
695 );
696 }
697 9 push @arr,$val;
698 }
699
700 9 ▶ return wantarray? @arr: $arr[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 9 0 9
701 }
702  
703 # -----------------------------------------------------------------------------
704  
705 =head3 getArray() - Liefere Wertliste zu Schlüssel
706  
707 =head4 Synopsis
708  
709 $arr|@arr = $sec->getArray($key);
710  
711 =head4 Arguments
712  
713 =over 4
714  
715 =item $key
716  
717 Schlüssel dessen Wertliste geliefert wird.
718  
719 =back
720  
721 =head4 Returns
722  
723 Wert bzw. Werte
724  
725 =head4 Description
726  
727 Liefere die Wertliste von Schlüssel $key.
728  
729 =cut
730  
731 # -----------------------------------------------------------------------------
732  
733 0 ▶sub getArray {
Subroutine getArray: not called CC=7 SCAR=40.3
Pod documented
734 0 my ($self,$key) = @_;
735  
736 0 my $s = $self->[3]->{$key};
737 0 ▶ if (ref $s) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $s) { } 0 0 0
elsif (defined $s and $s ne '') { } 0 0 0
738 0 ▶ return wantarray? @$s: $s;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
739 }
740 ▶ elsif (defined $s && $s ne '') {
Condition0%0/3
defined $s and $s ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $s and $s ne ""
defined $s $s ne ""
Truth table0%0/3
A and B
A= defined $sB= $s ne ""
ABresult
0X0
100
111
741 0 ▶ return wantarray? ($s): [$s];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
742 }
743 else {
744 0 ▶ return wantarray? (): [];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
745 }
746 }
747  
748 # -----------------------------------------------------------------------------
749  
750 =head3 getBool() - Liefere boolschen Wert zu Schlüssel
751  
752 =head4 Synopsis
753  
754 $bool = $sec->getBool($key);
755 $bool = $sec->getBool($key,$default);
756  
757 =head4 Arguments
758  
759 =over 4
760  
761 =item $key
762  
763 Schlüssel, dessen Wert geliefert wird.
764  
765 =item $default
766  
767 Defaultwert, wenn Attribut nicht gesetzt
768  
769 =back
770  
771 =head4 Returns
772  
773 Wert (Skalar)
774  
775 =head4 Description
776  
777 Liefere boolschen Wert zu Schlüssel $key.
778  
779 =cut
780  
781 # -----------------------------------------------------------------------------
782  
783 0 ▶sub getBool {
Subroutine getBool: not called CC=8 SCAR=42.8
Pod documented
784 0 my ($self,$key,$default) = @_;
785  
786 0 ▶ my $val = lc($self->get($key) // $default // '');
Condition0%0/5
($self->get($key) // $default) // ""
l!l
00
$self->get($key) // $default
l!l&&r!l&&!r
000
MC/DC0%0/2
($self->get($key) // $default) // ""
$self->get($key) $default
Truth table0%0/3
A // B
A= $self->get($key)B= $default
ABresult
1X1
011
000
787 0 ▶ if ($val eq '' || $val eq 'no' || $val eq '0') {
Branch: 2 branches
BranchTrueFalseTotal
if ($val eq '' or $val eq 'no' or $val eq '0') { } 0 0 0
elsif ($val eq 'yes' or $val eq '1') { } 0 0 0
Condition0%0/6
$val eq "" or $val eq "no" or $val eq 0
l!l&&r!l&&!r
000
$val eq "" or $val eq "no"
l!l&&r!l&&!r
000
MC/DC0%0/3
$val eq "" or $val eq "no" or $val eq 0
$val eq "" $val eq "no" $val eq 0
Truth table0%0/4
A or B or C
A= $val eq ""B= $val eq "no"C= $val eq 0
ABCresult
1XX1
01X1
0011
0000
788 0 $val = 0;
789 }
790 ▶ elsif ($val eq 'yes' || $val eq '1') {
Condition0%0/3
$val eq "yes" or $val eq 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$val eq "yes" or $val eq 1
$val eq "yes" $val eq 1
Truth table0%0/3
A or B
A= $val eq "yes"B= $val eq 1
ABresult
1X1
011
000
791 0 $val = 1;
792 }
793 else {
794 0 $self->throw(
795 'COTEDO-00001: Illegal attribute value. Only Yes/No allowed.',
796 Attribute => $key,
797 Value => $val,
798 File => $self->file,
799 Line => $self->line,
800 -stacktrace => 0,
801 );
802 }
803  
804 0 return $val;
805 }
806  
807 # -----------------------------------------------------------------------------
808  
809 =head3 getMandatory() - Liefere Wert eines Pflichtattributs
810  
811 =head4 Synopsis
812  
813 $val = $sec->getMandatory($key);
814  
815 =head4 Arguments
816  
817 =over 4
818  
819 =item $key
820  
821 Schlüssel, dessen Wert geliefert wird.
822  
823 =back
824  
825 =head4 Returns
826  
827 Wert (Skalar)
828  
829 =head4 Description
830  
831 Wie $sec->L<get|"get() - Liefere Wert zu Schlüssel">(), nur dass ein Wert existieren muss, sonst
832 wird eine Exception geworfen.
833  
834 =cut
835  
836 # -----------------------------------------------------------------------------
837  
838 0 ▶sub getMandatory {
Subroutine getMandatory: not called CC=3 SCAR=24.8
Pod documented
839 0 my ($self,$key) = @_;
840  
841 0 my $val = $self->get($key);
842 0 ▶ if (!defined $val || $val eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $val or $val eq "") 0 0 0
Condition0%0/3
not defined $val or $val eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $val or $val eq ""
not defined $val $val eq ""
Truth table0%0/3
not(A) or B
A= not defined $valB= $val eq ""
ABresult
1X1
011
000
843 0 $self->throw(
844 'SECOBJ-00002: Attribute has no value',
845 Key => $key,
846 File => $self->file,
847 Line => $self->line,
848 -stacktrace => 0,
849 );
850 }
851  
852 0 return $val;
853 }
854  
855 # -----------------------------------------------------------------------------
856  
857 =head3 getRef() - Liefere Referenz auf Schlüsselwert
858  
859 =head4 Synopsis
860  
861 $ref = $sec->getRef($key);
862  
863 =cut
864  
865 # -----------------------------------------------------------------------------
866  
867 0 ▶sub getRef {
Subroutine getRef: not called CC=1 SCAR=6.9
Pod documented
868 0 my ($self,$key) = @_;
869 0 return \$self->[3]->{$key};
870 }
871  
872 # -----------------------------------------------------------------------------
873  
874 =head3 search() - Suche Attributwert
875  
876 =head4 Synopsis
877  
878 ($key,$val) = $sec->search(\@sections,\@keys);
879  
880 =head4 Description
881  
882 Durchsuche die Liste der Abschnitts-Objekte ($self,@sections)
883 nach dem ersten Attribut aus @keys, das einen Wert besitzt und
884 liefere das Schlüssel/Wert-Paar zurück. Ist der Wert 'NULL',
885 wird '' (Leerstring) geliefert. Auf diese Weise kann auf "höherer Ebene"
886 definierter Wert außer Kraft gesetzt werden.
887  
888 =cut
889  
890 # -----------------------------------------------------------------------------
891  
892 0 ▶sub search {
Subroutine search: not called CC=5 SCAR=34.0
Pod documented
893 0 my $self = shift;
894 0 my $sectionA = shift;
895 0 my $keyA = [];
896  
897 0 my $key = '';
898 0 my $text = '';
899 0 SECTION: for my $sec ($self,@$sectionA) {
900 0 for (@$keyA) {
901 0 ▶ if ($text = $sec->get($_)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text = $sec->get($_)) 0 0 0
902 0 $key = $_;
903 0 last SECTION;
904 }
905 }
906 }
907 0 ▶ if ($text eq 'NULL') {
Branch: 1 branch
BranchTrueFalseTotal
if ($text eq "NULL") 0 0 0
908 0 $text = '';
909 }
910  
911 0 return ($key,$text);
912 }
913  
914 # -----------------------------------------------------------------------------
915  
916 =head3 try() - Werte abfragen ohne Exception
917  
918 =head4 Synopsis
919  
920 $val = $sec->try($key);
921 @vals = $sec->try(@keys);
922  
923 =head4 Description
924  
925 Wie L<get|"get() - Liefere Wert zu Schlüssel">(), nur dass im Falle eines unerlaubten Schlüssels
926 keine Exception geworfen, sondern C<undef> geliefert wird.
927  
928 =cut
929  
930 # -----------------------------------------------------------------------------
931  
932 0 ▶sub try {
Subroutine try: not called CC=1 SCAR=6.9
Pod documented
933 0 my ($self,$key) = @_;
934 0 return $self->[3]->try($key);
935 }
936  
937 # -----------------------------------------------------------------------------
938  
939 =head3 memoize() - Ermittele Wert und cache ihn auf Attribut
940  
941 =head4 Synopsis
942  
943 $val = $sec->memoize($key,$sub);
944  
945 =head4 Description
946  
947 Die Methode liefert den Wert des Attributs $key. Ist kein Wert
948 definiert (Wert ist C<undef>), wird die Methode $sec->$sub($key)
949 gerufen, der Wert berechnet und auf dem Attribut $key gespeichert.
950 Weitere Aufrufe liefern diesen Wert, ohne dass er neu berechnet wird.
951  
952 Die Methode ist nützlich, um in Objektmethoden eingebettet zu werden,
953 die einen berechneten Wert liefern, der nicht immer wieder neu
954 gerechnet werden soll.
955  
956 =head4 Example
957  
958 sub name {
959 return shift->memoize('name',sub {
960 my ($self,$key) = @_;
961 my $name = $self->get(ucfirst $key);
962 $name =~ s/^\W+//;
963 $name =~ s|\W+|/|g;
964 return $name;
965 });
966 }
967  
968 =cut
969  
970 # -----------------------------------------------------------------------------
971  
972 0 ▶sub memoize {
Subroutine memoize: not called CC=2 SCAR=17.9
Pod documented
973 0 my ($self,$key,$sub) = @_;
974  
975 0 my $val = $self->[3]->{$key};
976 0 ▶ if (!defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 0 0 0
977 0 $val = $self->$sub($key);
978 0 $self->[3]->{$key} = $val;
979 }
980  
981 0 return $val;
982 }
983  
984 # -----------------------------------------------------------------------------
985  
986 =head3 memoizeWeaken() - Ermittele schwache Referenz
987  
988 =head4 Synopsis
989  
990 $val = $sec->memoizeWeaken($key,$sub);
991  
992 =head4 Description
993  
994 Die Methode ist identisch zu $sec->L<memoize|"memoize() - Ermittele Wert und cache ihn auf Attribut">(), nur dass eine
995 Referenz ermittelt und automatisch zu einer schwachen Referenz
996 gemacht wird.
997  
998 =cut
999  
1000 # -----------------------------------------------------------------------------
1001  
1002 0 ▶sub memoizeWeaken {
Subroutine memoizeWeaken: not called CC=2 SCAR=17.9
Pod documented
1003 0 my ($self,$key,$sub) = @_;
1004  
1005 0 my $val = $self->[3]->{$key};
1006 0 ▶ if (!defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 0 0 0
1007 0 $val = $self->$sub($key);
1008 0 $self->[3]->{$key} = $val;
1009 0 $self->[3]->weaken($key);
1010 }
1011  
1012 0 return $val;
1013 }
1014  
1015 # -----------------------------------------------------------------------------
1016  
1017 =head3 set() - Setze Schlüssel auf Wert
1018  
1019 =head4 Synopsis
1020  
1021 $sec->set(@keyVal);
1022  
1023 =head4 Arguments
1024  
1025 =over 4
1026  
1027 =item @keyVal
1028  
1029 Schlüssel/Wert-Paare, die gesetzt werden.
1030  
1031 =back
1032  
1033 =head4 Returns
1034  
1035 nichts
1036  
1037 =cut
1038  
1039 # -----------------------------------------------------------------------------
1040  
1041 0 ▶sub set {
Subroutine set: not called CC=1 SCAR=6.9
Pod documented
1042 0 my $self = shift;
1043 # @_: @keyVal
1044 0 $self->[3]->set(@_);
1045 0 return;
1046 }
1047  
1048 # -----------------------------------------------------------------------------
1049  
1050 =head3 setDefault() - Setze Defaultwert
1051  
1052 =head4 Synopsis
1053  
1054 $sec->setDefault(@keyVal);
1055  
1056 =head4 Arguments
1057  
1058 =over 4
1059  
1060 =item @keyVal
1061  
1062 Liste der Schlüssel/Wert-Paare.
1063  
1064 =back
1065  
1066 =head4 Returns
1067  
1068 I<nichts>
1069  
1070 =head4 Description
1071  
1072 Setze Schlüssel ohne Wert, d.h. wenn der Wert ein Leerstring ist,
1073 setze ihn auf den angegebenen Defaultwert.
1074  
1075 =head4 Example
1076  
1077 $sec->setDefault(
1078 Width => 1000,
1079 EntityMenuWidth => 345,
1080 BorderWidth => 1,
1081 PackageMenuHeight => 34,
1082 );
1083  
1084 =cut
1085  
1086 # -----------------------------------------------------------------------------
1087  
1088 0 ▶sub setDefault {
Subroutine setDefault: not called CC=3 SCAR=24.8
Pod documented
1089 0 my $self = shift;
1090 # @_: @keyVal
1091  
1092 0 my $h = $self->[3];
1093 0 ▶ for (my $i = 0; $i < @_; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 0 0 0
1094 0 ▶ if ($h->{$_[$i]} eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($$h{$_[$i]} eq "") 0 0 0
1095 0 $h->{$_[$i]} = $_[$i+1];
1096 }
1097 }
1098  
1099 0 return;
1100 }
1101  
1102 # -----------------------------------------------------------------------------
1103  
1104 =head3 add() - Füge Schlüssel und Wert hinzu
1105  
1106 =head4 Synopsis
1107  
1108 $sec->add(@keyVal);
1109  
1110 =head4 Arguments
1111  
1112 =over 4
1113  
1114 =item @keyVal
1115  
1116 Schlüssel/Wert-Paare, die gesetzt werden.
1117  
1118 =back
1119  
1120 =head4 Returns
1121  
1122 nichts
1123  
1124 =cut
1125  
1126 # -----------------------------------------------------------------------------
1127  
1128 0 ▶sub add {
Subroutine add: not called CC=1 SCAR=6.9
Pod documented
1129 0 my $self = shift;
1130 # @_: @keyVal
1131 0 $self->[3]->add(@_);
1132 0 return;
1133 }
1134  
1135 # -----------------------------------------------------------------------------
1136  
1137 =head3 push() - Füge Element zu Arraykomponente hinzu
1138  
1139 =head4 Synopsis
1140  
1141 $sec->push($key,$val);
1142  
1143 =head4 Arguments
1144  
1145 =over 4
1146  
1147 =item $key
1148  
1149 Arraykomponente.
1150  
1151 =item $val
1152  
1153 Wert, der zum Array am Ende hinzugefügt wird.
1154  
1155 =back
1156  
1157 =head4 Description
1158  
1159 Füge Wert $val zur Arraykomponente $key hinzu. Die Methode liefert keinen
1160 Wert zurück.
1161  
1162 =cut
1163  
1164 # -----------------------------------------------------------------------------
1165  
1166 0 ▶sub push {
Subroutine push: not called CC=1 SCAR=6.9
Pod documented
1167 0 my ($self,$key,$val) = @_;
1168 0 CORE::push @{$self->[3]->{$key}},$val;
1169 0 return;
1170 }
1171  
1172 # -----------------------------------------------------------------------------
1173  
1174 =head3 weaken() - Erzeuge schwache Referenz
1175  
1176 =head4 Synopsis
1177  
1178 $ref = $sec->weaken($key);
1179 $ref = $sec->weaken($key=>$ref);
1180  
1181 =head4 Description
1182  
1183 Mache die Referenz von Schlüssel $key zu einer schwachen Referenz
1184 und liefere sie zurück. Ist eine Referenz $ref als Parameter angegeben,
1185 setze die Referenz zuvor.
1186  
1187 =cut
1188  
1189 # -----------------------------------------------------------------------------
1190  
1191 0 ▶sub weaken {
Subroutine weaken: not called CC=1 SCAR=6.9
Pod documented
1192 0 return shift->[3]->weaken(@_);
1193 }
1194  
1195 # -----------------------------------------------------------------------------
1196  
1197 =head3 validate() - Prüfe und ergänze Attribute
1198  
1199 =head4 Synopsis
1200  
1201 $sec->validate($contentAllowed,\@keys);
1202  
1203 =head4 Arguments
1204  
1205 =over 4
1206  
1207 =item $contentAllowed
1208  
1209 Wenn falsch, erlaubt der Abschnitt keinen Content (außer "# eof"
1210 als Dateiende-Markierung).
1211  
1212 =item @keys
1213  
1214 Liste der zulässigen Abschnittsattribute
1215  
1216 =back
1217  
1218 =head4 Returns
1219  
1220 Nichts
1221  
1222 =head4 Description
1223  
1224 Die Methode finalisiert das Abschnittsobjekt in folgender Weise:
1225  
1226 =over 2
1227  
1228 =item *
1229  
1230 Sie prüft, dass wenn kein Content erlaubt ist, keiner existiert.
1231  
1232 =item *
1233  
1234 Sie prüft, dass nur Schlüssel im Objekt vorkommen, die in @keys
1235 vorkommen. Kommt ein anderer Schlüssel im Objekt vor, wird eine
1236 Exception geworfen.
1237  
1238 =item *
1239  
1240 Sie fügt Schlüssel aus @keys zum Objekt hinzu, die das Objekt nicht hat.
1241  
1242 =back
1243  
1244 =cut
1245  
1246 # -----------------------------------------------------------------------------
1247  
1248 0 ▶sub validate {
Subroutine validate: not called CC=9 SCAR=45.0
Pod documented
1249 0 my $self = shift;
1250 0 my $contentAllowed = shift;
1251 0 my $keyA = shift; # erlaubte Attribute
1252  
1253 # Prüfung Content
1254  
1255 0 ▶ if (!$contentAllowed && $self->[4] ne '' && $self->[4] ne '# eof') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $contentAllowed and $self->[4] ne "" and $self->[4] ne "# eof") 0 0 0
Condition0%0/6
not $contentAllowed and $self->[4] ne "" and $self->[4] ne "# eof"
!ll&&!rl&&r
000
not $contentAllowed and $self->[4] ne ""
!ll&&!rl&&r
000
MC/DC0%0/3
not $contentAllowed and $self->[4] ne "" and $self->[4] ne "# eof"
not $contentAllowed $self->[4] ne "" $self->[4] ne "# eof"
Truth table0%0/4
not(A) and B and C
A= not $contentAllowedB= $self->[4] ne ""C= $self->[4] ne "# eof"
ABCresult
0XX0
10X0
1100
1111
1256 0 $self->throw(
1257 'SECTION-00001: Inhalt ist nicht erlaubt',
1258 Section => $self->type,
1259 Content => $self->[4],
1260 File => $self->file,
1261 Line => $self->line,
1262 -stacktrace => 0,
1263 );
1264 }
1265  
1266 # Ergänzung fehlender Attribute
1267  
1268 0 my $h = $self->[3];
1269 0 for my $key (@$keyA) {
1270 0 ▶ if (!exists $h->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $h->{$key}) 0 0 0
1271 0 $h->{$key} = '';
1272 }
1273 }
1274  
1275 0 ▶ if ($h->hashSize > @$keyA) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h->hashSize > @$keyA) 0 0 0
1276 # Wenn im Hash mehr Schlüssel als erwartet vorkommen,
1277 # fehlerhafte Schlüssel ermitteln und Exception werfen
1278  
1279 0 my %h;
1280 0 @h{@$keyA} = (1) x @$keyA;
1281 0 my @keys;
1282 0 for my $key ($self->[3]->keys) {
1283 0 ▶ if (!exists $h{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $h{$key}) 0 0 0
1284 0 CORE::push @keys,$key;
1285 }
1286 }
1287  
1288 $self->throw(
1289 0 'SECTION-00001: Unknown section attributes',
1290 Section => $self->type,
1291 Keys => join(', ',@keys),
1292 File => $self->file,
1293 Line => $self->line,
1294 -stacktrace => 0,
1295 );
1296 }
1297  
1298 0 return;
1299 }
1300  
1301 # -----------------------------------------------------------------------------
1302  
1303 =head3 lockKeys() - Locke die Keys des Attribut-Hash
1304  
1305 =head4 Synopsis
1306  
1307 $sec->lockKeys;
1308  
1309 =cut
1310  
1311 # -----------------------------------------------------------------------------
1312  
1313 0 ▶sub lockKeys {
Subroutine lockKeys: not called CC=1 SCAR=6.9
Pod documented
1314 0 my $self = shift;
1315 0 $self->[3]->lockKeys;
1316 0 return;
1317 }
1318  
1319 # -----------------------------------------------------------------------------
1320  
1321 =head2 Fehler
1322  
1323 =head3 error() - Wirf eine Exception mit Dateiname und Zeilennummer
1324  
1325 =head4 Synopsis
1326  
1327 $sec->error($msg,@keyVal);
1328  
1329 =head4 Arguments
1330  
1331 =over 4
1332  
1333 =item $msg
1334  
1335 Die Fehlermeldung.
1336  
1337 =item @keyVal
1338  
1339 Detailinformation zum Fehler.
1340  
1341 =back
1342  
1343 =head4 Returns
1344  
1345 Die Methode kehrt nicht zurück
1346  
1347 =head4 Description
1348  
1349 Die Methode wirft eine Exception mit dem Fehlertext $msg und den
1350 als Schlüssel/Wert-Paare angegebenen Informationen @keyVal. Ferner
1351 wird von der Methode der Dateiname und die Zeilennummer des
1352 Abschnitts ergnzt. Die Exception beinhaltet keinen Stacktrace.
1353  
1354 =cut
1355  
1356 # -----------------------------------------------------------------------------
1357  
1358 0 ▶sub error {
Subroutine error: not called CC=1 SCAR=6.9
Pod documented
1359 0 my ($self,$msg) = splice @_,0,2;
1360 # @_: @keyVal
1361  
1362 0 $self->throw(
1363 $msg,
1364 @_,
1365 File => $self->file,
1366 Line => $self->line,
1367 -stacktrace => 0,
1368 );
1369 }
1370  
1371 # -----------------------------------------------------------------------------
1372  
1373 =head2 Automatische Akzessor-Methoden
1374  
1375 =head3 AUTOLOAD() - Erzeuge Akzessor-Methode
1376  
1377 =head4 Synopsis
1378  
1379 $val = $this->AUTOLOAD;
1380 $val = $this->AUTOLOAD($val);
1381  
1382 =head4 Description
1383  
1384 Erzeuge beim ersten Aufruf eine Akzessor-Methode für einen Schlüssel
1385 des Schlüssel/Wert-Hashs und führe den betreffenden Methodenaufruf
1386 durch.
1387  
1388 =cut
1389  
1390 # -----------------------------------------------------------------------------
1391  
1392 8 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=5 SCAR=27.2
1393 8 my $this = shift;
1394 # @_: Methodenargumente
1395  
1396 8 my ($key) = our $AUTOLOAD =~ /::(\w+)$/;
1397 8 ▶ return if $key !~ /[^A-Z]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /[^A-Z]/ 8 0 8
1398  
1399 # Klassenmethoden generieren wir nicht
1400  
1401 0 ▶ if (!ref $this) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $this) 0 0 0
1402 0 $this->throw(
1403 'HASH-00002: Klassen-Methode existiert nicht',
1404 Method => $key,
1405 );
1406 }
1407  
1408 # Methode nur generieren, wenn Attribut existiert
1409  
1410 0 ▶ if (!exists $this->[3]->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $this->[3]{$key}) 0 0 0
1411 0 ▶ $this->throw(
Branch: 1 branch
BranchTrueFalseTotal
ref $this ? : 0 0 0
1412 'HASH-00001: Schlüssel existiert nicht',
1413 Attribute => $key,
1414 Class => ref($this)? ref($this): $this,
1415 );
1416 }
1417  
1418 # Attribut-Methode generieren. Wenn der Hash $self->[3] gelockt ist,
1419 # führt ein unerlaubter Zugriff zu einer Exception.
1420  
1421 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1422 0 ▶ *{$AUTOLOAD} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1423 0 my $self = shift;
1424 # @_: $val
1425  
1426 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
1427 0 return $self->[3]->{$key} = shift;
1428 }
1429  
1430 0 return $self->[3]->{$key};
1431 0 };
1432  
1433 # Methode aufrufen
1434 0 return $this->$key(@_);
1435 }
1436  
1437 # -----------------------------------------------------------------------------
1438  
1439 =head1 VERSION
1440  
1441 1.240
1442  
1443 =head1 AUTHOR
1444  
1445 Frank Seitz, L<http://fseitz.de/>
1446  
1447 =head1 COPYRIGHT
1448  
1449 Copyright (C) 2026 Frank Seitz
1450  
1451 =head1 LICENSE
1452  
1453 This code is free software; you can redistribute it and/or modify
1454 it under the same terms as Perl itself.
1455  
1456 =cut
1457  
1458 # -----------------------------------------------------------------------------
1459  
1460 6 1;
1461  
1462 # eof
« Quiq/Option.pm ↑ Summary Quiq/Html/Widget/CheckBox.pm »