Venus/Process.pm

filter: Statementstmt 89.1% 156 / 175 Branchbran 87.0% 47 / 54 Conditioncond 70.0% 7 / 10 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 72.2% 26 / 36 Podpod 89.4% 17 / 19 totaltotal 84.6% 254 / 300 CC 34 SCAR 12.3
CC
34
Cov
88%
fork
7.5
forks
7.0
_forkable
6.0
CRAP 3.4
« Venus/Array.pm ↑ Summary Venus/Replace.pm »
1 package Venus::Process;
2  
3 2 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 use overload (
9 2 '""' => 'explain',
10 '~~' => 'explain',
11 fallback => 1,
12 2 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use Venus::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 base 'Venus::Kind::Utility';
17  
18 2 with 'Venus::Role::Accessible';
19 2 with 'Venus::Role::Explainable';
20  
21 2 require Config;
22 2 require Cwd;
23 2 require File::Spec;
24 2 require POSIX;
25  
26 2 state $GETCWD = Cwd->getcwd;
27 2 state $MAPSIG = {%SIG};
28  
29 # HOOKS
30  
31 sub _chdir {
32 0 ▶ CORE::chdir(shift);
Subroutine _chdir: not called CC=1 SCAR=6.9
33 }
34  
35 sub _exit {
36 0 ▶ POSIX::_exit(shift);
Subroutine _exit: not called CC=1 SCAR=6.9
37 }
38  
39 sub _exitcode {
40 0 ▶ $?;
Subroutine _exitcode: not called CC=1 SCAR=6.9
41 }
42  
43 sub _fork {
44 0 ▶ CORE::fork();
Subroutine _fork: not called CC=1 SCAR=6.9
45 }
46  
47 sub _forkable {
48 0 ▶ $Config::Config{d_pseudofork} ? 0 : 1;
Subroutine _forkable: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$Config::Config{'d_pseudofork'} ? : 0 0 0
49 }
50  
51 sub _kill {
52 0 ▶ CORE::kill(@_);
Subroutine _kill: not called CC=1 SCAR=6.9
53 }
54  
55 sub _open {
56 0 ▶ CORE::open(shift, shift, shift);
Subroutine _open: not called CC=1 SCAR=6.9
57 }
58  
59 sub _pid {
60 1 ▶ $$;
Subroutine _pid: called CC=1 SCAR=0.0
61 }
62  
63 sub _setsid {
64 0 ▶ POSIX::setsid();
Subroutine _setsid: not called CC=1 SCAR=6.9
65 }
66  
67 sub _waitpid {
68 0 ▶ CORE::waitpid(shift, shift);
Subroutine _waitpid: not called CC=1 SCAR=6.9
69 }
70  
71 # METHODS
72  
73 sub chdir {
74 6 ▶ my ($self, $path) = @_;
Subroutine chdir: called CC=3 SCAR=11.0
Pod documented
75  
76 6 ▶ $path ||= $GETCWD;
Condition66%2/3
$path ||= $GETCWD
l!l&&r!l&&!r
420
MC/DC0%0/2
$path ||= $GETCWD
$path $GETCWD
Truth table66%2/3
A ||= B
A= $pathB= $GETCWD
ABresult
1X1
011
000
77  
78 6 ▶ _chdir($path) or do {
Branch: 1 branch
BranchTrueFalseTotal
_chdir($path) or do { my $throw; my $error = "Can't chdir ${path}: $!"; $throw = $self->throw; $throw->message($error); $throw->stash("path", $path); $throw->stash("pid", _pid()); $throw->error } 1 5 6
79 1 my $throw;
80 1 my $error = "Can't chdir $path: $!";
81 1 $throw = $self->throw;
82 1 $throw->message($error);
83 1 $throw->stash(path => $path);
84 1 $throw->stash(pid => _pid());
85 1 $throw->error;
86 };
87  
88 5 return $self;
89 }
90  
91 sub check {
92 3 ▶ my ($self, $pid) = @_;
Subroutine check: called CC=2 SCAR=6.9
Pod documented
93  
94 3 my $result = _waitpid($pid, POSIX::WNOHANG());
95  
96 3 ▶ return wantarray ? ($result, _exitcode) : $result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 1 3
97 }
98  
99 sub daemon {
100 1 ▶ my ($self) = @_;
Subroutine daemon: called CC=2 SCAR=7.6
Pod documented
101  
102 1 ▶ if (my $process = $self->fork) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $process = $self->fork) { } 1 0 1
103 1 return $process->disengage->do('setsid');
104 }
105 else {
106 0 return $self->exit;
107 }
108 }
109  
110 sub default {
111 53 ▶ return _pid();
Subroutine default: called CC=1 SCAR=0.0
Pod undocumented
112 }
113  
114 sub disengage {
115 2 ▶ my ($self) = @_;
Subroutine disengage: called CC=2 SCAR=6.9
Pod documented
116  
117 2 $self->chdir(File::Spec->rootdir);
118  
119 2 $self->$_(File::Spec->devnull) for qw(stdin stdout stderr);
120  
121 2 return $self;
122 }
123  
124 sub engage {
125 1 ▶ my ($self) = @_;
Subroutine engage: called CC=2 SCAR=6.9
Pod documented
126  
127 1 $self->chdir;
128  
129 1 $self->$_ for qw(stdin stdout stderr);
130  
131 1 return $self;
132 }
133  
134 sub exit {
135 14 ▶ my ($self, $code) = @_;
Subroutine exit: called CC=2 SCAR=6.9
Pod documented
136  
137 14 ▶ return _exit($code // 0);
Condition100%2/2
$code // 0
l!l
113
MC/DC100%1/1
$code // 0
$code
Truth table100%2/2
A
A= $code
Aresult
11
00
138 }
139  
140 sub explain {
141 62 ▶ my ($self) = @_;
Subroutine explain: called CC=1 SCAR=0.0
Pod undocumented
142  
143 62 return $self->get;
144 }
145  
146 sub fork {
147 49 ▶ my ($self, $code, @args) = @_;
Subroutine fork: called CC=7 SCAR=20.1
Pod documented
148  
149 49 ▶ if (not(_forkable())) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_forkable()) 1 48 49
150 1 my $throw;
151 1 my $error = "Can't fork process @{[_pid()]}: Fork emulation not supported";
152 1 $throw = $self->throw;
153 1 $throw->message($error);
154 1 $throw->stash(process => _pid());
155 1 $throw->error;
156 }
157 48 ▶ if (defined(my $pid = _fork())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $pid = _fork())) { } 48 0 48
158 48 my $process;
159  
160 48 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) 32 16 48
161 32 ▶ return wantarray ? (undef, $pid) : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 11 21 32
162 }
163  
164 16 $process = $self->class->new;
165  
166 16 ▶ if ($code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code) 3 13 16
167 3 local $_ = $process;
168 3 $process->$code(@args);
169 }
170  
171 16 ▶ return wantarray ? ($process, _pid()) : $process;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 11 16
172 }
173 else {
174 0 my $throw;
175 0 my $error = "Can't fork process @{[_pid()]}: $!";
176 0 $throw = $self->throw;
177 0 $throw->message($error);
178 0 $throw->stash(process => _pid());
179 0 $throw->error;
180 }
181 }
182  
183 sub forks {
184 3 ▶ my ($self, $count, $code, @args) = @_;
Subroutine forks: called CC=7 SCAR=19.5
Pod documented
185  
186 3 my $pid;
187 my @pids;
188 3 my $process;
189  
190 3 ▶ for (my $i = 1; $i <= ($count || 0); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= ($count || 0)) 7 1 8
Condition50%1/2
$count || 0
l!l
80
MC/DC0%0/1
$count || 0
$count
Truth table50%1/2
A
A= $count
Aresult
11
00
191 7 ($process, $pid) = $self->fork($code, @args, $i);
192 7 ▶ if (!$process) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($process) 5 2 7
193 5 push @pids, $pid;
194 }
195 7 ▶ if ($process) {
Branch: 1 branch
BranchTrueFalseTotal
if ($process) 2 5 7
196 2 last;
197 }
198 }
199  
200 3 ▶ return wantarray ? ($process ? ($process, []) : ($process, [@pids]) ) : $process;
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 2 1 3
$process ? : 1 1 2
201 }
202  
203 sub kill {
204 1 ▶ my ($self, $name, @pids) = @_;
Subroutine kill: called CC=1 SCAR=0.0
Pod documented
205  
206 1 return _kill(uc($name), @pids);
207 }
208  
209 sub setsid {
210 3 ▶ my ($self) = @_;
Subroutine setsid: called CC=2 SCAR=6.9
Pod documented
211  
212 3 ▶ return _setsid != -1 || do {
Condition66%2/3
_setsid() != -1 || do { my $throw; my $error = "Can't start a new session: $!"; $throw = $self->throw; $throw->message($error); $throw->stash('pid', _pid()); $throw->error }
l!l&&r!l&&!r
201
MC/DC0%0/2
_setsid() != -1 || do { my $throw; my $error = "Can't start a new session: $!"; $throw = $self->throw; $throw->message($error); $throw->stash('pid', _pid()); $throw->error }
_setsid() != -1 do { my $throw; my $error = "Can't start a new session: $!"; $throw = $self->throw; $throw->message($error); $throw->stash('pid', _pid()); $throw->error }
Truth table33%1/3
A || B
A= _setsid() != -1B= do { my $throw; my $error = "Can't start a new session: $!"; $throw = $self->throw; $throw->message($error); $throw->stash('pid', _pid()); $throw->error }
ABresult
1X1
011
000
213 1 my $throw;
214 1 my $error = "Can't start a new session: $!";
215 1 $throw = $self->throw;
216 1 $throw->message($error);
217 1 $throw->stash(pid => _pid());
218 1 $throw->error;
219 };
220 }
221  
222 sub stderr {
223 5 ▶ my ($self, $path) = @_;
Subroutine stderr: called CC=4 SCAR=13.9
Pod documented
224  
225 5 state $STDERR;
226  
227 5 ▶ if (!$STDERR) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($STDERR) 5 0 5
228 5 _open($STDERR, '>&', \*STDERR);
229 }
230 5 ▶ if (!$path) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $path) { } 2 3 5
231 2 _open(\*STDERR, '>&', $STDERR);
232 }
233 else {
234 3 ▶ _open(\*STDERR, '>&', IO::File->new($path, 'w')) or do {
Branch: 1 branch
BranchTrueFalseTotal
_open(\*STDERR, ">&", "IO::File"->new($path, "w")) or do { my $throw; my $error = "Can't redirect STDERR to ${path}: $!"; $throw = $self->throw; $throw->message($error); $throw->stash("path", $path); $throw->stash("pid", _pid()); $throw->error } 1 2 3
235 1 my $throw;
236 1 my $error = "Can't redirect STDERR to $path: $!";
237 1 $throw = $self->throw;
238 1 $throw->message($error);
239 1 $throw->stash(path => $path);
240 1 $throw->stash(pid => _pid());
241 1 $throw->error;
242 };
243 }
244  
245 4 return $self;
246 }
247  
248 sub stdin {
249 5 ▶ my ($self, $path) = @_;
Subroutine stdin: called CC=4 SCAR=13.9
Pod documented
250  
251 5 state $STDIN;
252  
253 5 ▶ if (!$STDIN) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($STDIN) 5 0 5
254 5 _open($STDIN, '<&', \*STDIN);
255 }
256 5 ▶ if (!$path) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $path) { } 2 3 5
257 2 _open(\*STDIN, '<&', $STDIN);
258 }
259 else {
260 3 ▶ _open(\*STDIN, '<&', IO::File->new($path, 'r')) or do {
Branch: 1 branch
BranchTrueFalseTotal
_open(\*STDIN, "<&", "IO::File"->new($path, "r")) or do { my $throw; my $error = "Can't redirect STDIN to ${path}: $!"; $throw = $self->throw; $throw->message($error); $throw->stash("path", $path); $throw->stash("pid", _pid()); $throw->error } 1 2 3
261 1 my $throw;
262 1 my $error = "Can't redirect STDIN to $path: $!";
263 1 $throw = $self->throw;
264 1 $throw->message($error);
265 1 $throw->stash(path => $path);
266 1 $throw->stash(pid => _pid());
267 1 $throw->error;
268 };
269 }
270  
271 4 return $self;
272 }
273  
274 sub stdout {
275 5 ▶ my ($self, $path) = @_;
Subroutine stdout: called CC=4 SCAR=13.9
Pod documented
276  
277 5 state $STDOUT;
278  
279 5 ▶ if (!$STDOUT) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($STDOUT) 5 0 5
280 5 _open($STDOUT, '>&', \*STDOUT);
281 }
282 5 ▶ if (!$path) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $path) { } 2 3 5
283 2 _open(\*STDOUT, '>&', $STDOUT);
284 }
285 else {
286 3 ▶ _open(\*STDOUT, '>&', IO::File->new($path, 'w')) or do {
Branch: 1 branch
BranchTrueFalseTotal
_open(\*STDOUT, ">&", "IO::File"->new($path, "w")) or do { my $throw; my $error = "Can't redirect STDOUT to ${path}: $!"; $throw = $self->throw; $throw->message($error); $throw->stash("path", $path); $throw->stash("pid", _pid()); $throw->error } 1 2 3
287 1 my $throw;
288 1 my $error = "Can't redirect STDOUT to $path: $!";
289 1 $throw = $self->throw;
290 1 $throw->message($error);
291 1 $throw->stash(path => $path);
292 1 $throw->stash(pid => _pid());
293 1 $throw->error;
294 };
295 }
296  
297 4 return $self;
298 }
299  
300 sub trap {
301 5 ▶ my ($self, $name, $expr) = @_;
Subroutine trap: called CC=2 SCAR=7.4
Pod documented
302  
303 $SIG{uc($name)} = !ref($expr) ? uc($expr) : sub {
304 0 ▶ local($!, $?);
Subroutine __ANON__: not called CC=1 SCAR=6.9
305 0 return $expr->(@_);
306 5 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
!ref($expr) ? : 2 3 5
307  
308 5 return $self;
309 }
310  
311 sub wait {
312 24 ▶ my ($self, $pid) = @_;
Subroutine wait: called CC=2 SCAR=6.9
Pod documented
313  
314 24 my $result = _waitpid($pid, 0);
315  
316 24 ▶ return wantarray ? ($result, _exitcode) : $result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 21 24
317 }
318  
319 sub work {
320 1 ▶ my ($self, $code, @args) = @_;
Subroutine work: called CC=1 SCAR=0.0
Pod documented
321  
322 my @returned = $self->fork(sub{
323 1 ▶ my ($process) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
324 1 local $_ = $process;
325 1 $process->$code(@args);
326 1 $process->exit;
327 1 });
328  
329 1 return $returned[-1];
330 }
331  
332 sub untrap {
333 2 ▶ my ($self, $name) = @_;
Subroutine untrap: called CC=2 SCAR=6.9
Pod documented
334  
335 2 ▶ if ($name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) { } 1 1 2
336 1 $SIG{uc($name)} = $$MAPSIG{uc($name)};
337 }
338 else {
339 1 %SIG = %$MAPSIG;
340 }
341  
342 2 return $self;
343 }
344  
345 2 1;
346  
347  
348  
349 =head1 NAME
350  
351 Venus::Process - Process Class
352  
353 =cut
354  
355 =head1 ABSTRACT
356  
357 Process Class for Perl 5
358  
359 =cut
360  
361 =head1 SYNOPSIS
362  
363 package main;
364  
365 use Venus::Process;
366  
367 my $parent = Venus::Process->new;
368  
369 my $process = $parent->fork;
370  
371 if ($process) {
372 # do something in child process ...
373 $process->exit;
374 }
375 else {
376 # do something in parent process ...
377 $parent->wait(-1);
378 }
379  
380 # $parent->exit;
381  
382 =cut
383  
384 =head1 DESCRIPTION
385  
386 This package provides methods for handling and forking processes.
387  
388 =cut
389  
390 =head1 INHERITS
391  
392 This package inherits behaviors from:
393  
394 L<Venus::Kind::Utility>
395  
396 =cut
397  
398 =head1 INTEGRATES
399  
400 This package integrates behaviors from:
401  
402 L<Venus::Role::Accessible>
403  
404 L<Venus::Role::Explainable>
405  
406 =cut
407  
408 =head1 METHODS
409  
410 This package provides the following methods:
411  
412 =cut
413  
414 =head2 chdir
415  
416 chdir(Str $path) (Process)
417  
418 The chdir method changes the working directory the current process is operating
419 within.
420  
421 I<Since C<0.06>>
422  
423 =over 4
424  
425 =item chdir example 1
426  
427 # given: synopsis;
428  
429 $parent = $parent->chdir;
430  
431 # bless({...}, 'Venus::Process')
432  
433 =back
434  
435 =over 4
436  
437 =item chdir example 2
438  
439 # given: synopsis;
440  
441 $parent = $parent->chdir('/tmp');
442  
443 # bless({...}, 'Venus::Process')
444  
445 =back
446  
447 =over 4
448  
449 =item chdir example 3
450  
451 # given: synopsis;
452  
453 $parent = $parent->chdir('/xyz');
454  
455 # Exception! Venus::Process::Error (isa Venus::Error)
456  
457 =back
458  
459 =cut
460  
461 =head2 check
462  
463 check(Int $pid) (Int, Int)
464  
465 The check method does a non-blocking L<perlfunc/waitpid> operation and returns
466 the wait status. In list context, returns the specified process' exit code (if
467 terminated).
468  
469 I<Since C<0.06>>
470  
471 =over 4
472  
473 =item check example 1
474  
475 package main;
476  
477 use Venus::Process;
478  
479 my $parent = Venus::Process->new;
480  
481 my ($process, $pid) = $parent->fork;
482  
483 if ($process) {
484 # in forked process ...
485 $process->exit;
486 }
487  
488 my $check = $parent->check($pid);
489  
490 # 0
491  
492 =back
493  
494 =over 4
495  
496 =item check example 2
497  
498 package main;
499  
500 use Venus::Process;
501  
502 my $parent = Venus::Process->new;
503  
504 my ($process, $pid) = $parent->fork;
505  
506 if ($process) {
507 # in forked process ...
508 $process->exit;
509 }
510  
511 my ($check, $status) = $parent->check('00000');
512  
513 # (-1, -1)
514  
515 =back
516  
517 =over 4
518  
519 =item check example 3
520  
521 package main;
522  
523 use Venus::Process;
524  
525 my $parent = Venus::Process->new;
526  
527 my ($process, $pid) = $parent->fork(sub{ $_->exit(1) });
528  
529 if ($process) {
530 # in forked process ...
531 $process->exit;
532 }
533  
534 my ($check, $status) = $parent->check($pid);
535  
536 # ($pid, 1)
537  
538 =back
539  
540 =cut
541  
542 =head2 daemon
543  
544 daemon() (Process)
545  
546 The daemon method detaches the process from controlling terminal and runs it in
547 the background as system daemon. This method internally calls L</disengage> and
548 L</setsid> and attempts to change the working directory to the root directory.
549  
550 I<Since C<0.06>>
551  
552 =over 4
553  
554 =item daemon example 1
555  
556 # given: synopsis;
557  
558 my $daemon = $parent->daemon; # exits parent immediately
559  
560 # in forked process ...
561  
562 # $daemon->exit;
563  
564 =back
565  
566 =cut
567  
568 =head2 disengage
569  
570 disengage() (Process)
571  
572 The disengage method limits the interactivity of the process by changing the
573 working directory to the root directory and redirecting its standard file
574 descriptors from and to C</dev/null>, or the OS' equivalent. These state
575 changes can be undone by calling the L</engage> method.
576  
577 I<Since C<0.06>>
578  
579 =over 4
580  
581 =item disengage example 1
582  
583 # given: synopsis;
584  
585 $parent = $parent->disengage;
586  
587 # bless({...}, 'Venus::Process')
588  
589 =back
590  
591 =cut
592  
593 =head2 engage
594  
595 engage() (Process)
596  
597 The engage method ensures the interactivity of the process by changing the
598 working directory to the directory used to launch the process, and by
599 redirecting/returning its standard file descriptors from and to their defaults.
600 This method effectively does the opposite of the L</disengage> method.
601  
602 I<Since C<0.06>>
603  
604 =over 4
605  
606 =item engage example 1
607  
608 # given: synopsis;
609  
610 $parent = $parent->engage;
611  
612 # bless({...}, 'Venus::Process')
613  
614 =back
615  
616 =cut
617  
618 =head2 exit
619  
620 exit(Int $status) (Int)
621  
622 The exit method exits the program immediately.
623  
624 I<Since C<0.06>>
625  
626 =over 4
627  
628 =item exit example 1
629  
630 # given: synopsis;
631  
632 my $exit = $parent->exit;
633  
634 # 0
635  
636 =back
637  
638 =over 4
639  
640 =item exit example 2
641  
642 # given: synopsis;
643  
644 my $exit = $parent->exit(1);
645  
646 # 1
647  
648 =back
649  
650 =cut
651  
652 =head2 fork
653  
654 fork(Str | CodeRef $code, Any @args) (Process, Int)
655  
656 The fork method calls the system L<perlfunc/fork> function and creates a new
657 process running the same program at the same point (or call site). This method
658 returns a new L<Venus::Process> object representing the child process (from
659 within the execution of the child process (or fork)), and returns C<undef> to
660 the parent (or originating) process. In list context, this method returns both
661 the process and I<PID> (or process ID) of the child process. If a callback or
662 argument is provided it will be executed in the child process.
663  
664 I<Since C<0.06>>
665  
666 =over 4
667  
668 =item fork example 1
669  
670 # given: synopsis;
671  
672 $process = $parent->fork;
673  
674 # if ($process) {
675 # # in forked process ...
676 # $process->exit;
677 # }
678 # else {
679 # # in parent process ...
680 # $parent->wait(-1);
681 # }
682  
683 # in child process
684  
685 # bless({...}, 'Venus::Process')
686  
687 =back
688  
689 =over 4
690  
691 =item fork example 2
692  
693 # given: synopsis;
694  
695 my $pid;
696  
697 ($process, $pid) = $parent->fork;
698  
699 # if ($process) {
700 # # in forked process ...
701 # $process->exit;
702 # }
703 # else {
704 # # in parent process ...
705 # $parent->wait($pid);
706 # }
707  
708 # in parent process
709  
710 # (undef, $pid)
711  
712 =back
713  
714 =over 4
715  
716 =item fork example 3
717  
718 # given: synopsis;
719  
720 my $pid;
721  
722 ($process, $pid) = $parent->fork(sub{
723 $$_{started} = time;
724 });
725  
726 # if ($process) {
727 # # in forked process ...
728 # $process->exit;
729 # }
730 # else {
731 # # in parent process ...
732 # $parent->wait($pid);
733 # }
734  
735 # in parent process
736  
737 # (undef, $pid)
738  
739 =back
740  
741 =over 4
742  
743 =item fork example 4
744  
745 # given: synopsis;
746  
747 $process = $parent->fork(sub{});
748  
749 # simulate fork failure
750  
751 # no forking attempted if NOT supported
752  
753 # Exception! Venus::Process:Error (isa Venus::Error)
754  
755 =back
756  
757 =cut
758  
759 =head2 forks
760  
761 forks(Str | CodeRef $code, Any @args) (Process, ArrayRef[Int])
762  
763 The forks method creates multiple forks by calling the L</fork> method C<n>
764 times, based on the count specified. As with the L</fork> method, this method
765 returns a new L<Venus::Process> object representing the child process (from
766 within the execution of the child process (or fork)), and returns C<undef> to
767 the parent (or originating) process. In list context, this method returns both
768 the process and an arrayref of I<PID> values (or process IDs) for each of the
769 child processes created. If a callback or argument is provided it will be
770 executed in each child process.
771  
772 I<Since C<0.06>>
773  
774 =over 4
775  
776 =item forks example 1
777  
778 # given: synopsis;
779  
780 $process = $parent->forks(5);
781  
782 # if ($process) {
783 # # do something in (each) forked process ...
784 # $process->exit;
785 # }
786 # else {
787 # # do something in parent process ...
788 # $parent->wait(-1);
789 # }
790  
791 # bless({...}, 'Venus::Process')
792  
793 =back
794  
795 =over 4
796  
797 =item forks example 2
798  
799 # given: synopsis;
800  
801 my $pids;
802  
803 ($process, $pids) = $parent->forks(5);
804  
805 # if ($process) {
806 # # do something in (each) forked process ...
807 # $process->exit;
808 # }
809 # else {
810 # # do something in parent process ...
811 # $parent->wait($_) for @$pids;
812 # }
813  
814 # in parent process
815  
816 # (undef, $pids)
817  
818 =back
819  
820 =over 4
821  
822 =item forks example 3
823  
824 # given: synopsis;
825  
826 my $pids;
827  
828 ($process, $pids) = $parent->forks(5, sub{
829 my ($fork, $pid, $iteration) = @_;
830 # $iteration is the fork iteration index
831 $fork->exit;
832 });
833  
834 # if ($process) {
835 # # do something in (each) forked process ...
836 # $process->exit;
837 # }
838 # else {
839 # # do something in parent process ...
840 # $parent->wait($_) for @$pids;
841 # }
842  
843 # in child process
844  
845 # bless({...}, 'Venus::Process')
846  
847 =back
848  
849 =cut
850  
851 =head2 kill
852  
853 kill(Str $signal, Int @pids) (Int)
854  
855 The kill method calls the system L<perlfunc/kill> function which sends a signal
856 to a list of processes and returns truthy or falsy. B<Note:> A truthy result
857 doesn't necessarily mean all processes were successfully signalled.
858  
859 I<Since C<0.06>>
860  
861 =over 4
862  
863 =item kill example 1
864  
865 # given: synopsis;
866  
867 if ($process = $parent->fork) {
868 # in forked process ...
869 $process->exit;
870 }
871  
872 my $kill = $parent->kill('term', int$process);
873  
874 # 1
875  
876 =back
877  
878 =cut
879  
880 =head2 setsid
881  
882 setsid() (Int)
883  
884 The setsid method calls the L<POSIX/setsid> function and sets the process group
885 identifier of the current process.
886  
887 I<Since C<0.06>>
888  
889 =over 4
890  
891 =item setsid example 1
892  
893 # given: synopsis;
894  
895 my $setsid = $parent->setsid;
896  
897 # 1
898  
899 =back
900  
901 =over 4
902  
903 =item setsid example 2
904  
905 # given: synopsis;
906  
907 my $setsid = $parent->setsid;
908  
909 # Exception! Venus::Process::Error (isa Venus::Error)
910  
911 =back
912  
913 =cut
914  
915 =head2 stderr
916  
917 stderr(Str $path) (Process)
918  
919 The stderr method redirects C<STDERR> to the path provided, typically
920 C</dev/null> or some equivalent. If called with no arguments C<STDERR> will be
921 restored to its default.
922  
923 I<Since C<0.06>>
924  
925 =over 4
926  
927 =item stderr example 1
928  
929 # given: synopsis;
930  
931 $parent = $parent->stderr;
932  
933 # bless({...}, 'Venus::Process')
934  
935 =back
936  
937 =over 4
938  
939 =item stderr example 2
940  
941 # given: synopsis;
942  
943 $parent = $parent->stderr('/nowhere');
944  
945 # Exception! Venus::Process:Error (isa Venus::Error)
946  
947 =back
948  
949 =cut
950  
951 =head2 stdin
952  
953 stdin(Str $path) (Process)
954  
955 The stdin method redirects C<STDIN> to the path provided, typically
956 C</dev/null> or some equivalent. If called with no arguments C<STDIN> will be
957 restored to its default.
958  
959 I<Since C<0.06>>
960  
961 =over 4
962  
963 =item stdin example 1
964  
965 # given: synopsis;
966  
967 $parent = $parent->stdin;
968  
969 # bless({...}, 'Venus::Process')
970  
971 =back
972  
973 =over 4
974  
975 =item stdin example 2
976  
977 # given: synopsis;
978  
979 $parent = $parent->stdin('/nowhere');
980  
981 # Exception! Venus::Process::Error (isa Venus::Error)
982  
983 =back
984  
985 =cut
986  
987 =head2 stdout
988  
989 stdout(Str $path) (Process)
990  
991 The stdout method redirects C<STDOUT> to the path provided, typically
992 C</dev/null> or some equivalent. If called with no arguments C<STDOUT> will be
993 restored to its default.
994  
995 I<Since C<0.06>>
996  
997 =over 4
998  
999 =item stdout example 1
1000  
1001 # given: synopsis;
1002  
1003 $parent = $parent->stdout;
1004  
1005 # bless({...}, 'Venus::Process')
1006  
1007 =back
1008  
1009 =over 4
1010  
1011 =item stdout example 2
1012  
1013 # given: synopsis;
1014  
1015 $parent = $parent->stdout('/nowhere');
1016  
1017 # Exception! Venus::Process::Error (isa Venus::Process)
1018  
1019 =back
1020  
1021 =cut
1022  
1023 =head2 trap
1024  
1025 trap(Str $name, Str | CodeRef $expr) (Process)
1026  
1027 The trap method registers a process signal trap (or callback) which will be
1028 invoked whenever the current process receives that matching signal. The signal
1029 traps are globally installed and will overwrite any preexisting behavior.
1030 Signal traps are inherited by child processes (or forks) but can be overwritten
1031 using this method, or reverted to the default behavior by using the L</untrap>
1032 method.
1033  
1034 I<Since C<0.06>>
1035  
1036 =over 4
1037  
1038 =item trap example 1
1039  
1040 # given: synopsis;
1041  
1042 $parent = $parent->trap(term => sub{
1043 die 'Something failed!';
1044 });
1045  
1046 # bless({...}, 'Venus::Process')
1047  
1048 =back
1049  
1050 =cut
1051  
1052 =head2 untrap
1053  
1054 untrap(Str $name) (Process)
1055  
1056 The untrap method restores the process signal trap specified to its default
1057 behavior. If called with no arguments, it restores all signal traps overwriting
1058 any user-defined signal traps in the current process.
1059  
1060 I<Since C<0.06>>
1061  
1062 =over 4
1063  
1064 =item untrap example 1
1065  
1066 # given: synopsis;
1067  
1068 $parent->trap(chld => 'ignore')->trap(term => sub{
1069 die 'Something failed!';
1070 });
1071  
1072 $parent = $parent->untrap('term');
1073  
1074 # bless({...}, 'Venus::Process')
1075  
1076 =back
1077  
1078 =over 4
1079  
1080 =item untrap example 2
1081  
1082 # given: synopsis;
1083  
1084 $parent->trap(chld => 'ignore')->trap(term => sub{
1085 die 'Something failed!';
1086 });
1087  
1088 $parent = $parent->untrap;
1089  
1090 # bless({...}, 'Venus::Process')
1091  
1092 =back
1093  
1094 =cut
1095  
1096 =head2 wait
1097  
1098 wait(Int $pid) (Int, Int)
1099  
1100 The wait method does a blocking L<perlfunc/waitpid> operation and returns the
1101 wait status. In list context, returns the specified process' exit code (if
1102 terminated).
1103  
1104 I<Since C<0.06>>
1105  
1106 =over 4
1107  
1108 =item wait example 1
1109  
1110 package main;
1111  
1112 use Venus::Process;
1113  
1114 my $parent = Venus::Process->new;
1115  
1116 my ($process, $pid) = $parent->fork;
1117  
1118 if ($process) {
1119 # in forked process ...
1120 $process->exit;
1121 }
1122  
1123 my $wait = $parent->wait($pid);
1124  
1125 # 0
1126  
1127 =back
1128  
1129 =over 4
1130  
1131 =item wait example 2
1132  
1133 package main;
1134  
1135 use Venus::Process;
1136  
1137 my $parent = Venus::Process->new;
1138  
1139 my ($process, $pid) = $parent->fork;
1140  
1141 if ($process) {
1142 # in forked process ...
1143 $process->exit;
1144 }
1145  
1146 my ($wait, $status) = $parent->wait('00000');
1147  
1148 # (-1, -1)
1149  
1150 =back
1151  
1152 =over 4
1153  
1154 =item wait example 3
1155  
1156 package main;
1157  
1158 use Venus::Process;
1159  
1160 my $parent = Venus::Process->new;
1161  
1162 my ($process, $pid) = $parent->fork(sub{ $_->exit(1) });
1163  
1164 if ($process) {
1165 # in forked process ...
1166 $process->exit;
1167 }
1168  
1169 my ($wait, $status) = $parent->wait($pid);
1170  
1171 # ($pid, 1)
1172  
1173 =back
1174  
1175 =cut
1176  
1177 =head2 work
1178  
1179 work(Str | CodeRef $code, Any @args) (Int)
1180  
1181 The work method forks the current process, runs the callback provided in the
1182 child process, and immediately exits after. This method returns the I<PID> of
1183 the child process. It is recommended to install an L<perlfunc/alarm> in the
1184 child process (i.e. callback) to avoid creating zombie processes in situations
1185 where the parent process might exit before the child process is done working.
1186  
1187 I<Since C<0.06>>
1188  
1189 =over 4
1190  
1191 =item work example 1
1192  
1193 # given: synopsis;
1194  
1195 my $pid = $parent->work(sub{
1196 my ($process) = @_;
1197 # in forked process ...
1198 $process->exit;
1199 });
1200  
1201 # $pid
1202  
1203 =back
1204  
1205 =cut
1206  
1207 =head1 OPERATORS
1208  
1209 This package overloads the following operators:
1210  
1211 =cut
1212  
1213 =over 4
1214  
1215 =item operation: C<("")>
1216  
1217 This package overloads the C<""> operator.
1218  
1219 B<example 1>
1220  
1221 # given: synopsis;
1222  
1223 my $result = "$parent";
1224  
1225 # $pid
1226  
1227 =back
1228  
1229 =over 4
1230  
1231 =item operation: C<(~~)>
1232  
1233 This package overloads the C<~~> operator.
1234  
1235 B<example 1>
1236  
1237 # given: synopsis;
1238  
1239 my $result = $parent ~~ /^\d+$/;
1240  
1241 # 1
1242  
1243 =back
« Venus/Array.pm ↑ Summary Venus/Replace.pm »