File Coverage

blib/lib/IPC/Open2/Simple.pm
Criterion Covered Total %
statement 32 36 88.8
branch 2 4 50.0
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 42 48 87.5


line stmt bran cond sub pod time code
1             package IPC::Open2::Simple;
2              
3 1     1   21107 use strict;
  1         3  
  1         40  
4 1     1   13 use warnings;
  1         2  
  1         29  
5 1     1   895 use IO::Handle;
  1         7460  
  1         46  
6 1     1   1023 use IPC::Open2;
  1         3656  
  1         53  
7 1     1   6 use Exporter 'import';
  1         1  
  1         27  
8 1     1   4 use Carp;
  1         3  
  1         273  
9              
10             our $VERSION = '0.01';
11             our @EXPORT_OK = qw(open2s);
12              
13             sub open2s {
14 1     1 1 312 my ($out, $in, @cmd) = @_;
15              
16             # Sanity checks
17 1         3 for my $arg ($out, $in) {
18 2 50       8 unless (ref($arg) eq 'SCALAR') {
19 0         0 carp "open2s: \$out and \$in both must be a scalarref";
20 0         0 return undef;
21             }
22             }
23 1 50       4 unless (@cmd) {
24 0         0 carp "open2s: empty command line";
25 0         0 return undef;
26             }
27              
28 1         9 my ($fh_out, $fh_in) = (IO::Handle->new, IO::Handle->new);
29              
30 1         43 my $pid = open2($fh_out, $fh_in, @cmd);
31             # open2() will die if it cannot fork, so we
32             # do not need to check $pid here.
33              
34             # Pass the input
35 1         5955 $fh_in->print($$in);
36 1         29 $fh_in->close;
37              
38             # Read the output
39             {
40 1         11 local $/;
  1         12  
41 1         5 until ($fh_out->eof) {
42 1         235 $$out .= $fh_out->getline;
43             }
44             }
45              
46             # Wait for the child's tragic death
47 1         66 waitpid $pid, 0;
48              
49 1         34 return $? >> 8;
50             }
51              
52             1;
53              
54             __END__