/usr/share/perl5/Child.pm is in libchild-perl 0.009-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | package Child;
use 5.006;
use strict;
use warnings;
use Carp;
use Child::Util;
use Child::Link::Proc;
use Child::Link::Parent;
use Exporter 'import';
our $VERSION = "0.009";
our @PROCS;
our @EXPORT_OK = qw/child/;
add_accessors qw/code/;
sub child(&;@) {
my ( $code, @params ) = @_;
my $caller = caller;
return __PACKAGE__->new( $code, @params )->start;
}
sub all_procs { @PROCS }
sub all_proc_pids {
my $class = shift;
map { $_->pid } $class->all_procs;
}
sub wait_all {
my $class = shift;
$_->wait() for $class->all_procs;
}
sub new {
my ( $class, $code, $plugin, @data ) = @_;
return bless( { _code => $code }, $class )
unless $plugin;
my $build = __PACKAGE__;
$build .= '::IPC::' . ucfirst $plugin;
eval "require $build; 1"
|| croak( "Could not load plugin '$plugin': $@" );
return $build->new( $code, @data );
}
sub shared_data {}
sub child_class { 'Child::Link::Proc' }
sub parent_class { 'Child::Link::Parent' }
sub start {
my $self = shift;
my $ppid = $$;
my @data = $self->shared_data;
if ( my $pid = fork() ) {
my $proc = $self->child_class->new( $pid, @data );
push @PROCS => $proc;
return $proc;
}
# In the child
@PROCS = ();
my $parent = $self->parent_class->new( $ppid, @data );
my $code = $self->code;
$code->( $parent );
exit;
}
1;
__END__
=head1 NAME
Child - Object oriented simple interface to fork()
=head1 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.
B<NOTE>: kill() is unpredictable on windows, strawberry perl sends the kill
signal to the parent as well as the child.
=head1 SYNOPSIS
=head2 BASIC
use Child;
my $child = Child->new(sub {
my ( $parent ) = @_;
....
# exit() is called for you at the end.
});
my $proc = $child->start
# Kill the child if it is not done
$proc->complete || $proc->kill(9);
$proc->wait; #blocking
=head2 IPC
# Build with IPC
my $child2 = Child->new(sub {
my $self = shift;
$self->say("message1");
$self->say("message2");
my $reply = $self->read(1);
}, pipe => 1 );
my $proc2 = $child2->start;
# Read (blocking)
my $message1 = $proc2->read();
my $message2 = $proc2->read();
$proc2->say("reply");
=head2 SHORTCUT
Child can export the child() shortcut function when requested. This function
creates and starts the child process in one action.
use Child qw/child/;
my $proc = child {
my $parent = shift;
...
};
You can also request IPC:
use Child qw/child/;
my $child = child {
my $parent = shift;
...
} pipe => 1;
=head1 DETAILS
First you define a child, you do this by constructing a L<Child> object.
Defining a child does not start a new process, it is just the way to define
what the new process will look like. Once you have defined the child you can
start the process by calling $child->start(). One child object can start as
many processes as you like.
When you start a child an L<Child::Link::Proc> object is returned. This object
provides multiple useful methods for interacting with your process. Within the
process itself an L<Child::Link::Parent> is created and passed as the only
parameter to the function used to define the child. The parent object is how
the child interacts with its parent.
=head1 PROCESS MANAGEMENT METHODS
=over 4
=item @procs = Child->all_procs()
Get a list of all the processes that have been started. This list is cleared in
processes when they are started; that is a child will not list its siblings.
=item @pids = Child->all_proc_pids()
Get a list of all the pids of processes that have been started.
=item Child->wait_all()
Call wait() on all processes.
=back
=head1 EXPORTS
=over 4
=item $proc = child( sub { ... } )
=item $proc = child { ... }
=item $proc = child( sub { ... }, $plugin, @data )
=item $proc = child { ... } $plugin => @data
Create and start a process in one action.
=back
=head1 CONSTRUCTOR
=over 4
=item $child = Child->new( sub { ... } )
=item $child = Child->new( sub { ... }, $plugin, @plugin_data )
Create a new Child object. Does not start the child.
=back
=head1 OBJECT METHODS
=over
=item $proc = $child->start()
Start the child process.
=back
=head1 SEE ALSO
=over 4
=item L<Child::Link::Proc>
The proc object that is returned by $child->start()
=item L<Child::Link::Parent>
The parent object that is provided as the argumunt to the function used to
define the child.
=item L<Child::Link::IPC>
The base class for IPC plugin link objects. This provides the IPC methods.
=back
=head1 HISTORY
Most of this was part of L<Parrallel::Runner> intended for use in the L<Fennec>
project. Fennec is being broken into multiple parts, this is one such part.
=head1 FENNEC PROJECT
This module is part of the Fennec project. See L<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.
=over 2
=item L<Fennec> - The core framework
The primary Fennec project that ties them all together.
=back
=head1 AUTHORS
Chad Granum L<exodist7@gmail.com>
=head1 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.
|