File Coverage

blib/lib/IPC/Simple/Group.pm
Criterion Covered Total %
statement 44 46 95.6
branch 6 10 60.0
condition n/a
subroutine 14 15 93.3
pod 6 9 66.6
total 70 80 87.5


line stmt bran cond sub pod time code
1             package IPC::Simple::Group;
2             # ABSTRACT: work with several processes as a group
3             $IPC::Simple::Group::VERSION = '0.07';
4              
5 4     4   23 use strict;
  4         8  
  4         97  
6 4     4   16 use warnings;
  4         5  
  4         77  
7              
8 4     4   17 use Carp;
  4         5  
  4         161  
9 4     4   19 use IPC::Simple::Channel qw();
  4         6  
  4         1956  
10              
11             sub new {
12 1     1 0 3 my $class = shift;
13              
14 1         9 my $self = bless{
15             members => {},
16             messages => IPC::Simple::Channel->new,
17             }, $class;
18              
19 1         4 $self->add(@_);
20              
21 1         3 return $self;
22             }
23              
24             sub add {
25 1     1 0 2 my $self = shift;
26              
27 1         4 for (@_) {
28 2 50       6 croak 'processes must be grouped *before* launching them'
29             unless $_->is_ready;
30              
31 2 50       6 croak 'processes must be named to be grouped'
32             unless $_->name;
33              
34             croak 'processes with a recv_cb may not be grouped'
35 2 50       5 if $_->{cb};
36              
37             croak 'processes with a term_cb may not be grouped'
38 2 50       5 if $_->{term_cb};
39             }
40              
41 1         4 for (@_) {
42 2         8 $self->{members}{ $_->{name} } = $_;
43 2     4   6 $_->{recv_cb} = sub{ $self->{messages}->put( $_[0] ) };
  4         18  
44 2     2   7 $_->{term_cb} = sub{ $self->drop( $_[0] ) };
  2         6  
45             }
46             }
47              
48             sub drop {
49 2     2 0 4 my $self = shift;
50              
51             delete $self->{members}{ $_->{name} }
52 2         17 for @_;
53              
54 2 100       4 unless (%{ $self->{members} }) {
  2         23  
55 1         5 $self->{messages}->shutdown;
56             }
57             }
58              
59             sub members {
60 3     3 1 4 my $self = shift;
61 3         4 return values %{ $self->{members} };
  3         14  
62             }
63              
64             sub launch {
65 1     1 1 4 my $self = shift;
66 1         4 $_->launch for $self->members;
67             }
68              
69             sub terminate {
70 1     1 1 2 my $self = shift;
71 1         4 $_->terminate(@_) for $self->members;
72             }
73              
74             sub signal {
75 0     0 1 0 my ($self, $signal) = @_;
76 0         0 $self->signal($signal) for $self->members;
77             }
78              
79             sub join {
80 1     1 1 2 my $self = shift;
81 1         2 $_->join for $self->members;
82             }
83              
84             sub recv {
85 5     5 1 122 my $self = shift;
86 5         20 $self->{messages}->recv;
87             }
88              
89             1;
90              
91             __END__