| 1 | # ----------------------------------------------------------------------------- | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | =encoding utf8 | ||||||||||||||||||
| 4 | |||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||
| 6 | |||||||||||||||||||
| 7 | Quiq::Exit - Prüfe Exitstatus von Child-Prozess | ||||||||||||||||||
| 8 | |||||||||||||||||||
| 9 | =head1 BASE CLASS | ||||||||||||||||||
| 10 | |||||||||||||||||||
| 11 | L<Quiq::Object> | ||||||||||||||||||
| 12 | |||||||||||||||||||
| 13 | =head1 DESCRIPTION | ||||||||||||||||||
| 14 | |||||||||||||||||||
| 15 | Die Klasse implementiert eine einzelne Methode. Diese prüft den Status | ||||||||||||||||||
| 16 | eines terminierten Child-Prozesses. Im Fehlerfall löst sie eine | ||||||||||||||||||
| 17 | Exception aus. | ||||||||||||||||||
| 18 | |||||||||||||||||||
| 19 | =cut | ||||||||||||||||||
| 20 | |||||||||||||||||||
| 21 | # ----------------------------------------------------------------------------- | ||||||||||||||||||
| 22 | |||||||||||||||||||
| 23 | package Quiq::Exit; | ||||||||||||||||||
| 24 | 270 | ▶ | use base qw/Quiq::Object/; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 25 | |||||||||||||||||||
| 26 | 270 | ▶ | use v5.10; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 27 | 270 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 28 | 270 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 29 | |||||||||||||||||||
| 30 | 270 | our $VERSION = '1.240'; | |||||||||||||||||
| 31 | |||||||||||||||||||
| 32 | 270 | ▶ | use Cwd (); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 33 | |||||||||||||||||||
| 34 | # ----------------------------------------------------------------------------- | ||||||||||||||||||
| 35 | |||||||||||||||||||
| 36 | =head1 METHODS | ||||||||||||||||||
| 37 | |||||||||||||||||||
| 38 | =head2 Klassenmethoden | ||||||||||||||||||
| 39 | |||||||||||||||||||
| 40 | =head3 check() - Prüfe den Status eines terminierten Child-Prozesses | ||||||||||||||||||
| 41 | |||||||||||||||||||
| 42 | =head4 Synopsis | ||||||||||||||||||
| 43 | |||||||||||||||||||
| 44 | $this->check; | ||||||||||||||||||
| 45 | $this->check($exitCode); | ||||||||||||||||||
| 46 | $this->check($exitCode,$cmd); | ||||||||||||||||||
| 47 | |||||||||||||||||||
| 48 | =head4 Arguments | ||||||||||||||||||
| 49 | |||||||||||||||||||
| 50 | =over 4 | ||||||||||||||||||
| 51 | |||||||||||||||||||
| 52 | =item $exitCode (Default: $?) | ||||||||||||||||||
| 53 | |||||||||||||||||||
| 54 | (Integer) Der Returnwert von system() oder $? im Falle von qx// (bzw. ``). | ||||||||||||||||||
| 55 | |||||||||||||||||||
| 56 | =item $cmd (Default: undef) | ||||||||||||||||||
| 57 | |||||||||||||||||||
| 58 | (String) Ausgeführtes Kommando. Dieses wird im Fehlerfall | ||||||||||||||||||
| 59 | in den Exception-Text eingesetzt. | ||||||||||||||||||
| 60 | |||||||||||||||||||
| 61 | =back | ||||||||||||||||||
| 62 | |||||||||||||||||||
| 63 | =head4 Description | ||||||||||||||||||
| 64 | |||||||||||||||||||
| 65 | Prüfe den Status eines terminierten Child-Prozesses und löse | ||||||||||||||||||
| 66 | eine Execption aus, wenn dieser ungleich 0 ist. Ausnahme: Einen Abbruch | ||||||||||||||||||
| 67 | via ^C tolerieren wir. | ||||||||||||||||||
| 68 | |||||||||||||||||||
| 69 | =head4 Examples | ||||||||||||||||||
| 70 | |||||||||||||||||||
| 71 | Prüfe den Status nach Aufruf von system(): | ||||||||||||||||||
| 72 | |||||||||||||||||||
| 73 | my $r = system($cmd); | ||||||||||||||||||
| 74 | Quiq::Exit->check($r,$cmd); | ||||||||||||||||||
| 75 | |||||||||||||||||||
| 76 | Minimale Variante (Prüfung über $?): | ||||||||||||||||||
| 77 | |||||||||||||||||||
| 78 | system($cmd); | ||||||||||||||||||
| 79 | Quiq::Exit->check; | ||||||||||||||||||
| 80 | |||||||||||||||||||
| 81 | Prüfe den Status nach Anwendung des Backtick-Operators: | ||||||||||||||||||
| 82 | |||||||||||||||||||
| 83 | $str = `$cmd`; | ||||||||||||||||||
| 84 | Quiq::Exit->check($?,$cmd); | ||||||||||||||||||
| 85 | |||||||||||||||||||
| 86 | =cut | ||||||||||||||||||
| 87 | |||||||||||||||||||
| 88 | # ----------------------------------------------------------------------------- | ||||||||||||||||||
| 89 | |||||||||||||||||||
| 90 | 27 | ▶ | sub check { | ||||||||||||||||
|
Subroutine
check: called CC=6 SCAR=24.1
Pod
documented
| |||||||||||||||||||
| 91 | 27 | my $this = shift; | |||||||||||||||||
| 92 | 27 | my $exitCode = shift; | |||||||||||||||||
| 93 | 27 | my $cmd = shift; | |||||||||||||||||
| 94 | |||||||||||||||||||
| 95 | 27 | ▶ | if ($exitCode == 0) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 96 | 23 | return; # ok | |||||||||||||||||
| 97 | } | ||||||||||||||||||
| 98 | elsif ($exitCode == -1) { | ||||||||||||||||||
| 99 | 0 | $this->throw( | |||||||||||||||||
| 100 | 'CMD-00001: Failed to execute command', | ||||||||||||||||||
| 101 | Command => qq|"$cmd"|, | ||||||||||||||||||
| 102 | ErrorMessage => $!, | ||||||||||||||||||
| 103 | ); | ||||||||||||||||||
| 104 | } | ||||||||||||||||||
| 105 | elsif ($exitCode & 127) { # Abbruch mit Signal | ||||||||||||||||||
| 106 | 0 | my $sig = $exitCode & 127; # unterste 8 Bit sind Signalnummer | |||||||||||||||||
| 107 | 0 | ▶ | if ($sig == 2) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 108 | 0 | return; # Abbruch per ^C tolerieren wir | |||||||||||||||||
| 109 | } | ||||||||||||||||||
| 110 | 0 | my $core = $exitCode & 128; # das 8. Bit zeigt Coredump an | |||||||||||||||||
| 111 | 0 | ▶ | $this->throw( | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 112 | 'CMD-00003: Child died with signal', | ||||||||||||||||||
| 113 | Signal => $sig.($core? ' (Coredump)': ''), | ||||||||||||||||||
| 114 | Command => $cmd, | ||||||||||||||||||
| 115 | ErrorMessage => $!, | ||||||||||||||||||
| 116 | ); | ||||||||||||||||||
| 117 | } | ||||||||||||||||||
| 118 | 4 | $exitCode >>= 8; | |||||||||||||||||
| 119 | 4 | $this->throw( | |||||||||||||||||
| 120 | 'CMD-00002: Command failed with error', | ||||||||||||||||||
| 121 | ExitCode => $exitCode, | ||||||||||||||||||
| 122 | Command => $cmd, | ||||||||||||||||||
| 123 | Cwd => Cwd::getcwd, | ||||||||||||||||||
| 124 | ErrorMessage => $!, | ||||||||||||||||||
| 125 | ); | ||||||||||||||||||
| 126 | } | ||||||||||||||||||
| 127 | |||||||||||||||||||
| 128 | # ----------------------------------------------------------------------------- | ||||||||||||||||||
| 129 | |||||||||||||||||||
| 130 | =head1 VERSION | ||||||||||||||||||
| 131 | |||||||||||||||||||
| 132 | 1.240 | ||||||||||||||||||
| 133 | |||||||||||||||||||
| 134 | =head1 AUTHOR | ||||||||||||||||||
| 135 | |||||||||||||||||||
| 136 | Frank Seitz, L<http://fseitz.de/> | ||||||||||||||||||
| 137 | |||||||||||||||||||
| 138 | =head1 COPYRIGHT | ||||||||||||||||||
| 139 | |||||||||||||||||||
| 140 | Copyright (C) 2026 Frank Seitz | ||||||||||||||||||
| 141 | |||||||||||||||||||
| 142 | =head1 LICENSE | ||||||||||||||||||
| 143 | |||||||||||||||||||
| 144 | This code is free software; you can redistribute it and/or modify | ||||||||||||||||||
| 145 | it under the same terms as Perl itself. | ||||||||||||||||||
| 146 | |||||||||||||||||||
| 147 | =cut | ||||||||||||||||||
| 148 | |||||||||||||||||||
| 149 | # ----------------------------------------------------------------------------- | ||||||||||||||||||
| 150 | |||||||||||||||||||
| 151 | 270 | 1; | |||||||||||||||||
| 152 | |||||||||||||||||||
| 153 | # eof | ||||||||||||||||||