Quiq/Program.pm

filter: Statementstmt 44.7% 90 / 201 Branchbran 18.7% 12 / 64 Conditioncond 9.5% 2 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 57.8% 22 / 38 Podpod 100% 23 / 23 totaltotal 41.3% 149 / 360 CC 43 SCAR 33.6
CC
43
Cov
36%
help
110.0
parameters
90.0
log
20.0
CRAP 28.7
« Quiq/Html/HorizontalMenu.pm ↑ Summary Quiq/Udl.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Program - Basisklasse für Programme
8  
9 =head1 BASE CLASSES
10  
11 =over 2
12  
13 =item *
14  
15 L<Quiq::Process>
16  
17 =item *
18  
19 L<Quiq::Hash>
20  
21 =back
22  
23 =head1 SYNOPSIS
24  
25 Programm:
26  
27 #!/usr/bin/env perl
28
29 use Quiq::Program;
30 exit Quiq::Program->run('MyProg')->exitCode;
31
32 # eof
33  
34 Programm-Klasse:
35  
36 package MyProg;
37 use base 'Quiq::Program';
38
39 sub main {
40 my $self = shift;
41 ...
42 return;
43 }
44
45 # eof
46  
47 Optionen und Argumente:
48  
49 my ($error,$opt,$argA) = $self->options(
50 ...
51 -help => 0,
52 );
53 if ($error) {
54 $self->help(10,"ERROR: $error");
55 }
56 elsif ($opt->help) {
57 $self->help;
58 }
59 elsif (@$argA != 1) {
60 $self->help(11,'ERROR: Falsche Anzahl Argumente');
61 }
62 my $myArg = shift @$argA;
63 ...
64  
65 =cut
66  
67 # -----------------------------------------------------------------------------
68  
69 package Quiq::Program;
70 1 ▶use base qw/Quiq::Process Quiq::Hash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
75  
76 1 our $VERSION = '1.240';
77  
78 1 ▶use Quiq::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
79 1 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
80 1 ▶use Quiq::Parameters;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 1 ▶use Quiq::Assert;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 1 ▶use Quiq::Option;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 1 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
84 1 ▶use Quiq::FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
85 1 ▶use PerlIO::encoding;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 1 ▶use Quiq::System;
Subroutine BEGIN: called CC=1 SCAR=0.0
87 1 ▶use Quiq::Hash;
Subroutine BEGIN: called CC=1 SCAR=0.0
88  
89 # -----------------------------------------------------------------------------
90  
91 =head1 METHODS
92  
93 =head2 Programmsteuerung
94  
95 =head3 run() - Führe Programm-Klassen aus
96  
97 =head4 Synopsis
98  
99 $prg = Quiq::Program->run($programClass,@options);
100  
101 =head4 Options
102  
103 Siehe Methode L<new|"new() - Instantiiere Programm-Objekt">()
104  
105 =cut
106  
107 # -----------------------------------------------------------------------------
108  
109 2 ▶sub run {
Subroutine run: called CC=8 SCAR=28.5
Pod documented
110 2 my $class = shift;
111 2 ▶ my $programClass = shift || $class;
Condition33%1/3
shift() || $class
l!l&&r!l&&!r
020
MC/DC0%0/2
shift() || $class
shift() $class
Truth table33%1/3
A || B
A= shift()B= $class
ABresult
1X1
011
000
112 # @_: @options
113  
114 # Änderungen an STD*-Dateihandles lokal beschränken
115  
116 2 local *STDIN = *STDIN;
117 2 local *STDOUT = *STDOUT;
118 2 local *STDERR = *STDERR;
119  
120 2 my $self = $class->new(@_);
121 2 eval {
122 2 Quiq::Perl->loadClass($programClass);
123 2 $self->rebless($programClass);
124  
125 2 my @arr = $self->main;
126 2 ▶ while (@arr) {
Branch: 1 branch
BranchTrueFalseTotal
if (@arr) 0 2 2
127 0 my $key = shift @arr;
128 0 ▶ if ($key eq 'programClass') {
Branch: 2 branches
BranchTrueFalseTotal
if ($key eq 'programClass') { } 0 0 0
elsif ($key eq 'methods') { } 0 0 0
129 0 my $subProgramClass = shift @arr;
130 0 Quiq::Perl->loadClass($subProgramClass);
131 0 $self->rebless($subProgramClass);
132 }
133 elsif ($key eq 'methods') {
134 0 my $methodA = shift @arr;
135 0 for my $meth (@$methodA) {
136 0 $self->$meth;
137 }
138 }
139 else {
140 0 die "Unbekannte Direktive: $key\n";
141 }
142 }
143 };
144 2 ▶ if ($@ && $@ !~ /^exit: \d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ and not $@ =~ /^exit: \d+$/) 0 2 2
Condition33%1/3
$@ and not $@ =~ /^exit: \d+$/
!ll&&!rl&&r
200
MC/DC0%0/2
$@ and not $@ =~ /^exit: \d+$/
$@ not $@ =~ /^exit: \d+$/
Truth table33%1/3
A and not(B)
A= $@B= not $@ =~ /^exit: \d+$/
ABresult
0X0
100
111
145 0 $self->catch($@);
146 }
147 2 $self->finish;
148  
149 2 return $self;
150 }
151  
152 # -----------------------------------------------------------------------------
153  
154 =head3 exit() - Terminiere das Programm
155  
156 =head4 Synopsis
157  
158 $prg->exit;
159 $prg->exit($exitCode);
160  
161 =head4 Description
162  
163 Terminiere das Programm mit Exitcode $exitCode. Ist kein Exitcode
164 angegeben, terminiere mit dem Exitcode der auf dem Programmobjekt
165 gesetzt ist. Die Methode kehrt nicht zurück. Nach ihrem Aufruf wird
166 die Methode L<finish|"finish() - Abschließender Code vor Programmende">() ausgeführt.
167  
168 =cut
169  
170 # -----------------------------------------------------------------------------
171  
172 0 ▶sub exit {
Subroutine exit: not called CC=2 SCAR=17.9
Pod documented
173 0 my $self = shift;
174 # @_: $exitCode
175  
176 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
177 0 $self->exitCode(shift);
178 }
179
180 0 die sprintf "exit: %s\n",$self->exitCode;
181 }
182  
183 # -----------------------------------------------------------------------------
184  
185 =head2 Getter/Setter
186  
187 =head3 exitCode() - Liefere/Setze Exitcode
188  
189 =head4 Synopsis
190  
191 $exitCode = $prg->exitCode;
192 $exitCode = $prg->exitCode($exitCode);
193  
194 =cut
195  
196 # -----------------------------------------------------------------------------
197  
198 0 ▶sub exitCode {
Subroutine exitCode: not called CC=2 SCAR=17.9
Pod documented
199 0 my $self = shift;
200 # @_: $exitCode
201  
202 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
203 0 $self->{'exitCode'} = shift;
204 }
205  
206 0 return $self->{'exitCode'};
207 }
208  
209 # -----------------------------------------------------------------------------
210  
211 =head3 name() - Name des Programms
212  
213 =head4 Synopsis
214  
215 $name = $this->name;
216  
217 =head4 Description
218  
219 Liefere den Namen des Programms. Der Programmname ist die letzte
220 Pfadkomponente von $0.
221  
222 =cut
223  
224 # -----------------------------------------------------------------------------
225  
226 0 ▶sub name {
Subroutine name: not called CC=1 SCAR=6.9
Pod documented
227 0 my $this = shift;
228 0 (my $name = $0) =~ s|.*/||;
229 0 return $name;
230 }
231  
232 # -----------------------------------------------------------------------------
233  
234 =head2 Programmcode
235  
236 =head3 main() - Hauptprogramm
237  
238 =head4 Synopsis
239  
240 $prg->main;
241  
242 =cut
243  
244 # -----------------------------------------------------------------------------
245  
246 1 ▶sub main {
Subroutine main: called CC=1 SCAR=0.0
247 1 my $self = shift;
248 1 return;
249 }
250  
251 # -----------------------------------------------------------------------------
252  
253 =head3 catch() - Fange und behandle unbehandelte Exception
254  
255 =head4 Synopsis
256  
257 $prg->catch($exception);
258  
259 =head4 Description
260  
261 Exception-Handler, der unbehandelte Exceptions der Anwendung fängt.
262 Kann von der Programmklasse bei Bedarf überschrieben werden.
263 Das Default-Verhalten ist, dass der Exception-Text auf STDERR
264 ausgegeben und der Exitcode auf 99 gesetzt wird.
265  
266 Das Programm terminiert nicht sofort, sondern die Methode
267 L<finish|"finish() - Abschließender Code vor Programmende">() wird noch ausgeführt.
268  
269 =cut
270  
271 # -----------------------------------------------------------------------------
272  
273 0 ▶sub catch {
Subroutine catch: not called CC=1 SCAR=6.9
Pod documented
274 0 my ($self,$exception) = @_;
275  
276 0 warn $exception;
277 0 $self->exitCode(99);
278  
279 0 return;
280 }
281  
282 # -----------------------------------------------------------------------------
283  
284 =head3 finish() - Abschließender Code vor Programmende
285  
286 =head4 Synopsis
287  
288 $prg->finish;
289  
290 =cut
291  
292 # -----------------------------------------------------------------------------
293  
294 2 ▶sub finish {
Subroutine finish: called CC=1 SCAR=0.0
Pod documented
295 2 my $self = shift;
296 2 return;
297 }
298  
299 # -----------------------------------------------------------------------------
300  
301 =head2 Umgebung
302  
303 =head3 env() - Liefere/Setze Environment-Hash
304  
305 =head4 Synopsis
306  
307 $envH = $this->env;
308 $envH = $this->env(\%env);
309  
310 =cut
311  
312 # -----------------------------------------------------------------------------
313  
314 2 ▶sub env {
Subroutine env: called CC=2 SCAR=6.9
Pod documented
315 2 my $this = shift;
316 # @_: $envH
317  
318 2 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 1 2
319 1 %ENV = %{(shift)};
320 }
321  
322 2 return \%ENV;
323 }
324  
325 # -----------------------------------------------------------------------------
326  
327 =head3 argv() - Liefere/Setze Argument-Array
328  
329 =head4 Synopsis
330  
331 $argA|@args = $this->argv;
332 $argA|@args = $this->argv(\@argv);
333  
334 =cut
335  
336 # -----------------------------------------------------------------------------
337  
338 2 ▶sub argv {
Subroutine argv: called CC=3 SCAR=11.0
Pod documented
339 2 my $this = shift;
340 # @_: $argA
341  
342 2 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 1 2
343 1 @ARGV = @{(shift)};
344 }
345  
346 2 ▶ return wantarray? @ARGV: \@ARGV;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 2 2
347 }
348  
349 # -----------------------------------------------------------------------------
350  
351 =head3 stdin() - Liefere/Setze STDIN Filehandle
352  
353 =head4 Synopsis
354  
355 $fh = $this->stdin;
356 $fh = $this->stdin($fh);
357  
358 =cut
359  
360 # -----------------------------------------------------------------------------
361  
362 0 ▶sub stdin {
Subroutine stdin: not called CC=2 SCAR=17.9
Pod documented
363 0 my $this = shift;
364 # @_: $fh
365  
366 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
367 0 *STDIN = shift;
368 }
369  
370 0 return \*STDIN;
371 }
372  
373 # -----------------------------------------------------------------------------
374  
375 =head3 stdout() - Liefere/Setze STDOUT Filehandle
376  
377 =head4 Synopsis
378  
379 $fh = $this->stdout;
380 $fh = $this->stdout($fh);
381  
382 =cut
383  
384 # -----------------------------------------------------------------------------
385  
386 1 ▶sub stdout {
Subroutine stdout: called CC=2 SCAR=7.0
Pod documented
387 1 my $this = shift;
388 # @_: $fh
389  
390 1 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 0 1
391 1 *STDOUT = shift;
392 }
393  
394 1 return \*STDOUT;
395 }
396  
397 # -----------------------------------------------------------------------------
398  
399 =head3 stderr() - Liefere/Setze STDERR Filehandle
400  
401 =head4 Synopsis
402  
403 $fh = $this->stderr;
404 $fh = $this->stderr($fh);
405  
406 =cut
407  
408 # -----------------------------------------------------------------------------
409  
410 1 ▶sub stderr {
Subroutine stderr: called CC=2 SCAR=7.0
Pod documented
411 1 my $this = shift;
412 # @_: $fh
413  
414 1 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 0 1
415 1 *STDERR = shift;
416 }
417  
418 1 return \*STDERR;
419 }
420  
421 # -----------------------------------------------------------------------------
422  
423 =head2 Character Encoding
424  
425 =head3 encoding() - Standard-Encoding
426  
427 =head4 Synopsis
428  
429 $encoding = $prg->encoding;
430  
431 =head4 Description
432  
433 Liefere das Standard-Encoding, das in der Systemumgebung eingestellt ist.
434 Im Konstruktor werden STDIN, STDOUT und STDERR auf dieses Encoding
435 eingestellt, d.h. Eingaben und Ausgaben automatisch gemäß dieses
436 Encodings gewandelt.
437  
438 =cut
439  
440 # -----------------------------------------------------------------------------
441  
442 0 ▶sub encoding {
Subroutine encoding: not called CC=1 SCAR=6.9
Pod documented
443 0 return shift->{'encoding'};
444 }
445  
446 # -----------------------------------------------------------------------------
447  
448 =head3 decode() - Dekodiere Zeichenkette gemäß Standard-Encoding
449  
450 =head4 Synopsis
451  
452 $str = $prg->decode($str);
453  
454 =cut
455  
456 # -----------------------------------------------------------------------------
457  
458 0 ▶sub decode {
Subroutine decode: not called CC=1 SCAR=6.9
Pod documented
459 0 my $self = shift;
460 # @_: $str
461 0 return Encode::decode($self->encoding,$_[0]);
462 }
463  
464 # -----------------------------------------------------------------------------
465  
466 =head3 encode() - Enkodiere Zeichenkette gemäß Standard-Encoding
467  
468 =head4 Synopsis
469  
470 $str = $prg->encode($str);
471  
472 =cut
473  
474 # -----------------------------------------------------------------------------
475  
476 0 ▶sub encode {
Subroutine encode: not called CC=1 SCAR=6.9
Pod documented
477 0 my $self = shift;
478 # @_: $str
479 0 return Encode::encode($self->encoding,$_[0]);
480 }
481  
482 # -----------------------------------------------------------------------------
483  
484 =head2 Parameter
485  
486 =head3 parameters() - Argumente und Optionen des Programmaufrufs
487  
488 =head4 Synopsis
489  
490 [1] ($argA,$opt) = $prg->parameters($sloppy,$minArgs,$maxArgs,@optVal);
491 [2] $opt = $prg->parameters($sloppy,0,0,@optVal);
492 [3] $argA = $prg->parameters($sloppy,$minArgs,$maxArgs,@optVal);
493  
494 =head4 Arguments
495  
496 =over 4
497  
498 =item $sloppy
499  
500 Wirf keine Exception, wenn unerwartete Parameter (also Optionen und
501 Arumente) in @ARGV enthalten sind. Diese Parameter bleiben in @ARGV
502 stehen.
503  
504 =item $minArgs
505  
506 Mindestanzahl an Argumenten.
507  
508 =item $maxArgs
509  
510 Maximale Anzahl an Argumenten.
511  
512 =item @optVal
513  
514 Liste der Optionen und ihrer Defaultwerte.
515  
516 =back
517  
518 =head4 Returns
519  
520 =over 4
521  
522 =item $opt
523  
524 Hash-Objekt mit den Optionen.
525  
526 =item $argA
527  
528 Referenz auf Array mit mindestens $minArgs und höchstens
529 $maxArgs Argumenten.
530  
531 =back
532  
533 =head4 Description
534  
535 Liefere die Argumente und Optionen des Programmaufs. Werden weniger als
536 $minArgs oder mehr als $maxArgs Argumente oder nicht deklarierte
537 Optionen übergeben, wird eine Exception geworfen. Ist $sloppy gesetzt,
538 wird im Falle überzähliger Parameter I<keine> Exception geworfen.
539 Die überzähligen Parameter bleiben in @ARGV erhalten.
540  
541 Im Skalarkontext wird nur $opt geliefert, wenn keine Argumente erwartet
542 werden ($minArgs und $maxArgs sind 0), andernfalls $argA. Letzteres
543 ist nützlich, wenn C<-help> die einzige Option ist.
544  
545 =cut
546  
547 # -----------------------------------------------------------------------------
548  
549 0 ▶sub parameters {
Subroutine parameters: not called CC=9 SCAR=45.0
Pod documented
550 0 my ($self,$sloppy,$minArgs,$maxArgs) = splice @_,0,4;
551  
552 0 my ($argA,$opt) = Quiq::Parameters->extract(0,0,
553 $self->encoding,\@ARGV,$maxArgs,@_);
554 0 ▶ if ($opt->exists('help') && $opt->help) {
Branch: 3 branches
BranchTrueFalseTotal
if ($opt->exists('help') and $opt->help) { } 0 0 0
elsif (@$argA < $minArgs) { } 0 0 0
elsif (@ARGV and not $sloppy) { } 0 0 0
Condition0%0/3
$opt->exists("help") and $opt->help
!ll&&!rl&&r
000
MC/DC0%0/2
$opt->exists("help") and $opt->help
$opt->exists("help") $opt->help
Truth table0%0/3
A and B
A= $opt->exists("help")B= $opt->help
ABresult
0X0
100
111
555 0 $self->help;
556 }
557 elsif (@$argA < $minArgs) {
558 0 $self->help(11,'ERROR: Missing arguments');
559 }
560 ▶ elsif (@ARGV && !$sloppy) {
Condition0%0/3
@ARGV and not $sloppy
!ll&&!rl&&r
000
MC/DC0%0/2
@ARGV and not $sloppy
@ARGV not $sloppy
Truth table0%0/3
A and not(B)
A= @ARGVB= not $sloppy
ABresult
0X0
100
111
561 0 $self->help(12,"ERROR: Unexpected parameter(s): @ARGV");
562 }
563  
564 0 ▶ return wantarray? ($argA,$opt):
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 0 0 0
defined $maxArgs && $maxArgs == 0 ? : 0 0 0
Condition0%0/3
defined $maxArgs and $maxArgs == 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $maxArgs and $maxArgs == 0
defined $maxArgs $maxArgs == 0
Truth table0%0/3
A and B
A= defined $maxArgsB= $maxArgs == 0
ABresult
0X0
100
111
565 defined($maxArgs) && $maxArgs == 0? $opt: $argA;
566 }
567  
568 # -----------------------------------------------------------------------------
569  
570 =head2 Zusicherungen
571  
572 =head3 assert() - Prüfe Werte
573  
574 =head4 Synopsis
575  
576 $prg->assert(sub {...});
577  
578 =head4 Description
579  
580 Prüfe Werte durch Methoden der Klasse Quiq::Assert. Ist eine
581 Zusicherung verletzt, wird die betreffende Exception in die
582 Ausgabe der Programm-Hilfeseite umgesetzt. Die Subroutine
583 erhält als Argument ein instantiiertes Quiq::Assert-Objekt.
584  
585 =head4 Example
586  
587 Prüfe die Werte der Variablen $system und $user gegen eine Menge
588 möglicher Werte:
589
590 $self->assert(sub {
591 my $a = shift;
592 $a->isEnumValue($system,['test','prod'],
593 -name=>'SYSTEM',
594 );
595 $a->isEnumValue($user,[qw/etlt etls etlr etlp/],
596 -name=>'USER',
597 );
598 });
599  
600 =cut
601  
602 # -----------------------------------------------------------------------------
603  
604 0 ▶sub assert {
Subroutine assert: not called CC=2 SCAR=17.9
Pod documented
605 0 my ($self,$sub) = @_;
606  
607 0 my $a = Quiq::Assert->new(
608 stacktrace => 0,
609 nameSection => 'Parameter',
610 );
611  
612 0 eval {$sub->($a)};
613 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
614 0 $@ =~ s/ASSERT-\d+: //;
615 0 $self->help(10,$@);
616 }
617  
618 0 return;
619 }
620  
621 # -----------------------------------------------------------------------------
622  
623 =head2 Optionen
624  
625 =head3 options() - Verarbeite Programmoptionen (DEPRECATED)
626  
627 =head4 Synopsis
628  
629 ($error,$optH,$argA) = $prg->options(@keyVal);
630  
631 =head4 Description
632  
633 FIXME: Veraltete Methode. Alle Stellen, wo die Methode options()
634 genutzt wird, auf parameters() portieren.
635  
636 =cut
637  
638 # -----------------------------------------------------------------------------
639  
640 0 ▶sub options {
Subroutine options: not called CC=3 SCAR=24.8
Pod documented
641 0 my $self = shift;
642 # @_: @keyVal
643  
644 0 my $argA = $self->argv;
645  
646 # Dekodiere die Programmargumente (2017-06-06)
647 # ACHTUNG: älterer Code dekodiert möglicherweise selbst!
648  
649 0 my $enc = $self->encoding;
650 0 for my $arg (@$argA) {
651 0 $arg = Encode::decode($enc,$arg);
652 }
653  
654 0 my $optH = eval{Quiq::Option->extract(
655 -simpleMessage => 1,
656 # -mode => 'sloppy',
657 $argA,
658 @_
659 )};
660 0 ▶ if (!$@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 0 0
661 0 $self->set(optH=>$optH);
662 }
663  
664 0 return ($@,$optH,$argA);
665 }
666  
667 # -----------------------------------------------------------------------------
668  
669 =head3 opt() - Liefere Optionsobjekt oder Optionswerte
670  
671 =head4 Synopsis
672  
673 $val = $prg->opt($key); # [1]
674 @vals = $prg->opt(@keys); # [2]
675 $optH = $prg->opt; # [3]
676  
677 =cut
678  
679 # -----------------------------------------------------------------------------
680  
681 0 ▶sub opt {
Subroutine opt: not called CC=2 SCAR=17.9
Pod documented
682 0 my $self = shift;
683 # @_: $key -or- @keys -or- ()
684  
685 0 my $optH = $self->{'optH'};
686 0 ▶ if (!@_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 0 0 0
687 0 return $optH;
688 }
689  
690 0 return $optH->get(@_);
691 }
692  
693 # -----------------------------------------------------------------------------
694  
695 =head2 Verzeichnisse
696  
697 =head3 projectDir() - Projektverzeichnis
698  
699 =head4 Synopsis
700  
701 $dir = $prg->projectDir($depth);
702  
703 =head4 Description
704  
705 Liefere den Verzeichnispfad, der $depth Stufen oberhalb des
706 Verzeichnisses endet, in dem das Programm installiert ist.
707  
708 Der Installationspfad wird anhand von $0 ermittelt. Wurde das
709 Programm mit einem relativen Pfad aufgerufen, wird dieser zu einem
710 absoluten Pfad komplettiert.
711  
712 =head4 Example
713  
714 Wurde das Programm myprog unter dem Pfad
715  
716 /opt/myapp/bin/myprog
717  
718 installiert, dann liefert $prg->projectDir(1) den Pfad
719  
720 /opt/myapp
721  
722 als Projektverzeichnis.
723  
724 =cut
725  
726 # -----------------------------------------------------------------------------
727  
728 0 ▶sub projectDir {
Subroutine projectDir: not called CC=2 SCAR=17.9
Pod documented
729 0 my ($self,$depth) = @_;
730  
731 0 my $path = $0;
732 0 ▶ if ($path !~ m|^/|) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path =~ m[^/]) 0 0 0
733 # Einen relativen Pfad machen wir zu einem absoluten
734 # Pfad, indem wir ihn um das aktuelle Verzeichnis ergänzen.
735  
736 0 $path =~ s|^./||;
737 0 $path = sprintf '%s/%s',$self->cwd,$path;
738 }
739  
740 # InstallDir bestimmen, indem wir das Programm und $depth
741 # Verzeichnisse darüber vom Pfad entfernen
742  
743 0 my @path = split m|/|,$path;
744 0 splice @path,-($depth+1);
745 0 my $dir = join '/',@path;
746  
747 0 return $dir;
748 }
749  
750 # -----------------------------------------------------------------------------
751  
752 =head2 Zeit
753  
754 =head3 elapsed() - Vergangene Zeit in Sekunden
755  
756 =head4 Synopsis
757  
758 $sec = $prg->elapsed;
759  
760 =head4 Returns
761  
762 Sekunden (Float)
763  
764 =head4 Description
765  
766 Ermittele die vergangene Zeit in Sekunden und liefere diese zurück.
767  
768 =cut
769  
770 # -----------------------------------------------------------------------------
771  
772 0 ▶sub elapsed {
Subroutine elapsed: not called CC=1 SCAR=6.9
Pod documented
773 0 my $self = shift;
774 0 return Time::HiRes::gettimeofday-$self->{'t0'};
775 }
776  
777 # -----------------------------------------------------------------------------
778  
779 =head2 Logging
780  
781 =head3 log() - Schreibe Meldung nach STDERR
782  
783 =head4 Synopsis
784  
785 $prg->log($fmt,@args);
786 $prg->log($level,$fmt,@args);
787  
788 =head4 Description
789  
790 Schreibe eine Logmeldung nach STDERR, wenn $level größer oder gleich
791 dem eingestellten Loglevel ($prg->logLevel) ist. Ist $level nicht
792 angegeben, wird 1 angenommen.
793  
794 Die Logmeldung wird per
795  
796 printf STDERR $fmt,@args;
797  
798 erzeugt. Endet $fmt nicht mit einem Newline, wird es hinzugefügt.
799  
800 Per Default ist der LogLevel 0. Er wird mit
801  
802 $prg->logLevel($n); # $n > 0
803  
804 eingestellt.
805  
806 =head4 Caveats
807  
808 =over 2
809  
810 =item *
811  
812 Ist $fmt eine Zahl, muss der Level $level explizit angegeben werden.
813  
814 =item *
815  
816 Die Argumente der Methode werden I<immer> ausgewertet, auch wenn
817 kein Logging erfolgt. Ist damit ein größerer Aufwand verbunden,
818 kann es sinnvoll sein, eine Bedingung zu formulieren:
819  
820 if ($level >= $prg->logLevel) {
821 # $msg mit großem Aufwand erzeugen
822 $prg->log($level,$msg);
823 }
824  
825 =back
826  
827 =cut
828  
829 # -----------------------------------------------------------------------------
830  
831 0 ▶sub log {
Subroutine log: not called CC=4 SCAR=30.0
Pod documented
832 0 my $self = shift;
833 0 ▶ my $level = $_[0] =~ /^\d+$/? shift: 1;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /^\d+$/ ? : 0 0 0
834 # @_: $fmt,@args
835  
836 0 ▶ if ($self->logLevel >= $level) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->logLevel >= $level) 0 0 0
837 0 my $fmt = shift;
838 0 ▶ if (substr($fmt,-1,1) ne "\n") {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($fmt, -1, 1) ne "\n") 0 0 0
839 0 $fmt .= "\n";
840 }
841 0 printf STDERR $fmt,@_;
842 }
843  
844 0 return;
845 }
846  
847 # -----------------------------------------------------------------------------
848  
849 =head2 Hilfe
850  
851 =head3 help() - Gib Hilfetext aus und beende Programm
852  
853 =head4 Synopsis
854  
855 $self->help;
856 $self->help($exitCode);
857 $self->help($exitCode,$msg);
858  
859 =head4 Description
860  
861 Der Hilfetext wird aus der POD-Dokumentation des Programms generiert.
862  
863 =over 2
864  
865 =item *
866  
867 Ist $exitCode == 0, wird der Hilfetext auf STDOUT ausgegeben.
868 Ist $exitCode != 0, wird der Hilfetext auf STDERR ausgegeben.
869  
870 =item *
871  
872 Ist $msg angegeben, wird die Hilfeseite oben und unten um Text $msg
873 ergänzt (jeweils mit Leerzeile abgetrennt).
874  
875 =item *
876  
877 Ist $exitCode == 0 und STDOUT mit einem Terminal verbunden, wird
878 der Hilfetext im Pager dargestellt (Environment-Variable $PAGER
879 oder less).
880  
881 =back
882  
883 =cut
884  
885 # -----------------------------------------------------------------------------
886  
887 0 ▶sub help {
Subroutine help: not called CC=10 SCAR=47.0
Pod documented
888 0 my $self = shift;
889 0 ▶ my $exitCode = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
890 0 ▶ my $msg = shift || '';
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
891  
892 # Encoding des POD-Dokuments ermitteln
893  
894 0 my $podEncoding = 'iso-8859-1';
895 0 my $fh = Quiq::FileHandle->new('<',$0);
896 0 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
897 0 chomp;
898 0 ▶ if (/^=encoding\s+(\S+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^=encoding\s+(\S+)/) 0 0 0
899 0 $podEncoding = $1;
900 0 last;
901 }
902 }
903 0 $fh->close;
904  
905 # Doku erzeugen und dekodieren
906  
907 0 ▶ my $text = -t STDOUT? qx/pod2text --overstrike $0/: qx/pod2text $0/;
Branch: 1 branch
BranchTrueFalseTotal
-t STDOUT ? : 0 0 0
908 0 $text = Encode::decode($podEncoding,$text);
909  
910 0 ▶ if ($msg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg) 0 0 0
911 0 $msg =~ s/\n+$//;
912 0 $text =~ s/\n+$//;
913 0 $text = "$msg\n-----\n$text\n-----\n$msg\n";
914 }
915  
916 # Doku anzeigen
917  
918 0 ▶ if ($exitCode) {
Branch: 2 branches
BranchTrueFalseTotal
if ($exitCode) { } 0 0 0
elsif (-t STDOUT) { } 0 0 0
919 # Ausgabe auf STDERR
920 0 print STDERR $text;
921 }
922 elsif (-t STDOUT) {
923 # Anzeige im Pager
924  
925 0 ▶ my $pager = $ENV{'PAGER'} || 'less -i';
Condition0%0/2
$ENV{'PAGER'} || 'less -i'
l!l
00
MC/DC0%0/1
$ENV{'PAGER'} || 'less -i'
$ENV{'PAGER'}
Truth table0%0/2
A
A= $ENV{'PAGER'}
Aresult
11
00
926 0 my $encoding = $self->encoding;
927 0 my $fh = Quiq::FileHandle->new('|-',
928 "LESSCHARSET=$encoding $pager");
929 0 $fh->binmode(":encoding($encoding)");
930 0 $fh->print($text);
931 0 $fh->close;
932 }
933 else {
934 # Ausgabe auf STDOUT
935 0 print $text;
936 }
937  
938 0 $self->exit($exitCode);
939 }
940  
941 # -----------------------------------------------------------------------------
942  
943 =head2 Instantiierung
944  
945 =head3 new() - Instantiiere Programm-Objekt
946  
947 =head4 Synopsis
948  
949 $prg = $class->new(@options);
950  
951 =head4 Options
952  
953 =over 4
954  
955 =item -argv=>\@arr (Default: \@ARGV)
956  
957 Setze Programm-Argumente auf @arr.
958  
959 =item -env=>\%hash (Default: \%ENV)
960  
961 Setze Programm-Environment auf %hash.
962  
963 =item -stdin=>$fh (Default: \*STDIN)
964  
965 Setze STDIN des Programms auf $fh.
966  
967 =item -stdout=>$fh (Default: \*STDOUT)
968  
969 Setze STDOUT des Programms auf $fh.
970  
971 =item -stderr=>$fh (Default: \*STDERR)
972  
973 Setze STDERR des Programms auf $fh.
974  
975 =back
976  
977 =cut
978  
979 # -----------------------------------------------------------------------------
980  
981 2 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
982 2 my $class = shift;
983 # @_: @options
984  
985 # Optionen
986  
987 2 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 4 2 6
988 4 my $key = shift;
989 4 ▶ if ($key =~ /^(-env|-argv|-stdin|-stdout|-stderr)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^(-env|-argv|-stdin|-stdout|-stderr)$/) { } 4 0 4
990 4 $key = substr $key,1;
991 4 $class->$key(shift);
992 }
993 else {
994 0 $class->throw(
995 'PROG-00001: Unbekannte Option',
996 Option => $key,
997 );
998 }
999 }
1000  
1001 # STDIN, STDOUT, STDERR auf Systemencoding einstellen
1002  
1003 2 my $encoding = Quiq::System->encoding;
1004 2 Quiq::Perl->binmode(*STDIN,":encoding($encoding)");
1005 2 Quiq::Perl->binmode(*STDOUT,":encoding($encoding)");
1006 2 Quiq::Perl->binmode(*STDERR,":encoding($encoding)");
1007  
1008 # Objekt instantiieren
1009  
1010 2 return $class->SUPER::new(
1011 encoding => $encoding,
1012 exitCode => 0,
1013 logLevel => 0,
1014 optH => Quiq::Hash->new,
1015 t0 => Time::HiRes::gettimeofday,
1016 );
1017 }
1018  
1019 # -----------------------------------------------------------------------------
1020  
1021 =head1 VERSION
1022  
1023 1.240
1024  
1025 =head1 AUTHOR
1026  
1027 Frank Seitz, L<http://fseitz.de/>
1028  
1029 =head1 COPYRIGHT
1030  
1031 Copyright (C) 2026 Frank Seitz
1032  
1033 =head1 LICENSE
1034  
1035 This code is free software; you can redistribute it and/or modify
1036 it under the same terms as Perl itself.
1037  
1038 =cut
1039  
1040 # -----------------------------------------------------------------------------
1041  
1042 1 1;
1043  
1044 # eof
« Quiq/Html/HorizontalMenu.pm ↑ Summary Quiq/Udl.pm »