Venus::Process - Process Class
Process Class for Perl 5
package main; use Venus::Process; my $parent = Venus::Process->new; my $process = $parent->fork; if ($process) { # do something in child process ... $process->exit; } else { # do something in parent process ... $parent->wait(-1); } # $parent->exit;
This package provides methods for handling and forking processes.
This package inherits behaviors from:
Venus::Kind::Utility
This package integrates behaviors from:
Venus::Role::Accessible
Venus::Role::Buildable
Venus::Role::Explainable
Venus::Role::Valuable
This package provides the following methods:
chdir(Str $path) (Process)
The chdir method changes the working directory the current process is operating within.
Since 0.06
0.06
# given: synopsis; $parent = $parent->chdir; # bless({...}, 'Venus::Process')
# given: synopsis; $parent = $parent->chdir('/tmp'); # bless({...}, 'Venus::Process')
# given: synopsis; $parent = $parent->chdir('/xyz'); # Exception! Venus::Process::Error (isa Venus::Error)
check(Int $pid) (Int, Int)
The check method does a non-blocking "waitpid" in perlfunc operation and returns the wait status. In list context, returns the specified process' exit code (if terminated).
package main; use Venus::Process; my $parent = Venus::Process->new; my ($process, $pid) = $parent->fork; if ($process) { # in forked process ... $process->exit; } my $check = $parent->check($pid); # 0
package main; use Venus::Process; my $parent = Venus::Process->new; my ($process, $pid) = $parent->fork; if ($process) { # in forked process ... $process->exit; } my ($check, $status) = $parent->check('00000'); # (-1, -1)
package main; use Venus::Process; my $parent = Venus::Process->new; my ($process, $pid) = $parent->fork(sub{ $_->exit(1) }); if ($process) { # in forked process ... $process->exit; } my ($check, $status) = $parent->check($pid); # ($pid, 1)
daemon() (Process)
The daemon method detaches the process from controlling terminal and runs it in the background as system daemon. This method internally calls "disengage" and "setsid" and attempts to change the working directory to the root directory.
# given: synopsis; my $daemon = $parent->daemon; # exits parent immediately # in forked process ... # $daemon->exit;
disengage() (Process)
The disengage method limits the interactivity of the process by changing the working directory to the root directory and redirecting its standard file descriptors from and to /dev/null, or the OS' equivalent. These state changes can be undone by calling the "engage" method.
/dev/null
# given: synopsis; $parent = $parent->disengage; # bless({...}, 'Venus::Process')
engage() (Process)
The engage method ensures the interactivity of the process by changing the working directory to the directory used to launch the process, and by redirecting/returning its standard file descriptors from and to their defaults. This method effectively does the opposite of the "disengage" method.
# given: synopsis; $parent = $parent->engage; # bless({...}, 'Venus::Process')
exit(Int $status) (Int)
The exit method exits the program immediately.
# given: synopsis; my $exit = $parent->exit; # 0
# given: synopsis; my $exit = $parent->exit(1); # 1
fork(Str | CodeRef $code, Any @args) (Process, Int)
The fork method calls the system "fork" in perlfunc function and creates a new process running the same program at the same point (or call site). This method returns a new Venus::Process object representing the child process (from within the execution of the child process (or fork)), and returns undef to the parent (or originating) process. In list context, this method returns both the process and PID (or process ID) of the child process. If a callback or argument is provided it will be executed in the child process.
undef
# given: synopsis; $process = $parent->fork; # if ($process) { # # in forked process ... # $process->exit; # } # else { # # in parent process ... # $parent->wait(-1); # } # in child process # bless({...}, 'Venus::Process')
# given: synopsis; my $pid; ($process, $pid) = $parent->fork; # if ($process) { # # in forked process ... # $process->exit; # } # else { # # in parent process ... # $parent->wait($pid); # } # in parent process # (undef, $pid)
# given: synopsis; my $pid; ($process, $pid) = $parent->fork(sub{ $$_{started} = time; }); # if ($process) { # # in forked process ... # $process->exit; # } # else { # # in parent process ... # $parent->wait($pid); # } # in parent process # (undef, $pid)
# given: synopsis; $process = $parent->fork(sub{}); # simulate fork failure # no forking attempted if NOT supported # Exception! Venus::Process:Error (isa Venus::Error)
forks(Str | CodeRef $code, Any @args) (Process, ArrayRef[Int])
The forks method creates multiple forks by calling the "fork" method n times, based on the count specified. As with the "fork" method, this method returns a new Venus::Process object representing the child process (from within the execution of the child process (or fork)), and returns undef to the parent (or originating) process. In list context, this method returns both the process and an arrayref of PID values (or process IDs) for each of the child processes created. If a callback or argument is provided it will be executed in each child process.
n
# given: synopsis; $process = $parent->forks(5); # if ($process) { # # do something in (each) forked process ... # $process->exit; # } # else { # # do something in parent process ... # $parent->wait(-1); # } # bless({...}, 'Venus::Process')
# given: synopsis; my $pids; ($process, $pids) = $parent->forks(5); # if ($process) { # # do something in (each) forked process ... # $process->exit; # } # else { # # do something in parent process ... # $parent->wait($_) for @$pids; # } # in parent process # (undef, $pids)
# given: synopsis; my $pids; ($process, $pids) = $parent->forks(5, sub{ my ($fork, $pid, $iteration) = @_; # $iteration is the fork iteration index $fork->exit; }); # if ($process) { # # do something in (each) forked process ... # $process->exit; # } # else { # # do something in parent process ... # $parent->wait($_) for @$pids; # } # in child process # bless({...}, 'Venus::Process')
kill(Str $signal, Int @pids) (Int)
The kill method calls the system "kill" in perlfunc function which sends a signal to a list of processes and returns truthy or falsy. Note: A truthy result doesn't necessarily mean all processes were successfully signalled.
# given: synopsis; if ($process = $parent->fork) { # in forked process ... $process->exit; } my $kill = $parent->kill('term', int$process); # 1
setsid() (Int)
The setsid method calls the "setsid" in POSIX function and sets the process group identifier of the current process.
# given: synopsis; my $setsid = $parent->setsid; # 1
# given: synopsis; my $setsid = $parent->setsid; # Exception! Venus::Process::Error (isa Venus::Error)
stderr(Str $path) (Process)
The stderr method redirects STDERR to the path provided, typically /dev/null or some equivalent. If called with no arguments STDERR will be restored to its default.
STDERR
# given: synopsis; $parent = $parent->stderr; # bless({...}, 'Venus::Process')
# given: synopsis; $parent = $parent->stderr('/nowhere'); # Exception! Venus::Process:Error (isa Venus::Error)
stdin(Str $path) (Process)
The stdin method redirects STDIN to the path provided, typically /dev/null or some equivalent. If called with no arguments STDIN will be restored to its default.
STDIN
# given: synopsis; $parent = $parent->stdin; # bless({...}, 'Venus::Process')
# given: synopsis; $parent = $parent->stdin('/nowhere'); # Exception! Venus::Process::Error (isa Venus::Error)
stdout(Str $path) (Process)
The stdout method redirects STDOUT to the path provided, typically /dev/null or some equivalent. If called with no arguments STDOUT will be restored to its default.
STDOUT
# given: synopsis; $parent = $parent->stdout; # bless({...}, 'Venus::Process')
# given: synopsis; $parent = $parent->stdout('/nowhere'); # Exception! Venus::Process::Error (isa Venus::Process)
trap(Str $name, Str | CodeRef $expr) (Process)
The trap method registers a process signal trap (or callback) which will be invoked whenever the current process receives that matching signal. The signal traps are globally installed and will overwrite any preexisting behavior. Signal traps are inherited by child processes (or forks) but can be overwritten using this method, or reverted to the default behavior by using the "untrap" method.
# given: synopsis; $parent = $parent->trap(term => sub{ die 'Something failed!'; }); # bless({...}, 'Venus::Process')
untrap(Str $name) (Process)
The untrap method restores the process signal trap specified to its default behavior. If called with no arguments, it restores all signal traps overwriting any user-defined signal traps in the current process.
# given: synopsis; $parent->trap(chld => 'ignore')->trap(term => sub{ die 'Something failed!'; }); $parent = $parent->untrap('term'); # bless({...}, 'Venus::Process')
# given: synopsis; $parent->trap(chld => 'ignore')->trap(term => sub{ die 'Something failed!'; }); $parent = $parent->untrap; # bless({...}, 'Venus::Process')
wait(Int $pid) (Int, Int)
The wait method does a blocking "waitpid" in perlfunc operation and returns the wait status. In list context, returns the specified process' exit code (if terminated).
package main; use Venus::Process; my $parent = Venus::Process->new; my ($process, $pid) = $parent->fork; if ($process) { # in forked process ... $process->exit; } my $wait = $parent->wait($pid); # 0
package main; use Venus::Process; my $parent = Venus::Process->new; my ($process, $pid) = $parent->fork; if ($process) { # in forked process ... $process->exit; } my ($wait, $status) = $parent->wait('00000'); # (-1, -1)
package main; use Venus::Process; my $parent = Venus::Process->new; my ($process, $pid) = $parent->fork(sub{ $_->exit(1) }); if ($process) { # in forked process ... $process->exit; } my ($wait, $status) = $parent->wait($pid); # ($pid, 1)
work(Str | CodeRef $code, Any @args) (Int)
The work method forks the current process, runs the callback provided in the child process, and immediately exits after. This method returns the PID of the child process. It is recommended to install an "alarm" in perlfunc in the child process (i.e. callback) to avoid creating zombie processes in situations where the parent process might exit before the child process is done working.
# given: synopsis; my $pid = $parent->work(sub{ my ($process) = @_; # in forked process ... $process->exit; }); # $pid
This package overloads the following operators:
("")
This package overloads the "" operator.
""
example 1
# given: synopsis; my $result = "$parent"; # $pid
(~~)
This package overloads the ~~ operator.
~~
# given: synopsis; my $result = $parent ~~ /^\d+$/; # 1
To install Venus, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Venus
CPAN shell
perl -MCPAN -e shell install Venus
For more information on module installation, please visit the detailed CPAN module installation guide.