Subprocess/DefaultDelegates.pm

filter: Statementstmt 98.8% 82 / 83 Branchbran 50.0% 4 / 8 Conditioncond 57.4% 27 / 47 MC/DCmcdc 12.0% 3 / 25 Subroutinesub 100% 22 / 22 Podpod n/a totaltotal 74.5% 138 / 185 CC 27 SCAR 13.2
CC
27
Cov
82%
__ANON__
7.6
__ANON__
5.4
__ANON__
4.8
CRAP 3.7
« Subprocess/Handle.pm ↑ Summary Subprocess/Role/WithDelegates/Manager.pm »
1 package AnyEvent::Subprocess::DefaultDelegates;
2 48 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 48 $AnyEvent::Subprocess::DefaultDelegates::VERSION = '1.102912';
4 }
5 # ABSTRACT: sets up default delegate name/builder mappings
6  
7 48 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 48 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 48 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 48 ▶use AnyEvent::Subprocess::Role::WithDelegates::Manager qw(register_delegate);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 48 ▶use AnyEvent::Subprocess::Job::Delegate::Callback;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 48 ▶use AnyEvent::Subprocess::Job::Delegate::CaptureHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 48 ▶use AnyEvent::Subprocess::Job::Delegate::CompletionCondvar;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 48 ▶use AnyEvent::Subprocess::Job::Delegate::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 48 ▶use AnyEvent::Subprocess::Job::Delegate::MonitorHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 48 ▶use AnyEvent::Subprocess::Job::Delegate::PrintError;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 48 ▶use AnyEvent::Subprocess::Job::Delegate::Pty;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 48 ▶use AnyEvent::Subprocess::Job::Delegate::Timeout;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 47 register_delegate( 'Handle' => 'AnyEvent::Subprocess::Job::Delegate::Handle' );
23  
24 62 ▶register_delegate( 'StandardHandles' => sub {
Subroutine __ANON__: called CC=4 SCAR=14.9
25 62 ▶ my $args = shift || {};
Condition50%1/2
shift() || {}
l!l
062
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
26 62 ▶ my $prefix = $args->{prefix} || '';
Condition50%1/2
$args->{'prefix'} || ''
l!l
062
MC/DC0%0/1
$args->{'prefix'} || ''
$args->{'prefix'}
Truth table50%1/2
A
A= $args->{'prefix'}
Aresult
11
00
27 62 ▶ my $class = $args->{class} || 'AnyEvent::Subprocess::Job::Delegate::Handle';
Condition50%1/2
$args->{'class'} || 'AnyEvent::Subprocess::Job::Delegate::Handle'
l!l
062
MC/DC0%0/1
$args->{'class'} || 'AnyEvent::Subprocess::Job::Delegate::Handle'
$args->{'class'}
Truth table50%1/2
A
A= $args->{'class'}
Aresult
11
00
28  
29 return (
30 62 $class->new(
31 name => "${prefix}stdin",
32 direction => 'w',
33 replace => \*STDIN,
34 ),
35 $class->new(
36 name => "${prefix}stdout",
37 direction => 'r',
38 replace => \*STDOUT,
39 ),
40 $class->new(
41 name => "${prefix}stderr",
42 direction => 'r',
43 replace => \*STDERR,
44 ),
45 );
46 47 });
47  
48 10 ▶register_delegate( 'CommHandle' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.7
49 10 ▶ my $args = shift || {};
Condition50%1/2
shift() || {}
l!l
010
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
50 10 ▶ my $name = $args->{name} || 'comm';
Condition50%1/2
$args->{'name'} || 'comm'
l!l
010
MC/DC0%0/1
$args->{'name'} || 'comm'
$args->{'name'}
Truth table50%1/2
A
A= $args->{'name'}
Aresult
11
00
51  
52 10 return AnyEvent::Subprocess::Job::Delegate::Handle->new(
53 name => $name,
54 direction => 'rw',
55 pass_to_child => 1,
56 );
57 47 });
58  
59 8 ▶register_delegate( 'Pty' => sub {
Subroutine __ANON__: called CC=4 SCAR=15.6
60 8 ▶ my $args = shift || {};
Condition50%1/2
shift() || {}
l!l
08
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
61 8 ▶ $args->{name} ||= 'pty';
Condition50%1/2
$args->{"name"} ||= "pty"
l!l
08
MC/DC0%0/1
$args->{"name"} ||= "pty"
$args->{"name"}
Truth table50%1/2
A
A= $args->{"name"}
Aresult
11
00
62  
63 8 ▶ if(delete $args->{stderr}){
Branch: 1 branch
BranchTrueFalseTotal
if (delete $args->{"stderr"}) 0 8 8
64 $args->{redirect_handles} = [
65 0 \*STDIN,
66 \*STDOUT,
67 \*STDERR,
68 ];
69 }
70  
71 8 return AnyEvent::Subprocess::Job::Delegate::Pty->new(%$args);
72 47 });
73  
74 58 ▶register_delegate( 'CompletionCondvar' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.7
75 58 ▶ my $args = shift || {};
Condition50%1/2
shift() || {}
l!l
058
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
76 58 ▶ $args->{name} ||= 'completion_condvar';
Condition50%1/2
$args->{"name"} ||= "completion_condvar"
l!l
058
MC/DC0%0/1
$args->{"name"} ||= "completion_condvar"
$args->{"name"}
Truth table50%1/2
A
A= $args->{"name"}
Aresult
11
00
77  
78 58 return AnyEvent::Subprocess::Job::Delegate::CompletionCondvar->new(%$args);
79 47 });
80  
81 4 ▶register_delegate( 'Callback' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
82 4 ▶ my $args = shift || {};
Condition50%1/2
shift() || {}
l!l
40
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
83 4 ▶ $args->{name} ||= 'callback';
Condition100%2/2
$args->{"name"} ||= "callback"
l!l
22
MC/DC100%1/1
$args->{"name"} ||= "callback"
$args->{"name"}
Truth table100%2/2
A
A= $args->{"name"}
Aresult
11
00
84  
85 4 return AnyEvent::Subprocess::Job::Delegate::Callback->new(%$args);
86 47 });
87  
88 52 ▶register_delegate( 'Capture' => sub {
Subroutine __ANON__: called CC=4 SCAR=14.2
89 52 ▶ my $args = shift || {};
Condition50%1/2
shift() || {}
l!l
520
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
90 52 ▶ confess 'need handle' unless $args->{handle};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"handle"} 0 52 52
91 52 ▶ $args->{name} ||= $args->{handle} . '_capture';
Condition100%2/2
$args->{"name"} ||= $args->{"handle"} . '_capture'
l!l
3220
MC/DC100%1/1
$args->{"name"} ||= $args->{"handle"} . '_capture'
$args->{"name"}
Truth table100%2/2
A
A= $args->{"name"}
Aresult
11
00
92  
93 52 return AnyEvent::Subprocess::Job::Delegate::CaptureHandle->new(%$args);
94 47 });
95  
96 4 ▶register_delegate( 'MonitorHandle' => sub {
Subroutine __ANON__: called CC=7 SCAR=20.2
97 4 ▶ my $args = shift || {};
Condition50%1/2
shift() || {}
l!l
40
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
98 4 ▶ confess 'need handle' unless $args->{handle};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"handle"} 0 4 4
99 4 ▶ confess 'need callbacks' unless $args->{callbacks} || $args->{callback};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"callbacks"} or $args->{"callback"} 0 4 4
Condition66%2/3
$args->{"callbacks"} or $args->{"callback"}
l!l&&r!l&&!r
220
MC/DC0%0/2
$args->{"callbacks"} or $args->{"callback"}
$args->{"callbacks"} $args->{"callback"}
Truth table66%2/3
A or B
A= $args->{"callbacks"}B= $args->{"callback"}
ABresult
1X1
011
000
100  
101 4 my $handle = $args->{handle};
102 4 ▶ $args->{name} ||= $handle . '_monitor';
Condition100%2/2
$args->{"name"} ||= $handle . '_monitor'
l!l
22
MC/DC100%1/1
$args->{"name"} ||= $handle . '_monitor'
$args->{"name"}
Truth table100%2/2
A
A= $args->{"name"}
Aresult
11
00
103  
104 4 ▶ $args->{callbacks} ||= $args->{callback};
Condition66%2/3
$args->{"callbacks"} ||= $args->{"callback"}
l!l&&r!l&&!r
220
MC/DC0%0/2
$args->{"callbacks"} ||= $args->{"callback"}
$args->{"callbacks"} $args->{"callback"}
Truth table66%2/3
A ||= B
A= $args->{"callbacks"}B= $args->{"callback"}
ABresult
1X1
011
000
105 4 delete $args->{callback};
106  
107 4 return AnyEvent::Subprocess::Job::Delegate::MonitorHandle->new(%$args);
108 47 });
109  
110 32 ▶register_delegate('PrintError' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.7
111 32 ▶ my $args = shift || {};
Condition50%1/2
shift() || {}
l!l
032
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
112 32 ▶ $args->{name} ||= 'error_printer';
Condition50%1/2
$args->{"name"} ||= "error_printer"
l!l
032
MC/DC0%0/1
$args->{"name"} ||= "error_printer"
$args->{"name"}
Truth table50%1/2
A
A= $args->{"name"}
Aresult
11
00
113  
114 32 return AnyEvent::Subprocess::Job::Delegate::PrintError->new(%$args);
115 47 });
116  
117 4 ▶register_delegate( 'Timeout', sub {
Subroutine __ANON__: called CC=4 SCAR=16.9
118 4 my $args = shift;
119 return AnyEvent::Subprocess::Job::Delegate::Timeout->new(
120 name => $args->{name} || 'timeout',
121 time_limit => $args->{time_limit} || $args->{timeout},
122 4 ▶ kill_with => $args->{kill_with} || 9,
Condition42%3/7
$args->{'name'} || 'timeout'
l!l
04
$args->{'time_limit'} || $args->{'timeout'}
l!l&&r!l&&!r
040
$args->{'kill_with'} || 9
l!l
04
MC/DC0%0/1
$args->{'name'} || 'timeout'
$args->{'name'}
MC/DC0%0/2
$args->{'time_limit'} || $args->{'timeout'}
$args->{'time_limit'} $args->{'timeout'}
MC/DC0%0/1
$args->{'kill_with'} || 9
$args->{'kill_with'}
Truth table50%1/2
A
A= $args->{'name'}
Aresult
11
00
Truth table33%1/3
A || B
A= $args->{'time_limit'}B= $args->{'timeout'}
ABresult
1X1
011
000
Truth table50%1/2
A
A= $args->{'kill_with'}
Aresult
11
00
123 );
124 47 });
125  
126 47 1;
127  
128  
129  
130 =pod
131  
132 =head1 NAME
133  
134 AnyEvent::Subprocess::DefaultDelegates - sets up default delegate name/builder mappings
135  
136 =head1 VERSION
137  
138 version 1.102912
139  
140 =head1 DELEGATES PROVIDED
141  
142 =head2 Handle
143  
144 Provides connections to an arbitrary filehandle / fd / pipe / socket /
145 etc.
146  
147 See L<AnyEvent::Subprocess::Job::Delegate::Handle>
148  
149 =head2 StandardHandles
150  
151 Provides connections to the child's STDIN/STDOUT/STDERR handles.
152 Delegates are named stdin/stdout/stderr. Optional arg prefix adds a
153 prefix string to the delegates' names.
154  
155 =head2 CommHandle
156  
157 Provides a (bidirectional) socket to be shared between the child and
158 parent. Optional arg name provides delegate name (so you can have
159 more than one, if desired).
160  
161 Optional arg name controls name; defaults to 'comm.
162  
163 =head2 Pty
164  
165 Provides the child with stdin and stdout attached to a pseudo-tty, and
166 provides the parent with a delegate to control this. Optional arg
167 stderr controls whether or not the child's stderr will also go to the
168 pty; defaults to no.
169  
170 Optional arg name controls name; defaults to 'pty'.
171  
172 =head2 CompletionCondvar
173  
174 Supplies a delegate that is a L<AnyEvent::Condvar> that is sent the
175 child exit information ("Done class") when the child process exits.
176  
177 =head2 Callback
178  
179 Sets up an
180 L<AnyEvent::Subprocess::Job::Delegate::Callback|AnyEvent::Subprocess::Job::Delegate::Callback>
181 delegate. Optional argument name controls callback instance name;
182 defaults to 'callback'.
183  
184 =head2 Capture
185  
186 Captures a handle. Mandatory arg handle is the name of the handle
187 delegate to capture. The handle must be readable by the parent.
188 (i.e., a socket or a pipe from the child to the parent.)
189  
190 Delegate is named '[handle name]_capture'.
191  
192 (Note that you should not use the captured handle for reading anymore;
193 this delegate will steal all input. Captured output is returned in
194 via a delegate in the "done class".)
195  
196 =head2 MonitorHandle
197  
198 Calls a list of coderefs whenever a line is read from a handle.
199  
200 =head2 PrintError
201  
202 Delegate that calls a callback in the child to print the exception (if
203 any) the child throws.
204  
205 Use WithResult if you want to actually get the exception in the parent.
206  
207 =head1 Timeout
208  
209 Kill the subprocess with a signal C<kill_with> after C<timeout>
210 seconds elapse. See L<AnyEvent::Subprocess::Job::Delegate::Timeout>.
211  
212 =head1 SEE ALSO
213  
214 See the test suite to see all of these shortcuts in use.
215  
216 =head1 AUTHOR
217  
218 Jonathan Rockway <jrockway@cpan.org>
219  
220 =head1 COPYRIGHT AND LICENSE
221  
222 This software is copyright (c) 2011 by Jonathan Rockway.
223  
224 This is free software; you can redistribute it and/or modify it under
225 the same terms as the Perl 5 programming language system itself.
226  
227 =cut
228  
229  
230 __END__
« Subprocess/Handle.pm ↑ Summary Subprocess/Role/WithDelegates/Manager.pm »