Quiq/Unindent.pm

filter: Statementstmt 100% 47 / 47 Branchbran 95.8% 23 / 24 Conditioncond 81.8% 9 / 11 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 100% 8 / 8 Podpod 100% 4 / 4 totaltotal 95.0% 95 / 100 CC 18 SCAR 18.5
CC
18
Cov
96%
hereDoc
11.0
trimNl
4.0
string
3.0
CRAP 6.4
« Quiq/Progress.pm ↑ Summary Quiq/Database/Tree.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Unindent - Entferne Einrückung von "Here Document" oder String-Literal
8  
9 =head1 SYNOPSIS
10  
11 Klasse laden:
12  
13 use Quiq::Unindent;
14  
15 Eingerücktes "Here Document":
16  
17 {
18 $text = Quiq::Unindent->hereDoc(<<' EOT');
19 Dies ist
20 ein Text
21 EOT
22
23 print $text;
24 }
25  
26 Eingerücktes mehrzeiliges String-Literal:
27  
28 {
29 $text = Quiq::Unindent->string('
30 Dies ist
31 ein Text
32 ');
33
34 print $text;
35 }
36  
37 Resultat in beiden Fällen:
38  
39 Dies ist
40 ein Text
41  
42 =head1 DESCRIPTION
43  
44 Die Klasse stellt Methoden zur Verfügung, mit denen die in der
45 Regel unerwünschte Einrückung von eingerückten mehrzeiligen
46 String-Literalen und "Here Documents" entfernt werden kann.
47  
48 =cut
49  
50 # -----------------------------------------------------------------------------
51  
52 package Quiq::Unindent;
53  
54 270 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 270 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 270 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 270 our $VERSION = '1.240';
59  
60 270 ▶use Quiq::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 # -----------------------------------------------------------------------------
63  
64 =head1 METHODS
65  
66 =head2 Klassenmethoden
67  
68 =head3 hereDoc() - Entferne Einrückung von "Here Document"
69  
70 =head4 Synopsis
71  
72 $str = $class->hereDoc(<<'EOT');
73 <Text>
74 EOT
75
76 $str = $class->hereDoc($char,<<'EOT');
77 <Text>
78 EOT
79  
80 =head4 Arguments
81  
82 =over 4
83  
84 =item $char (optional)
85  
86 Fortsetzungszeichen. Siehe Quiq::String->joinLines().
87  
88 =item $text
89  
90 Eingerückter Text.
91  
92 =back
93  
94 =head4 Description
95  
96 Entferne von allen Zeilen die tiefste Einrückung, die allen Zeilen
97 gemeinsam ist, und liefere die resultierende Zeichenkette zurück,
98 wobei
99  
100 =over 2
101  
102 =item *
103  
104 alle Sub-Einrückungen erhalten bleiben
105  
106 =item *
107  
108 alle Leerzeilen erhalten bleiben, auch am Anfang und am Ende
109  
110 =back
111  
112 Ist der Ende-Marker eingerückt, muss dessen Einrückung bei der
113 Vereinbarung des Markers angegeben werden. Siehe C<<< <<' EOT' >>> in
114 den Beispielen.
115  
116 =head4 Examples
117  
118 =over 4
119  
120 =item 1.
121  
122 Gegenüberstellung der Syntax
123  
124 {
125 $text = Quiq::Unindent->hereDoc(<<' EOT');
126 Dies ist
127 ein Text
128 EOT
129 }
130  
131 ist äquivalent zu
132  
133 {
134 $text = <<'EOT';
135 Dies ist
136 ein Text
137 EOT
138 }
139  
140 =item 2.
141  
142 Sub-Einrückungen und Leerzeilen
143  
144 {
145 $text = Quiq::Unindent->hereDoc(<<' EOT');
146
147 Dies ist der
148 erste Absatz.
149
150 Dies ist ein
151 zweiter Absatz.
152
153 EOT
154 }
155  
156 ergibt
157  
158 |
159 | Dies ist der
160 |erste Absatz.
161 |
162 | Dies ist ein
163 |zweiter Absatz.
164 |
165  
166 d.h. Sub-Einrückungen und Leerzeilen bleiben erhalten.
167  
168 =back
169  
170 =cut
171  
172 # -----------------------------------------------------------------------------
173  
174 122 ▶sub hereDoc {
Subroutine hereDoc: called CC=11 SCAR=24.0
Pod documented
175 122 my $class = shift;
176 122 ▶ my $char = @_ == 2? shift: undef;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 ? : 118 4 122
177 122 ▶ my $str = shift // return '';
Condition50%1/2
shift() // (return "")
l!l
1220
MC/DC0%0/1
shift() // (return "")
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
178  
179 # Wir brauchen uns nur mit dem String befassen, wenn das erste
180 # Zeichen ein Whitespacezeichen ist. Wenn dies nicht der Fall
181 # ist, existiert keine Einrückung, die wir entfernen müssten.
182  
183 122 ▶ if ($str =~ /^\s/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^\s/) 82 40 122
184 82 my $ind;
185 82 ▶ while ($str =~ /^([ \t]*)(.?)/gm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^([ \t]*)(.?)/gm) 642 81 723
186 642 ▶ if (length $2 == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $2 == 0) { } 33 609 642
elsif (not defined $ind or length $1 < length $ind) { } 93 516 609
187 # Leerzeilen und Whitespace-Zeilen übergehen wir
188 }
189 ▶ elsif (!defined $ind || length $1 < length $ind) {
Condition100%3/3
not defined $ind or length $1 < length $ind
l!l&&r!l&&!r
8211516
MC/DC100%2/2
not defined $ind or length $1 < length $ind
not defined $ind length $1 < length $ind
Truth table100%3/3
not(A) or B
A= not defined $indB= length $1 < length $ind
ABresult
1X1
011
000
190 93 $ind = $1;
191 93 ▶ if (!$ind) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ind) 1 92 93
192 # Zeile ohne Einrückung gefunden
193 1 last;
194 }
195 }
196 }
197 82 ▶ if ($ind) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ind) 81 1 82
198 # gemeinsame Einrückung von allen Zeilen entfernen
199 81 $str =~ s/^$ind//gm;
200 }
201 }
202 122 ▶ if (defined $char) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $char) 3 119 122
203 3 $str = Quiq::String->joinLines($char,$str);
204 }
205  
206 122 return $str;
207 }
208  
209 # -----------------------------------------------------------------------------
210  
211 =head3 string() - Entferne Einrückung von mehrzeiligem String-Literal
212  
213 =head4 Synopsis
214  
215 $str = $class->string('
216 <Text>
217 ');
218
219 $str = $class->string($char,'
220 <Text>
221 ');
222  
223 =head4 Arguments
224  
225 =over 4
226  
227 =item $char (optional)
228  
229 Fortsetzungszeichen. Siehe Quiq::String->joinLines().
230  
231 =item $text
232  
233 Eingerückter Text.
234  
235 =back
236  
237 =head4 Description
238  
239 Wie Methode L<hereDoc|"hereDoc() - Entferne Einrückung von "Here Document"">(), wobei über die Einrückung hinaus
240  
241 =over 2
242  
243 =item *
244  
245 der erste Zeilenumbruch am Anfang entfernt wird (sofern vorhanden)
246  
247 =item *
248  
249 die Leerzeichen am Ende entfernt werden (sofern vorhanden)
250  
251 =back
252  
253 Diese (zusätzlichen) Manipulationen sorgen dafür, dass der
254 Leerraum entfernt wird, der dadurch entsteht, wenn die
255 Anführungsstriche auf einer eigenen Zeile stehen.
256  
257 =head4 Examples
258  
259 =over 4
260  
261 =item 1.
262  
263 Gegenüberstellung der Syntax:
264  
265 {
266 $text = Quiq::Unindent->string('
267 Dies ist
268 ein Text
269 ');
270 }
271  
272 ist äquivalent zu
273  
274 {
275 $text = 'Dies ist
276 ein Text
277 ';
278 }
279  
280 =item 2.
281  
282 Varianten
283  
284 $text = Quiq::Unindent->string(q~
285 Dies ist
286 ein Text
287 ~);
288
289 $text = Quiq::Unindent->string("
290 Dies ist
291 ein Text mit $variable
292 ");
293
294 $text = Quiq::Unindent->string(qq~
295 Dies ist
296 ein Text mit $variable
297 ~);
298  
299 =back
300  
301 =cut
302  
303 # -----------------------------------------------------------------------------
304  
305 34 ▶sub string {
Subroutine string: called CC=3 SCAR=11.0
Pod documented
306 34 my $class = shift;
307 34 ▶ my $char = @_ == 2? shift: undef;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 ? : 3 31 34
308 34 ▶ my $str = shift // return '';
Condition50%1/2
shift() // (return "")
l!l
340
MC/DC0%0/1
shift() // (return "")
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
309  
310 34 $str =~ s/^\n//;
311 34 $str =~ s/ +$//;
312  
313 34 return $class->hereDoc($char,$str);
314 }
315  
316 # -----------------------------------------------------------------------------
317  
318 =head3 trim() - Entferne Einrückung und Whitespace am Anfang und Ende
319  
320 =head4 Synopsis
321  
322 $strOut = $class->trim($strIn);
323 $strOut = $class->trim($char,$strIn);
324  
325 =head4 Arguments
326  
327 =over 4
328  
329 =item $char (optional)
330  
331 Fortsetzungszeichen. Siehe Quiq::String->joinLines().
332  
333 =item $strIn
334  
335 Eingerückter Text.
336  
337 =back
338  
339 =head4 Description
340  
341 Wie die Methoden L<hereDoc|"hereDoc() - Entferne Einrückung von "Here Document"">() und L<string|"string() - Entferne Einrückung von mehrzeiligem String-Literal">(), wobei über die
342 Einrückung hinaus
343  
344 =over 2
345  
346 =item *
347  
348 alle Leerzeilen am Anfang entfernt werden
349  
350 =item *
351  
352 jeglicher Leerraum am Ende entfernt wird
353  
354 =back
355  
356 Diese (zusätzlichen) Manipulationen sorgen dafür, dass der Text
357 als solches - d.h. ohne Einrückung und ohne Leerraum am Anfang und
358 am Ende - geliefert wird.
359  
360 Die Methode ist speziell für die I<interne> Bearbeitung eines
361 mehrzeiligen, ggf. mit einer Einrückung versehenen Parameterns
362 geeignet.
363  
364 =head4 Examples
365  
366 =over 4
367  
368 =item 1.
369  
370 Leerraum am Anfang und am Ende wird entfernt
371  
372 {
373 $text = Quiq::Unindent->trim("
374
375 SELECT
376 *
377 FROM
378 person
379 WHERE
380 nachname = 'Schulz'
381
382 ");
383 }
384  
385 ergibt
386  
387 |SELECT
388 | *
389 |FROM
390 | person
391 |WHERE
392 | nachname = 'Schulz'
393 ^
394 kein Newline
395  
396 =item 2.
397  
398 Interne Anwendung
399  
400 sub select {
401 my ($self,$stmt) = @_;
402
403 $stmt = Quiq::Unindent->trim($stmt);
404 if ($self->debug) {
405 warn $stmt,"\n";
406 }
407 ...
408 }
409  
410 Aufruf mit eingerücktem String-Literal, das I<intern> behandelt wird:
411  
412 $db->select("
413 SELECT
414 *
415 FROM
416 person
417 WHERE
418 nachname = 'Schulz'
419 ");
420  
421 =back
422  
423 =cut
424  
425 # -----------------------------------------------------------------------------
426  
427 91 ▶sub trim {
Subroutine trim: called CC=3 SCAR=11.0
Pod documented
428 91 my $class = shift;
429 91 ▶ my $char = @_ == 2? shift: undef;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 ? : 30 61 91
430 91 ▶ my $str = shift // return '';
Condition100%2/2
shift() // (return "")
l!l
847
MC/DC100%1/1
shift() // (return "")
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
431  
432 84 $str =~ s/^\s*\n//;
433 84 $str =~ s/\s+$//;
434  
435 84 return $class->hereDoc($char,$str);
436 }
437  
438 # -----------------------------------------------------------------------------
439  
440 =head3 trimNl() - Trim plus Newline
441  
442 =head4 Synopsis
443  
444 $strOut = $class->trimNl($strIn);
445 $strOut = $class->trimNl($char,$strIn);
446  
447 =head4 Arguments
448  
449 =over 4
450  
451 =item $char (optional)
452  
453 Fortsetzungszeichen. Siehe Quiq::String->joinLines().
454  
455 =item $strIn
456  
457 Eingerückter Text.
458  
459 =back
460  
461 =head4 Description
462  
463 Wie die Methode L<trim|"trim() - Entferne Einrückung und Whitespace am Anfang und Ende">(), jedoch wird am Ende genau ein Newline
464 angehängt, sofern der Sting nicht leer ist.
465  
466 =cut
467  
468 # -----------------------------------------------------------------------------
469  
470 31 ▶sub trimNl {
Subroutine trimNl: called CC=4 SCAR=13.9
Pod documented
471 31 my $class = shift;
472 31 ▶ my $char = @_ == 2? shift: undef;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 ? : 0 31 31
473 31 ▶ my $str = shift // return '';
Condition100%2/2
shift() // (return "")
l!l
301
MC/DC100%1/1
shift() // (return "")
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
474  
475 30 $str = $class->trim($char,$str);
476 30 ▶ if ($str ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($str ne "") 29 1 30
477 29 $str .= "\n";
478 }
479  
480 30 return $str;
481 }
482  
483 # -----------------------------------------------------------------------------
484  
485 =head1 VERSION
486  
487 1.240
488  
489 =head1 AUTHOR
490  
491 Frank Seitz, L<http://fseitz.de/>
492  
493 =head1 COPYRIGHT
494  
495 Copyright (C) 2026 Frank Seitz
496  
497 =head1 LICENSE
498  
499 This code is free software; you can redistribute it and/or modify
500 it under the same terms as Perl itself.
501  
502 =cut
503  
504 # -----------------------------------------------------------------------------
505  
506 270 1;
507  
508 # eof
« Quiq/Progress.pm ↑ Summary Quiq/Database/Tree.pm »