NAME Child - Object oriented simple interface to fork() DESCRIPTION Fork is too low level, and difficult to manage. Often people forget to exit at the end, reap their children, and check exit status. The problem is the low level functions provided to do these things. Throw in pipes for IPC and you just have a pile of things nobody wants to think about. Child is an Object Oriented interface to fork. It provides a clean way to start a child process, and manage it afterwords. It provides methods for running, waiting, killing, checking, and even communicating with a child process. SYNOPSIS BASIC use Child; my $child = Child->new(sub { my $self = shift; .... # exit() is called for you at the end. }); # Build with IPC my $child2 = Child->new(sub { my $self = shift; $self->say("message1"); $self->say("message2"); my $reply = $self->read(1); }, pipe => 1 ); # Read (blocking) my $message1 = $child2->read(1); # Read (non-blocking) my $message2 = $child2->read(); $child2->say("reply"); # Kill the child if it is not done $child->complete || $child->kill(9); $child->wait; #blocking SHORTCUT Child can export the child(&) shortcut function when requested. This function creates and starts the child process. use Child qw/child/; my $child = child { my $self = shift; ... }; You can also request IPC: use Child qw/child/; my $child = child { my $self = shift; ... } pipe => 1; To add IPC to children created with child() by default, import with ':pipe'. How child() behaves regarding IPC is lexical to each importing class. use Child qw/child :pipe/; my $child = child { my $self = shift; $self->say("message1"); }; my $message1 = $child->read(1); CLASS METHODS @children = Child->all_children() Get a list of all the children that have been started. This list is cleared in children when they are started. @pids = Child->all_child_pids() Get a list of all the pids of children that have been started. Child->wait_all() Call wait() on all children. CONSTRUCTOR $class->new( sub { ... } ) $class->new( sub { ... }, pipe => 1 ) Create a new Child object. Does not start the child. OBJECT METHODS $child->start() Start the child process. $bool = $child->is_complete() Check if the child is finished (non-blocking) $child->wait() Wait on the child (blocking) $child->kill($SIG) Send the $SIG signal to the child process. $child->read($BLOCK) Read a message from the child. Takes a single boolean argument; when true the method blocks. $child->write( @MESSAGES ) Send the messages to the child. works like print, you must add "\n". $child->say( @MESSAGES ) Send the messages to the child. works like say, adds the seperator for you (usually "\n"). $child->pid() Returns the child PID (only in parent process). $child->exit_status() Will be undef unless the process has exited, otherwise it will have the exit status. Note: When you call exit($N) the actual unix exit status will be bit shifed with extra information added. exit_status() will shift the value back for you. That means exit_status() will return 2 whun your child calls exit(2) see unix_exit() if you want the actual value wait() assigned to $?. $child->unix_exit() When you call exit($N) the actual unix exit status will be bit shifed with extra information added. See exit_status() if you want the actual value used in exit() in the child. $child->code() Returns the coderef used to construct the Child. $child->parent() Returns the parent processes PID. (Only in child) HISTORY Most of this was part of Parrallel::Runner intended for use in the Fennec project. Fennec is being brocken into multiple parts, this is one such part. FENNEC PROJECT This module is part of the Fennec project. See Fennec for more details. Fennec is a project to develop an extendable and powerful testing framework. Together the tools that make up the Fennec framework provide a potent testing environment. The tools provided by Fennec are also useful on their own. Sometimes a tool created for Fennec is useful outside the greator framework. Such tools are turned into their own projects. This is one such project. Fennec - The core framework The primary Fennec project that ties them all together. AUTHORS Chad Granum exodist7@gmail.com COPYRIGHT Copyright (C) 2010 Chad Granum Child is free software; Standard perl licence. Child is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.