Quiq/If.pm

filter: Statementstmt 95.2% 20 / 21 Branchbran 83.3% 5 / 6 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 94.7% 36 / 38 CC 5 SCAR 8.0
CC
5
Cov
93%
listIf
3.1
catIf
3.0
BEGIN
1.0
CRAP 2.2
« Quiq/Database/Patch.pm ↑ Summary Quiq/PostgreSql/Catalog.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::If - Liefere Werte unter einer Bedingung
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Object>
12  
13 =cut
14  
15 # -----------------------------------------------------------------------------
16  
17 package Quiq::If;
18 1 ▶use base qw/Quiq::Object/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 our $VERSION = '1.240';
25  
26 # -----------------------------------------------------------------------------
27  
28 =head1 METHODS
29  
30 =head2 Klassenmethoden
31  
32 =head3 catIf() - Konkateniere Strings bei erfüllter Bedingung
33  
34 =head4 Synopsis
35  
36 $str = $class->catIf($bool,sub {$expr,...});
37  
38 =head4 Arguments
39  
40 =over 4
41  
42 =item $bool
43  
44 Bedingung
45  
46 =item sub {$expr,...}
47  
48 Ausdrücke, deren Resultat konkateniert wird.
49  
50 =back
51  
52 =head4 Returns
53  
54 String
55  
56 =head4 Description
57  
58 Ist Bedingung C<$bool> falsch, liefere einen Leerstring. Andernfalls
59 konkateniere die Werte der Ausdrücke C<$expr, ...> und liefere das
60 Resultat zurück. Evaluiert ein Ausdruck C<$expr> zu C<undef>, wird
61 der Wert durch einen Leerstring ersetzt.
62  
63 Die Methode ist logisch äquivalent zu
64  
65 $str = !$bool? '': join '',$expr // '', ...;
66  
67 Sie vermeidet jedoch, dass C<$expr // '', ...> berechnet werden muss,
68 wenn C<$bool> falsch ist.
69  
70 =head4 Example
71  
72 B<Konkatenation bei zutreffender Bedingung>
73  
74 Quiq::If->catIf(1,sub {
75 'Dies',
76 'ist',
77 'ein',
78 undef,
79 'Test',
80 });
81 ==>
82 'DiesisteinTest'
83  
84 =cut
85  
86 # -----------------------------------------------------------------------------
87  
88 2 ▶sub catIf {
Subroutine catIf: called CC=3 SCAR=11.0
Pod documented
89 2 my ($class,$bool,$sub) = @_;
90 2 ▶ return !$bool? '': join '',map {$_ // ''} $sub->();
Branch: 1 branch
BranchTrueFalseTotal
!$bool ? : 1 1 2
Condition100%2/2
$_ // ""
l!l
41
MC/DC100%1/1
$_ // ""
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
91 }
92
93  
94 # -----------------------------------------------------------------------------
95  
96 =head3 listIf() - Liefere Liste bei erfüllter Bedingung
97  
98 =head4 Synopsis
99  
100 @ret = $class->listIf($bool,@list);
101 @ret = $class->listIf($bool,$sub);
102  
103 =head4 Arguments
104  
105 =over 4
106  
107 =item $bool
108  
109 Bedingung
110  
111 =item @list
112  
113 Liste, die bei erfüllter Bedingung geliefert wird.
114  
115 =back
116  
117 =head4 Returns
118  
119 String
120  
121 =head4 Description
122  
123 Ist Bedingung C<$bool> wahr, liefere @list bzw. den Rückgabewert
124 von $sub->(), andernfalls eine leere Liste.
125  
126 Die Methode ist logisch äquivalent zu
127  
128 !$bool? (): @list
129  
130 bzw.
131  
132 !$bool? (): $sub->()
133  
134 B<Anmerkung>: Die erste Variante hat den Nachteil, dass @list auch
135 dann ausgewertet wird, wenn $bool falsch ist. In dem Fall ist
136 die äquivalente Formulierung normalerweise vorzuziehen.
137  
138 =head4 Example
139  
140 Setze Attribut C<ready> des Quiq::Html::Page-Objekts
141 nur dann, wenn $refresh erfüllt ist:
142  
143 my $html = Quiq::Html::Page->html($h,
144 ...
145 Quiq::If->listIf($refresh,
146 ready => qq~
147 var refresh = $refresh;
148 var interval = setInterval(function() {
149 refresh--;
150 \$('#timer').text(refresh);
151 if (refresh == 0) {
152 clearInterval(interval);
153 \$('#timer').text('Lade Seite...');
154 location.reload();
155 }
156 },1000);
157 ~
158 ),
159 );
160  
161 =cut
162  
163 # -----------------------------------------------------------------------------
164  
165 2 ▶sub listIf {
Subroutine listIf: called CC=3 SCAR=11.3
Pod documented
166 2 my ($class,$bool) = splice @_,0,2;
167  
168 2 ▶ if (!$bool) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $bool) { } 1 1 2
elsif (ref $_[0] eq 'CODE') { } 1 0 1
169 1 return ();
170 }
171 elsif (ref($_[0]) eq 'CODE') {
172 1 return $_[0]->();
173 }
174  
175 0 return @_;
176 }
177
178  
179 # -----------------------------------------------------------------------------
180  
181 =head1 VERSION
182  
183 1.240
184  
185 =head1 AUTHOR
186  
187 Frank Seitz, L<http://fseitz.de/>
188  
189 =head1 COPYRIGHT
190  
191 Copyright (C) 2026 Frank Seitz
192  
193 =head1 LICENSE
194  
195 This code is free software; you can redistribute it and/or modify
196 it under the same terms as Perl itself.
197  
198 =cut
199  
200 # -----------------------------------------------------------------------------
201  
202 1 1;
203  
204 # eof
« Quiq/Database/Patch.pm ↑ Summary Quiq/PostgreSql/Catalog.pm »