Acme-SexualReproduction

 view release on metacpan or  search on metacpan

lib/Acme/SexualReproduction.pm  view on Meta::CPAN

Shares a hash for the male process' chromosomes, waits for the insemination, mixes the genes and spawns the child process. \%chromosomes hash reference is changed in the child process.

=cut

sub female {	
	my ($id, $chromosomes) = @_;
	croak "\$chromosomes must be a HASH reference" unless ref $chromosomes eq 'HASH';
	tie my $sperm, 'IPC::Shareable', {key => $id, create => 1 } or carp("Couldn't copulate with male process: $!"), return;
	sleep 0.5 while !keys %$sperm; # foreplay
	keys %$sperm eq keys %$chromosomes or carp("Chromosome mismatch"), return;
	my %child_chromosomes = map { $_, int rand 2 ? $chromosomes->{$_} : $sperm->{$_} } keys %$chromosomes;
	my $pid = fork;
	carp("Couldn't spawn a child: $!"), return unless defined $pid;
	%$chromosomes = %child_chromosomes if $pid == 0;
	return $pid;
}

=head1 AUTHOR

Ivan Krylov, C<< <krylov.r00t at gmail.com> >>



( run in 1.087 second using v1.01-cache-2.11-cpan-49f99fa48dc )