Subprocess/Job/Delegate/Handle.pm

filter: Statementstmt 100% 69 / 69 Branchbran 91.6% 11 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 22 / 22 Podpod 0.0% 0 / 8 totaltotal 91.8% 102 / 111 CC 7 SCAR 5.4
CC
7
Cov
99%
child_setup_hook
4.0
_build_pipes
3.0
build_code_args
2.0
CRAP 1.7
« Subprocess/Role/WithDelegates.pm ↑ Summary Subprocess/Types.pm »
1 package AnyEvent::Subprocess::Job::Delegate::Handle;
2 48 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 48 $AnyEvent::Subprocess::Job::Delegate::Handle::VERSION = '1.102912';
4 }
5 # ABSTRACT: share a filehandle or socket with the child
6 48 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 48 ▶use AnyEvent::Util qw(portable_pipe portable_socketpair);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 48 ▶use AnyEvent::Subprocess::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 48 ▶use POSIX qw(dup2);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 48 ▶use MooseX::Types::Moose qw(Str Int GlobRef ArrayRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 48 ▶use AnyEvent::Subprocess::Types qw(Direction);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 48 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 48 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 47 with 'AnyEvent::Subprocess::Job::Delegate';
18  
19 47 has 'direction' => (
20 is => 'ro',
21 isa => Direction,
22 required => 1,
23 documentation => 'r when parent reads a pipe, w when parent writes to a pipe, rw for a socket',
24 );
25  
26 47 has 'name' => (
27 is => 'ro',
28 isa => Str,
29 required => 1,
30 );
31  
32 47 has 'replace' => (
33 is => 'ro',
34 isa => GlobRef|Int,
35 predicate => 'has_replace',
36 );
37  
38 47 has 'pass_to_child' => (
39 is => 'ro',
40 isa => 'Bool',
41 default => 0,
42 required => 1,
43 );
44  
45 47 has 'want_leftovers' => (
46 is => 'ro',
47 isa => 'Bool',
48 default => 0,
49 );
50  
51 47 has 'pipes' => (
52 traits => ['NoClone'],
53 is => 'ro',
54 isa => ArrayRef[GlobRef],
55 lazy_build => 1,
56 );
57  
58 has 'handle_class' => (
59 is => 'ro',
60 isa => 'ClassName',
61 required => 1,
62 240 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
63 240 require AnyEvent::Subprocess::Handle;
64 240 return 'AnyEvent::Subprocess::Handle';
65 },
66 47 );
67  
68 141 ▶sub __build_handle {
Subroutine __build_handle: called CC=1 SCAR=0.0
69 141 my ($self, $fh, @rest) = @_;
70 141 return $self->handle_class->new( fh => $fh, @rest );
71 }
72  
73 has 'run_delegate_class' => (
74 is => 'ro',
75 isa => 'ClassName',
76 required => 1,
77 240 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
78 240 require AnyEvent::Subprocess::Running::Delegate::Handle;
79 240 return 'AnyEvent::Subprocess::Running::Delegate::Handle';
80 },
81 47 );
82  
83 47 has 'handle' => (
84 traits => ['NoClone'],
85 is => 'ro',
86 isa => 'AnyEvent::Subprocess::Handle',
87 lazy_build => 1,
88 );
89  
90 141 ▶sub _build_pipes {
Subroutine _build_pipes: called CC=3 SCAR=11.0
91 141 my $self = shift;
92 141 my $direction = $self->direction;
93  
94 141 ▶ if ($direction eq 'rw') {
Branch: 2 branches
BranchTrueFalseTotal
if ($direction eq 'rw') { } 5 136 141
elsif ($direction eq 'r') { } 94 42 136
95 5 return [ portable_socketpair() ];
96 }
97 elsif ( $direction eq 'r' ) {
98 94 return [ portable_pipe() ];
99 }
100 else {
101 42 return [ reverse (portable_pipe()) ];
102 }
103 }
104  
105 141 ▶sub _build_handle {
Subroutine _build_handle: called CC=1 SCAR=0.0
106 141 my $self = shift;
107  
108 141 my ($name, $direction) = map { $self->$_ } qw/name direction/;
109  
110 141 return $self->__build_handle(
111 $self->pipes->[0],
112 _direction => $direction,
113 _name => "parent $name handle ($direction)",
114 );
115 };
116  
117 141 ▶sub build_run_delegates {
Subroutine build_run_delegates: called CC=1 SCAR=0.0
Pod undocumented
118 141 my $self = shift;
119 141 return $self->run_delegate_class->new(
120 name => $self->name,
121 direction => $self->direction,
122 handle => $self->handle,
123 want_leftovers => $self->want_leftovers,
124 );
125 }
126  
127 91 ▶sub parent_finalize_hook {
Subroutine parent_finalize_hook: called CC=1 SCAR=0.0
Pod undocumented
128 91 my $self = shift;
129  
130 91 close $self->pipes->[1];
131 }
132  
133 50 ▶sub child_setup_hook {
Subroutine child_setup_hook: called CC=4 SCAR=13.9
Pod undocumented
134 50 my $self = shift;
135  
136 50 my $name = $self->handle->name;
137 50 $self->handle->do_not_want; # DO NOT WANT (in child)
138  
139 # reopen fake fds to the "real" ones
140  
141 50 my $ch = $self->pipes->[1];
142  
143 50 ▶ if ($self->has_replace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_replace) 48 2 50
144 48 ▶ my $replacement = ref $self->replace ?
Branch: 1 branch
BranchTrueFalseTotal
ref $self->replace ? : 45 3 48
145 fileno($self->replace) :
146 $self->replace;
147  
148 48 ▶ dup2( fileno($ch), $replacement )
Branch: 1 branch
BranchTrueFalseTotal
dup2(fileno $ch, $replacement) or confess("failed to dup $name to ${replacement}: $!") 48 0 48
149 or confess "failed to dup $name to $replacement: $!";
150 }
151  
152 50 AnyEvent::Util::fh_nonblocking $ch, 0;
153 }
154  
155 50 ▶sub build_code_args {
Subroutine build_code_args: called CC=2 SCAR=6.9
Pod undocumented
156 50 my $self = shift;
157 50 ▶ if ($self->pass_to_child){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pass_to_child) 2 48 50
158 2 return $self->name => $self->pipes->[1];
159 }
160 48 return;
161 }
162  
163 141 ▶sub parent_setup_hook {}
Subroutine parent_setup_hook: called CC=1 SCAR=0.0
Pod undocumented
164 34 ▶sub child_finalize_hook {}
Subroutine child_finalize_hook: called CC=1 SCAR=0.0
Pod undocumented
165 33 ▶sub receive_child_result {}
Subroutine receive_child_result: called CC=1 SCAR=0.0
Pod undocumented
166 1 ▶sub receive_child_error {}
Subroutine receive_child_error: called CC=1 SCAR=0.0
Pod undocumented
167  
168 47 __PACKAGE__->meta->make_immutable;
169  
170 47 1;
171  
172  
173  
174 =pod
175  
176 =head1 NAME
177  
178 AnyEvent::Subprocess::Job::Delegate::Handle - share a filehandle or socket with the child
179  
180 =head1 VERSION
181  
182 version 1.102912
183  
184 =head1 INITARGS
185  
186 =head2 direction
187  
188 'r' for a pipe from the child to the parent, 'w' for a pipe from the
189 parent to the child, 'rw' for a socket.
190  
191 =head2 replace
192  
193 Optional. If specified, can be a Perl filehandle (C<\*STDERR>) or
194 integer fd number (C<2>). This filehandle will be opened to the object
195 created by this delegate in the child. (So if you say
196 C<< direction => 'r', replace => \*STDERR >>, the parent will be able to
197 read the child's STDERR via this delegate. If you say
198 C<< direction => 'w', replace => 3 >>, then the child can open fd #3
199 and read from the parent.)
200  
201 =head2 pass_to_child
202  
203 If you don't want to replace a filehandle or file descriptor number,
204 you can just pass the filehandle object to the child instead. If you
205 set this to true, the child will get its end of the socket or pipe in
206 the argument hash passed to the child coderef. The key will be the
207 same as the name the delegate.
208  
209 =head1 METHODS
210  
211 =head2 handle
212  
213 This is the
214 L<AnyEvent::Subprocess::Handle|AnyEvent::Subprocess::Handle> object
215 that you use to communicate with the child. You typically want to
216 C<push_read> and C<push_write>, but all of
217 L<AnyEvent::Handle|AnyEvent::Handle>'s operations are available. See
218 that manpage for further details; there is a lot you can do, and this
219 module makes it all very easy.
220  
221 =head1 AUTHOR
222  
223 Jonathan Rockway <jrockway@cpan.org>
224  
225 =head1 COPYRIGHT AND LICENSE
226  
227 This software is copyright (c) 2011 by Jonathan Rockway.
228  
229 This is free software; you can redistribute it and/or modify it under
230 the same terms as the Perl 5 programming language system itself.
231  
232 =cut
233  
234  
235 __END__
« Subprocess/Role/WithDelegates.pm ↑ Summary Subprocess/Types.pm »