File Coverage

blib/lib/AnyEvent/Subprocess/Easy.pm
Criterion Covered Total %
statement 8 10 80.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 12 14 85.7


line stmt bran cond sub pod time code
1             package AnyEvent::Subprocess::Easy;
2             BEGIN {
3 2     2   84671 $AnyEvent::Subprocess::Easy::VERSION = '1.102912';
4             }
5             # ABSTRACT: wrappers around AnyEvent::Subprocess to save typing in simple cases
6 2     2   19 use strict;
  2         4  
  2         64  
7 2     2   10 use warnings;
  2         5  
  2         65  
8              
9 2     2   3707 use AnyEvent;
  0            
  0            
10             use AnyEvent::Subprocess;
11              
12             use Try::Tiny;
13              
14             use Sub::Exporter -setup => {
15             exports => ['qx_nonblock'],
16             };
17              
18             sub _build_input_capture_job {
19             my ($callback, $code, @delegates) = @_;
20              
21             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             return $proc;
37             }
38              
39             sub qx_nonblock {
40             my (@args) = @_;
41              
42             my $cmd = [@args];
43             $cmd = $args[0] if @args == 1;
44              
45             my $result_ready = AnyEvent->condvar;
46             my $callback = sub {
47             my $done = shift;
48              
49             if($done->exit_status != 0){
50             # make "recv" die with error message
51             $result_ready->croak(
52             "Process exited unsuccessfully with exit code: ". $done->exit_value
53             ),
54             }
55             else {
56             # send captured output
57             $result_ready->send(
58             $done->delegate('stdout_capture')->output,
59             );
60             }
61             };
62              
63             my $proc = _build_input_capture_job(
64             $callback, $cmd,
65             );
66              
67             $proc->run;
68              
69             return $result_ready;
70             }
71              
72             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__
142