Simple.pm

filter: Statementstmt 87.5% 14 / 16 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 90.9% 20 / 22 CC 1 SCAR 0.5
CC
1
Cov
88%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Simple/Process.pm ↑ Summary
1 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use 5.042;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package IO::Async::Open3::Simple 0.01 {
5  
6 2 ▶ use IPC::Open3 qw( open3 );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶ use Scalar::Util qw( reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶ use Symbol qw( gensym );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 0 ▶ use IO::Async::Loop;
Subroutine BEGIN: called CC=1 SCAR=2.6
10 use IO::Async::Stream;
11 use IO::Async::Open3::Simple::Process;
12 use Carp qw( croak );
13 use File::Temp ();
14  
15 # ABSTRACT: Interface to open3 under IO::Async
16  
17  
18 sub new ( $class, @rest ) {
19 my $default_handler = sub { };
20 my $args = ( reftype( $rest[0] ) || '' ) eq 'HASH' ? $rest[0] : {@rest};
21 my %self;
22 croak "stdin passed into IO::Async::Open3::Simple->new no longer supported" if $args->{stdin};
23 croak "raw passed into IO::Async::Open3::Simple->new no longer supported" if $args->{raw};
24 $self{$_} = $args->{$_} || $default_handler
25 for qw( on_stdout on_stderr on_start on_exit on_signal on_fail on_error on_success );
26 $self{loop} = $args->{loop} if $args->{loop};
27 bless \%self, $class;
28 }
29  
30  
31 sub run ( $self, @arguments ) {
32 croak "run method requires at least one argument"
33 unless @arguments >= 1;
34  
35 my $proc_user = ( ref $arguments[-1] eq 'CODE' ? pop @arguments : sub ($proc) { } );
36  
37 my $stdin;
38 $stdin = pop @arguments if @arguments && ref $arguments[-1];
39  
40 my $program = shift @arguments;
41  
42 my ( $child_stdin, $child_stdout, $child_stderr );
43 $child_stderr = gensym;
44  
45 # keep the temp file alive until open3 has dup'd it into the child
46 my $stdin_file;
47 if ( defined $stdin ) {
48 $stdin_file = File::Temp->new;
49 $stdin_file->autoflush(1);
50 $stdin_file->print(
51 ref($stdin) eq 'ARRAY'
52 ? join( "\n", $stdin->@* )
53 : $stdin->$*
54 );
55 $stdin_file->seek( 0, 0 );
56 $child_stdin = '<&' . fileno($stdin_file);
57 }
58  
59 my $loop = $self->{loop} ||= IO::Async::Loop->new;
60  
61 my $pid = eval { open3 $child_stdin, $child_stdout, $child_stderr, $program, @arguments };
62  
63 if ( my $error = $@ ) {
64 $self->{on_error}->( $error, $program, @arguments );
65 return;
66 }
67  
68 my $proc = IO::Async::Open3::Simple::Process->new( $pid, $child_stdin );
69 $proc_user->($proc);
70  
71 $self->{on_start}->( $proc, $program, @arguments );
72  
73 my $emit = sub ( $ref, $line ) {
74 $line =~ s/(\015?\012|\015)$//;
75 ref($ref) eq 'ARRAY' ? ( push $ref->@*, $line ) : $ref->( $proc, $line );
76 return;
77 };
78  
79 my $stdout_open = 1;
80 my $stderr_open = 1;
81 my $exited = 0;
82 my ( $exit_value, $signal );
83  
84 my ( $stdout_stream, $stderr_stream );
85  
86 my $finish = sub () {
87 return if $stdout_open || $stderr_open || !$exited;
88  
89 $proc->close;
90  
91 $self->{on_exit}->( $proc, $exit_value, $signal );
92 $self->{on_signal}->( $proc, $signal ) if $signal > 0;
93 $self->{on_fail}->( $proc, $exit_value ) if $exit_value > 0;
94 $self->{on_success}->($proc) if $signal == 0 && $exit_value == 0;
95  
96 undef $stdout_stream;
97 undef $stderr_stream;
98 undef $proc;
99 return;
100 };
101  
102 my $make_stream = sub ( $handle, $which, $open_ref ) {
103 my $stream = IO::Async::Stream->new(
104 read_handle => $handle,
105 on_read => sub ( $, $buffref, $eof ) {
106 while ( $buffref->$* =~ s/^(.*?\012)// ) {
107 $emit->( $self->{$which}, $1 );
108 }
109 if ( $eof && length $buffref->$* ) {
110 my $line = $buffref->$*;
111 $buffref->$* = '';
112 $emit->( $self->{$which}, $line );
113 }
114 return 0;
115 },
116 on_read_eof => sub ($) {
117 $open_ref->$* = 0;
118 $finish->();
119 return;
120 },
121 );
122 $loop->add($stream);
123 return $stream;
124 };
125  
126 $stdout_stream = $make_stream->( $child_stdout, 'on_stdout', \$stdout_open );
127 $stderr_stream = $make_stream->( $child_stderr, 'on_stderr', \$stderr_open );
128  
129 $loop->watch_process(
130 $pid => sub ( $, $status ) {
131 ( $exit_value, $signal ) = ( $status >> 8, $status & 127 );
132 $exited = 1;
133 $finish->();
134 return;
135 }
136 );
137  
138 $self;
139 }
140  
141 }
142  
143 __END__
« Simple/Process.pm ↑ Summary