blib/lib/Acme/SexualReproduction.pm

filter: Statementstmt 100% 43 / 43 Branchbran 66.6% 12 / 18 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 89.3% 67 / 75 CC 11 SCAR 15.5
CC
11
Cov
89%
female
8.3
male
4.1
BEGIN
1.0
CRAP 4.7
↑ Summary
1 package Acme::SexualReproduction;
2  
3 5 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use IPC::Shareable ':lock';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 our @EXPORT_OK = qw(male female);
10 5 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 Acme::SexualReproduction - beacuse fork() is for unicellular ones.
15  
16 =head1 VERSION
17  
18 Version 0.01
19  
20 =cut
21  
22 5 our $VERSION = '0.01';
23  
24  
25 =head1 SYNOPSIS
26  
27 This module allows you to improve the chances of your program kind to survive by mixing genes of their processes when creating a child process. Especially if your program is a K-strategic one.
28  
29 In a "female" process:
30  
31 use Acme::SexualReproduction 'female';
32 my $pid = female('unique ID', \%chromosomes);
33 ...
34  
35 In a "male" process:
36  
37 use Acme::SexualReproduction 'male';
38 male('unique ID', \%chromosomes);
39  
40 The child is spawned then from the female process.
41  
42 =head1 EXPORT
43  
44 Only two functions are exported: one for insemination (a "male" process) and one for allocating a shared hash of chromosomes and spawning a child (a "female" process).
45  
46 =head2 male($id, \%chromosomes);
47  
48 Tries to write the chromosomes to the shared memory of the female process with unique SHM $id. Sadly, does not return the child's PID.
49  
50 =cut
51  
52 =for comment
53 This subroutine was written first just because it was easier. I strongly disclaim any sexual discrimination from my side. It's written just for fun anyways.
54 =cut
55  
56 1 ▶sub male {
Subroutine male: called CC=4 SCAR=14.2
Pod documented
57 1 my ($id, $chromosomes) = @_;
58 1 ▶ croak "\$chromosomes must be a HASH reference" unless ref $chromosomes eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $chromosomes eq "HASH" 0 1 1
59 1 ▶ croak "Male process is sterile" unless keys %$chromosomes;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$chromosomes 0 1 1
60 1 ▶ tie my $sperm, 'IPC::Shareable', { key => $id } || croak "Couldn't copulate with female process, SHM ID $id: $!";
Condition50%1/2
{'key', $id} || croak("Couldn't copulate with female process, SHM ID ${id}: $!")
l!l
10
MC/DC0%0/1
{'key', $id} || croak("Couldn't copulate with female process, SHM ID ${id}: $!")
{'key', $id}
Truth table50%1/2
A
A= {'key', $id}
Aresult
11
00
61 1 (tied $sperm)->shlock;
62 1 @{$sperm}{keys %$chromosomes} = values %$chromosomes;
63 1 (tied $sperm)->shunlock;
64 1 return 1;
65 }
66  
67 =head2 $pid = female($id, \%chromosomes)
68  
69 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.
70  
71 =cut
72  
73 2 ▶sub female {
Subroutine female: called CC=8 SCAR=21.1
Pod documented
74 2 my ($id, $chromosomes) = @_;
75 2 ▶ croak "\$chromosomes must be a HASH reference" unless ref $chromosomes eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $chromosomes eq "HASH" 0 2 2
76 2 ▶ tie my $sperm, 'IPC::Shareable', {key => $id, create => 1 } or carp("Couldn't copulate with male process: $!"), return;
Branch: 1 branch
BranchTrueFalseTotal
tie my $sperm, "IPC::Shareable", {"key", $id, "create", 1} or carp("Couldn't copulate with male process: $!"), return 2 0 2
77 2 ▶ sleep 0.5 while !keys %$sperm; # foreplay
Branch: 1 branch
BranchTrueFalseTotal
keys %$sperm or do { sleep "0.5" } 2 1892 1894
78 2 ▶ keys %$sperm eq keys %$chromosomes or carp("Chromosome mismatch"), return;
Branch: 1 branch
BranchTrueFalseTotal
keys %$sperm eq keys %$chromosomes or carp("Chromosome mismatch"), return 2 0 2
79 2 ▶ my %child_chromosomes = map { $_, int rand 2 ? $chromosomes->{$_} : $sperm->{$_} } keys %$chromosomes;
Branch: 1 branch
BranchTrueFalseTotal
int rand 2 ? : 2 2 4
80 2 my $pid = fork;
81 2 ▶ carp("Couldn't spawn a child: $!"), return unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 2 2
82 2 ▶ %$chromosomes = %child_chromosomes if $pid == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $pid == 0 1 1 2
83 2 return $pid;
84 }
85  
86 =head1 AUTHOR
87  
88 Ivan Krylov, C<< <krylov.r00t at gmail.com> >>
89  
90 =head1 BUGS
91  
92 Please report any bugs or feature requests to C<bug-acme-sexualreproduction at rt.cpan.org>, or through
93 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Acme-SexualReproduction>. I will be notified, and then you'll
94 automatically be notified of progress on your bug as I make changes.
95  
96  
97  
98  
99 =head1 SUPPORT
100  
101 You can find documentation for this module with the perldoc command.
102  
103 perldoc Acme::SexualReproduction
104  
105  
106 You can also look for information at:
107  
108 =over 4
109  
110 =item * RT: CPAN's request tracker (report bugs here)
111  
112 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Acme-SexualReproduction>
113  
114 =item * AnnoCPAN: Annotated CPAN documentation
115  
116 L<http://annocpan.org/dist/Acme-SexualReproduction>
117  
118 =item * CPAN Ratings
119  
120 L<http://cpanratings.perl.org/d/Acme-SexualReproduction>
121  
122 =item * Search CPAN
123  
124 L<http://search.cpan.org/dist/Acme-SexualReproduction/>
125  
126 =back
127  
128  
129 =head1 ACKNOWLEDGEMENTS
130  
131  
132 =head1 LICENSE AND COPYRIGHT
133  
134 Copyright 2013 Ivan Krylov.
135  
136 This program is free software; you can redistribute it and/or modify it
137 under the terms of either: the GNU General Public License as published
138 by the Free Software Foundation; or the Artistic License.
139  
140 See http://dev.perl.org/licenses/ for more information.
141  
142  
143 =cut
144  
145 5 ▶no warnings 'void';
Subroutine BEGIN: called CC=1 SCAR=0.0
146 5 "fork() is for unicellar organisms!";
↑ Summary