Quiq/Record.pm

filter: Statementstmt 88.8% 72 / 81 Branchbran 78.5% 22 / 28 Conditioncond 55.5% 5 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 90.9% 10 / 11 Podpod 100% 4 / 4 totaltotal 82.7% 115 / 139 CC 18 SCAR 20.3
CC
18
Cov
84%
toString
12.7
fromString
7.3
fromFile
2.0
CRAP 7.6
« Quiq/Trash.pm ↑ Summary Quiq/PerlModule.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Record - Verarbeitung von Text-Records
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Object>
12  
13 =head1 DESCRIPTION
14  
15 Ein Text-Record ist eine Folge von Schlüssel/Wert-Paaren
16 in Textform, wobei
17  
18 =over 2
19  
20 =item *
21  
22 ein Schlüssel eine Folge von alphanumerischen Zeichen oder
23 Unterstrich ('_') oder Bindestrich ('-') ist, und
24  
25 =item *
26  
27 ein Wert ein beliebiger einzeiliger oder mehrzeiliger Text ist.
28  
29 =back
30  
31 Stringrepräsentation:
32  
33 Schlüssel1:
34 Wert1
35 Schlüssel2:
36 Wert2
37 ...
38  
39 oder
40  
41 Schlüssel1: Wert1
42 Schlüssel2: Wert2
43 ...
44  
45 oder ein Mischung aus beidem oder
46  
47 @@Schlüssel@@
48 Wert1
49 @@Schlüsse2@@
50 Wert2
51  
52 Um die dritte Repräsentation (mit @@Schlüssel@@) zu erzeugen, muss
53 als Option -format => '@' angegeben werden.
54  
55 =cut
56  
57 # -----------------------------------------------------------------------------
58  
59 package Quiq::Record;
60 2 ▶use base qw/Quiq::Object/;
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 2 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 2 our $VERSION = '1.240';
67  
68 2 ▶use Quiq::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 2 ▶use Quiq::Option;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 2 ▶use Quiq::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72 # -----------------------------------------------------------------------------
73  
74 =head1 METHODS
75  
76 =head2 Klassenmethoden
77  
78 =head3 fromString() - Lies Schlüssel/Wert-Paare aus String
79  
80 =head4 Synopsis
81  
82 @keyVal | $keyValA = $class->fromString($str);
83 @keyVal | $keyValA = $class->fromString(\$str);
84  
85 =head4 Description
86  
87 Lies Text-Record aus Zeichenkette $str, zerlege ihn in
88 Schlüssel/Wert-Paare und liefere die Liste der
89 Schlüssel/Wert-Paare zurück. Im Skalarkontext liefere eine
90 Referenz auf die Liste.
91  
92 =over 2
93  
94 =item *
95  
96 NEWLINEs am Anfang und am Ende eines Werts werden entfernt.
97  
98 =item *
99  
100 Eine Einrückung innerhalb eines mehrzeiligen Werts wird entfernt.
101 Eine Einrückung ist die längste Folge von Leerzeichen oder Tabs, die
102 allen Zeilen eines mehrzeiligen Werts gemeinsam ist.
103  
104 =back
105  
106 =cut
107  
108 # -----------------------------------------------------------------------------
109  
110 3 ▶sub fromString {
Subroutine fromString: called CC=7 SCAR=19.8
Pod documented
111 3 my $class = shift;
112 # @_: $str -or- \$str
113  
114 3 my $ref;
115 3 ▶ if (!defined $_[0]) { # undef
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $_[0]) { } 1 2 3
elsif (ref $_[0]) { } 2 0 2
116 1 $ref = \'';
117 }
118 elsif (ref $_[0]) { # String-Ref
119 2 $ref = $_[0];
120 }
121 else {
122 0 $ref = \$_[0]; # String
123 }
124  
125 3 my (@keys,@vals);
126 3 ▶ if (substr($$ref,0,1) eq '@') {
Branch: 2 branches
BranchTrueFalseTotal
if (substr($$ref, 0, 1) eq '@') { } 1 2 3
elsif (substr($$ref, 0, 1) eq '=') { } 0 2 2
127 1 @keys = $$ref =~ /^\@\@([\w-]+)\@\@ */gm;
128 1 @vals = split /^\@\@[\w-]+\@\@ */m,$$ref;
129 }
130 elsif (substr($$ref,0,1) eq '=') {
131 0 @keys = $$ref =~ /^==([\w-]+)== */gm;
132 0 @vals = split /^==[\w-]+== */m,$$ref;
133 }
134 else {
135 2 @keys = $$ref =~ /^([\w-]+) *[:=] */gm;
136 2 @vals = split /^[\w-]+ *[:=] */m,$$ref;
137 }
138 3 shift @vals;
139  
140 3 my @arr;
141 3 $#arr = @keys*2-1;
142  
143 3 ▶ for (my $i = 0; $i < @keys; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @keys) 6 3 9
144 6 $arr[$i*2] = $keys[$i];
145 6 my $val = Quiq::String->removeIndentation($vals[$i]);
146 6 $arr[$i*2+1] = $val;
147 }
148  
149 3 ▶ return wantarray? @arr: \@arr;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 0 3
150 }
151  
152 # -----------------------------------------------------------------------------
153  
154 =head3 fromFile() - Lies Schlüssel/Wert-Paare aus Datei
155  
156 =head4 Synopsis
157  
158 @keyVal | $keyValA = $class->fromFile($file,@opt);
159  
160 =head4 Arguments
161  
162 =over 4
163  
164 =item $file
165  
166 Datei, die den Record enthält.
167  
168 =back
169  
170 =head4 Options
171  
172 =over 4
173  
174 =item -encoding => $encoding
175  
176 Character Encoding, z.B. 'UTF-8'.
177  
178 =back
179  
180 =head4 Description
181  
182 Wie L<fromString|"fromString() - Lies Schlüssel/Wert-Paare aus String">, nur dass der Record aus Datei $file gelesen wird.
183  
184 =cut
185  
186 # -----------------------------------------------------------------------------
187  
188 0 ▶sub fromFile {
Subroutine fromFile: not called CC=1 SCAR=6.9
Pod documented
189 0 my ($class,$file) = splice @_,0,2;
190 # @_: @opt
191  
192 # Optionen
193  
194 0 my $encoding = undef;
195  
196 0 Quiq::Option->extract(\@_,
197 -encoding => \$encoding,
198 );
199  
200 0 return $class->fromString(Quiq::Path->read($file,-decode=>$encoding));
201 }
202  
203 # -----------------------------------------------------------------------------
204  
205 =head3 toString() - Schreibe Schlüssel/Wert-Paare auf String
206  
207 =head4 Synopsis
208  
209 $str = $class->toString(@keyVal,@opt);
210 $str = $class->toString(\@keyVal,@opt);
211  
212 =head4 Options
213  
214 =over 4
215  
216 =item --format => ':'|'@' (Default: ':')
217  
218 Im Falle von ':' erzeuge das Format:
219  
220 <KEY>:
221 <VAL>
222  
223 Im Falle von '@' erzeuge das Format:
224  
225 @@<KEY>@@
226 <VAL>
227  
228 =item -indent => $n (Default: 4)
229  
230 Tiefe der Einrückung.
231  
232 =item -ignoreNull => $bool (Default: 0)
233  
234 Ignoriere Schlüssel/Wert-Paare, bei denen der Wert null ist.
235  
236 =item -space => $n (Default: 0)
237  
238 Anzahl Leerzeilen zwischen den Einträgen.
239  
240 =item -strip => $bool (Default: 1)
241  
242 Entferne Leerzeilen am Anfang und Whitespace am Ende des Werts.
243  
244 =back
245  
246 =head4 Description
247  
248 Generiere für die Schlüssel/Wert-Paare @keyVal eine Text-Record
249 Repräsentation und liefere diese zurück.
250  
251 =cut
252  
253 # -----------------------------------------------------------------------------
254  
255 12 ▶sub toString {
Subroutine toString: called CC=12 SCAR=25.4
Pod documented
256 12 my $class = shift;
257 12 ▶ my $arr = ref $_[0]? shift: \@_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 10 2 12
258 # @_: Argumente
259  
260 12 my $format = ':';
261 12 my $indent = 4;
262 12 my $ignoreNull = 0;
263 12 my $space = 0;
264 12 my $strip = 1;
265  
266 12 Quiq::Option->extract(\@_,
267 -format => \$format,
268 -indent => \$indent,
269 -ignoreNull => \$ignoreNull,
270 -space => \$space,
271 -strip => \$strip,
272 );
273  
274 12 $indent = ' ' x $indent;
275 12 $space = "\n" x $space;
276  
277 12 my $str = '';
278 12 ▶ for (my $i = 0; $i < @$arr; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$arr) 44 12 56
279 44 my $key = $arr->[$i];
280 44 my $val = $arr->[$i+1];
281  
282 44 ▶ if (!defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 4 40 44
283 4 $val = '';
284 }
285 44 ▶ if ($strip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($strip) 38 6 44
286 38 $val =~ s/^\n+//;
287 38 $val =~ s/\s+$//;
288 }
289 44 ▶ if ($val eq '' && $ignoreNull) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val eq "" and $ignoreNull) 4 40 44
Condition100%3/3
$val eq "" and $ignoreNull
!ll&&!rl&&r
3644
MC/DC100%2/2
$val eq "" and $ignoreNull
$val eq "" $ignoreNull
Truth table100%3/3
A and B
A= $val eq ""B= $ignoreNull
ABresult
0X0
100
111
290 4 next;
291 }
292 40 ▶ if ($format eq ':' && $indent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq ":" and $indent) 40 0 40
Condition33%1/3
$format eq ":" and $indent
!ll&&!rl&&r
0040
MC/DC0%0/2
$format eq ":" and $indent
$format eq ":" $indent
Truth table33%1/3
A and B
A= $format eq ":"B= $indent
ABresult
0X0
100
111
293 40 Quiq::String->indent(\$val,$indent);
294 }
295 40 ▶ if ($space && $str) {
Branch: 1 branch
BranchTrueFalseTotal
if ($space and $str) 0 40 40
Condition33%1/3
$space and $str
!ll&&!rl&&r
4000
MC/DC0%0/2
$space and $str
$space $str
Truth table33%1/3
A and B
A= $spaceB= $str
ABresult
0X0
100
111
296 0 $str .= $space;
297 }
298  
299 40 ▶ if ($format eq ':') {
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq ':') { } 40 0 40
300 40 $str .= sprintf "%s:\n%s\n",$key,$val;
301 }
302 else {
303 0 $str .= sprintf "\@\@%s\@\@\n%s\n",$key,$val;
304 }
305 }
306  
307 12 return $str;
308 }
309  
310 # -----------------------------------------------------------------------------
311  
312 =head3 toFile() - Schreibe Schlüssel/Wert-Paare auf Datei
313  
314 =head4 Synopsis
315  
316 $class->toFile($file,@keyVal,@opt);
317 $class->toFile($file,\@keyVal,@opt);
318  
319 =head4 Options
320  
321 Siehe L<toString|"toString() - Schreibe Schlüssel/Wert-Paare auf String">
322  
323 =head4 Description
324  
325 Wie L<toString|"toString() - Schreibe Schlüssel/Wert-Paare auf String">, nur dass der Record auf eine Datei geschrieben wird.
326 Die Methode liefert keinen Wert zurück.
327  
328 =cut
329  
330 # -----------------------------------------------------------------------------
331  
332 6 ▶sub toFile {
Subroutine toFile: called CC=1 SCAR=0.0
Pod documented
333 6 my $class = shift;
334 6 my $file = shift;
335 # @_: siehe toString()
336  
337 6 Quiq::Path->write($file,$class->toString(@_));
338 6 return;
339 }
340  
341 # -----------------------------------------------------------------------------
342  
343 =head1 VERSION
344  
345 1.240
346  
347 =head1 AUTHOR
348  
349 Frank Seitz, L<http://fseitz.de/>
350  
351 =head1 COPYRIGHT
352  
353 Copyright (C) 2026 Frank Seitz
354  
355 =head1 LICENSE
356  
357 This code is free software; you can redistribute it and/or modify
358 it under the same terms as Perl itself.
359  
360 =cut
361  
362 # -----------------------------------------------------------------------------
363  
364 2 1;
365  
366 # eof
« Quiq/Trash.pm ↑ Summary Quiq/PerlModule.pm »