Acme-SexualReproduction

 view release on metacpan or  search on metacpan

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

	return 1;
}

=head2 $pid = female($id, \%chromosomes)

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> >>

=head1 BUGS

Please report any bugs or feature requests to C<bug-acme-sexualreproduction at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Acme-SexualReproduction>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.




=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Acme::SexualReproduction


You can also look for information at:

=over 4

=item * RT: CPAN's request tracker (report bugs here)

L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Acme-SexualReproduction>

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/Acme-SexualReproduction>

=item * CPAN Ratings

L<http://cpanratings.perl.org/d/Acme-SexualReproduction>

=item * Search CPAN

L<http://search.cpan.org/dist/Acme-SexualReproduction/>

=back


=head1 ACKNOWLEDGEMENTS


=head1 LICENSE AND COPYRIGHT

Copyright 2013 Ivan Krylov.

This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.


=cut

no warnings 'void';
"fork() is for unicellar organisms!";



( run in 0.642 second using v1.01-cache-2.11-cpan-e1769b4cff6 )