Quiq/Process.pm

filter: Statementstmt 81.9% 50 / 61 Branchbran 61.1% 11 / 18 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 85.7% 12 / 14 Podpod 100% 6 / 6 totaltotal 75.0% 81 / 108 CC 13 SCAR 15.1
CC
13
Cov
74%
mtime
12.0
cwd
6.8
uid
6.0
CRAP 4.5
« Quiq/Xml/LibXml.pm ↑ Summary Quiq/Table.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Process - Informationen über den laufenden Prozess
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::System>
12  
13 =cut
14  
15 # -----------------------------------------------------------------------------
16  
17 package Quiq::Process;
18 270 ▶use base qw/Quiq::System/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 270 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 270 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 270 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 270 our $VERSION = '1.240';
25  
26 270 ▶use Quiq::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 270 ▶use Cwd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
28 270 ▶use Quiq::System;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 # -----------------------------------------------------------------------------
31  
32 =head1 METHODS
33  
34 =head2 Klassenmethoden
35  
36 =head3 cwd() - Aktuelles Verzeichnis (Liefern/Setzen)
37  
38 =head4 Synopsis
39  
40 $dir = $this->cwd;
41 $this->cwd($dir);
42  
43 =head4 Alias
44  
45 cd()
46  
47 =head4 Description
48  
49 Liefere das aktuelle Verzeichnis ("current working directory") des
50 Prozesses. Ist ein Argument angegeben, wechsele in das betreffende
51 Verzeichnis. Tilde-Nt
52  
53 =head4 Examples
54  
55 Liefere aktuelles Verzeichnis:
56  
57 $dir = Quiq::Process->cwd;
58  
59 Wechsele Verzeichnis:
60  
61 Quiq::Process->cwd('/tmp');
62  
63 =cut
64  
65 # -----------------------------------------------------------------------------
66  
67 7 ▶sub cwd {
Subroutine cwd: called CC=6 SCAR=19.2
Pod documented
68 7 my $this = shift;
69 # @_: $dir
70  
71 7 ▶ if (!@_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 5 2 7
72 5 return Cwd::cwd;
73 }
74  
75 2 my $dir = Quiq::Path->expandTilde(shift);
76 2 ▶ if (!defined($dir) || $dir eq '' || $dir eq '.') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $dir or $dir eq "" or $dir eq ".") 0 2 2
Condition33%2/6
not defined $dir or $dir eq "" or $dir eq "."
l!l&&r!l&&!r
002
not defined $dir or $dir eq ""
l!l&&r!l&&!r
002
MC/DC0%0/3
not defined $dir or $dir eq "" or $dir eq "."
not defined $dir $dir eq "" $dir eq "."
Truth table25%1/4
not(A) or B or C
A= not defined $dirB= $dir eq ""C= $dir eq "."
ABCresult
1XX1
01X1
0011
0000
77 0 return;
78 }
79 2 ▶ CORE::chdir $dir or do {
Branch: 1 branch
BranchTrueFalseTotal
chdir $dir or do { $this->throw("PROCESS-00001: Cannot change directory", "Directory", $dir, "CurrentWorkingDirectory", Cwd::cwd()) } 1 1 2
80 1 $this->throw(
81 'PROCESS-00001: Cannot change directory',
82 Directory => $dir,
83 CurrentWorkingDirectory => Cwd::cwd,
84 );
85 };
86  
87 1 return;
88 }
89  
90 {
91 270 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
92 270 *cd = \&cwd;
93 }
94  
95 # -----------------------------------------------------------------------------
96  
97 =head3 euid() - Effektive User-Id (Liefern/Setzen)
98  
99 =head4 Synopsis
100  
101 $uid = $this->euid;
102 $this->euid($uid);
103  
104 =head4 Description
105  
106 Liefere die Effektive User-Id (EUID) des Prozesses. Ist ein Argument
107 angegeben, setze die EUID auf die betreffende User-Id.
108  
109 Um die Effektive User-Id zu ermitteln, kann auch einfach die globale
110 Perl-Variable $> abgefragt werden.
111  
112 =head4 Examples
113  
114 Liefere aktuelle EUID:
115  
116 $uid = Quiq::Process->euid;
117  
118 Setze EUID:
119  
120 Quiq::Process->euid(1000);
121  
122 =cut
123  
124 # -----------------------------------------------------------------------------
125  
126 3 ▶sub euid {
Subroutine euid: called CC=3 SCAR=11.0
Pod documented
127 3 my $this = shift;
128 # @_: $uid
129  
130 3 ▶ if (!@_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 1 2 3
131 1 return $>;
132 }
133  
134 2 my $uid = shift;
135 2 $> = $uid;
136 2 ▶ if ($> != $uid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($> != $uid) 1 1 2
137 1 $this->throw(
138 'PROCESS-00002: Cannot set effective user id (EUID)',
139 UID => $<,
140 EUID => $>,
141 NewEUID => $uid,
142 Error => "$!",
143 );
144 };
145  
146 1 return;
147 }
148  
149 # -----------------------------------------------------------------------------
150  
151 =head3 homeDir() - Home-Verzeichnis des Benutzers
152  
153 =head4 Synopsis
154  
155 $path = $this->homeDir;
156 $path = $this->homeDir($subPath);
157  
158 =head4 Description
159  
160 Liefere das Home-Verzeichnis des Benutzers, der den Prozess
161 ausführt.
162  
163 =cut
164  
165 # -----------------------------------------------------------------------------
166  
167 1 ▶sub homeDir {
Subroutine homeDir: called CC=3 SCAR=12.3
Pod documented
168 1 my $this = shift;
169 # @_: $subPath
170  
171 1 ▶ if (!exists $ENV{'HOME'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $ENV{"HOME"}) 0 1 1
172 0 $this->throw(
173 'PROCESS-00003: Environment variable HOME does not exist',
174 );
175 }
176  
177 1 my $path = $ENV{'HOME'};
178 1 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 1 1
179 0 $path .= "/$_[0]";
180 }
181  
182 1 return $path;
183 }
184  
185 # -----------------------------------------------------------------------------
186  
187 =head3 mtime() - Zeitpunkt der letzten Änderung am Programm
188  
189 =head4 Synopsis
190  
191 $mtime = $this->mtime;
192  
193 =head4 Returns
194  
195 Unix Epoch (Integer)
196  
197 =head4 Description
198  
199 Liefere den Zeitpunkt der letzten Änderung an den Dateien
200 des (Perl-)Prozesses in Unix Epoch.
201  
202 Diese Information ist nützlich, wenn das Programm z.B. Dateien
203 generiert und selbst entscheiden möchte, ob eine Neugenerierung
204 aufgrund einer Änderung am Programm nötig ist.
205  
206 =cut
207  
208 # -----------------------------------------------------------------------------
209  
210 0 ▶sub mtime {
Subroutine mtime: not called CC=3 SCAR=24.8
Pod documented
211 0 my $this = shift;
212  
213 0 my $mtime = (stat($0))[9];
214 0 for (values %INC) {
215 0 ▶ if ((my $t = (stat)[9]) > $mtime) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $t = (stat $_)[9]) > $mtime) 0 0 0
216 0 $mtime = $t;
217 }
218 }
219  
220 0 return $mtime;
221 }
222  
223 # -----------------------------------------------------------------------------
224  
225 =head3 uid() - UID des Prozesses oder eines (beliebigen) Benutzers
226  
227 =head4 Synopsis
228  
229 $uid = $this->uid;
230 $uid = $this->uid($user);
231  
232 =head4 Description
233  
234 Liefere die reale User-Id des Prozesses. Ist Parameter $user
235 angegeben, liefere die User-Id des betreffenden Benutzers.
236  
237 =cut
238  
239 # -----------------------------------------------------------------------------
240  
241 0 ▶sub uid {
Subroutine uid: not called CC=2 SCAR=17.9
Pod documented
242 0 my ($this,$user) = @_;
243 0 ▶ return $user? $this->SUPER::uid($user): $<;
Branch: 1 branch
BranchTrueFalseTotal
$user ? : 0 0 0
244 }
245  
246 # -----------------------------------------------------------------------------
247  
248 =head3 user() - Name des ausführenden Benutzers
249  
250 =head4 Synopsis
251  
252 $user = $this->user;
253  
254 =head4 Description
255  
256 Liefere den Namen des Benutzers, unter dessen Rechten der
257 Prozess ausgeführt wird.
258  
259 =cut
260  
261 # -----------------------------------------------------------------------------
262  
263 1 ▶sub user {
Subroutine user: called CC=1 SCAR=0.0
Pod documented
264 1 my $this = shift;
265 1 return Quiq::System->user($>);
266 }
267  
268 # -----------------------------------------------------------------------------
269  
270 =head1 VERSION
271  
272 1.240
273  
274 =head1 AUTHOR
275  
276 Frank Seitz, L<http://fseitz.de/>
277  
278 =head1 COPYRIGHT
279  
280 Copyright (C) 2026 Frank Seitz
281  
282 =head1 LICENSE
283  
284 This code is free software; you can redistribute it and/or modify
285 it under the same terms as Perl itself.
286  
287 =cut
288  
289 # -----------------------------------------------------------------------------
290  
291 270 1;
292  
293 # eof
« Quiq/Xml/LibXml.pm ↑ Summary Quiq/Table.pm »