Subprocess/Easy.pm

filter: Statementstmt 100% 35 / 35 Branchbran 100% 4 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 100% 50 / 50 CC 3 SCAR 2.9
CC
3
Cov
100%
qx_nonblock
2.0
__ANON__
2.0
BEGIN
1.0
CRAP 1.3
« Subprocess/Done.pm ↑ Summary Subprocess/Running/Delegate/MonitorHandle.pm »
1 package AnyEvent::Subprocess::Easy;
2 8 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 $AnyEvent::Subprocess::Easy::VERSION = '1.102912';
4 }
5 # ABSTRACT: wrappers around AnyEvent::Subprocess to save typing in simple cases
6 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 8 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use AnyEvent::Subprocess;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 8 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 8 ▶use Sub::Exporter -setup => {
Subroutine BEGIN: called CC=1 SCAR=0.0
15 exports => ['qx_nonblock'],
16 8 };
17  
18 16 ▶sub _build_input_capture_job {
Subroutine _build_input_capture_job: called CC=1 SCAR=0.0
19 16 my ($callback, $code, @delegates) = @_;
20  
21 16 my $proc = AnyEvent::Subprocess->new(
22 delegates => [
23 'PrintError',
24 { Handle => {
25 name => 'stdout',
26 direction => 'r',
27 replace => \*STDOUT,
28 }},
29 { Capture => { handle => 'stdout' } },
30 @delegates,
31 ],
32 code => $code,
33 on_completion => $callback,
34 );
35  
36 16 return $proc;
37 }
38  
39 16 ▶sub qx_nonblock {
Subroutine qx_nonblock: called CC=2 SCAR=6.9
Pod documented
40 16 my (@args) = @_;
41  
42 16 my $cmd = [@args];
43 16 ▶ $cmd = $args[0] if @args == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @args == 1 13 3 16
44  
45 16 my $result_ready = AnyEvent->condvar;
46 10 ▶ my $callback = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
47 10 my $done = shift;
48  
49 10 ▶ if($done->exit_status != 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($done->exit_status != 0) { } 1 9 10
50 # make "recv" die with error message
51 1 $result_ready->croak(
52 "Process exited unsuccessfully with exit code: ". $done->exit_value
53 ),
54 }
55 else {
56 # send captured output
57 9 $result_ready->send(
58 $done->delegate('stdout_capture')->output,
59 );
60 }
61 16 };
62  
63 16 my $proc = _build_input_capture_job(
64 $callback, $cmd,
65 );
66  
67 16 $proc->run;
68  
69 10 return $result_ready;
70 }
71  
72 7 1;
73  
74  
75  
76 =pod
77  
78 =head1 NAME
79  
80 AnyEvent::Subprocess::Easy - wrappers around AnyEvent::Subprocess to save typing in simple cases
81  
82 =head1 VERSION
83  
84 version 1.102912
85  
86 =head1 SYNOPSIS
87  
88 use AnyEvent::Subprocess::Easy qw(qx_nonblock);
89  
90 my $date = qx_nonblock('date')->recv;
91  
92 =head1 DESCRIPTION
93  
94 I was writing some examples and noticed some patterns that came up
95 again and again, so I converted them to functions. These are opaque
96 and non-customizeable, but might be helpful if you want to do
97 something common without a lot of typing. If they don't work quite
98 the way you want, it is not too hard to use AnyEvent::Subprocess
99 directly.
100  
101 =head1 EXPORTS
102  
103 We use L<Sub::Exporter|Sub::Exporter> here, so you can customize the
104 exports as appropriate.
105  
106 =head2 qx_nonblock( $cmdline | @cmdline )
107  
108 C<qx_nonblock> works like qx, except that it returns a condvar that
109 you C<recv> on to get the captured stdout. The C<recv> will throw an
110 exception if the process you run doesn't exit cleanly.
111  
112 You can pass in one string for the shell to interpret (like C<exec>),
113 or you can pass in a list of arguments (passed directly to C<exec>).
114 You can also pass in a coderef if you like; it will be called with an
115 undefined number of arguments in the child process (and should C<exit 0>
116 if it is successful).
117  
118 =head1 BUGS
119  
120 Not enough "easy" stuff here yet. Please contribute your common
121 patterns!
122  
123 =head1 SEE ALSO
124  
125 L<AnyEvent::Subprocess|AnyEvent::Subprocess>
126  
127 =head1 AUTHOR
128  
129 Jonathan Rockway <jrockway@cpan.org>
130  
131 =head1 COPYRIGHT AND LICENSE
132  
133 This software is copyright (c) 2011 by Jonathan Rockway.
134  
135 This is free software; you can redistribute it and/or modify it under
136 the same terms as the Perl 5 programming language system itself.
137  
138 =cut
139  
140  
141 __END__
« Subprocess/Done.pm ↑ Summary Subprocess/Running/Delegate/MonitorHandle.pm »