blib/lib/IPC/Open3/Simple.pm

filter: Statementstmt 98.4% 64 / 65 Branchbran 90.9% 20 / 22 Conditioncond 55.5% 5 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 90.2% 102 / 113 CC 17 SCAR 20.9
CC
17
Cov
93%
run
13.1
new
5.1
BEGIN
1.0
CRAP 8.1
↑ Summary
1 #################################################################
2 #
3 # IPC::Open3::Simple - A simple alternative to IPC::Open3
4 #
5 # $Id: Simple.pm,v 1.7 2006/07/20 13:30:02 erwan Exp $
6 #
7 # 060714 erwan Created
8 #
9 #################################################################
10  
11 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 package IPC::Open3::Simple;
15  
16 2 ▶use Carp qw(croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use IPC::Open3;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 2 our $VERSION = '0.04';
23  
24 #-----------------------------------------------------------------
25 #
26 # new - constructor. takes a hash where keys are in, out and err
27 # and values are closures/coderefs
28 #
29  
30 6 ▶sub new {
Subroutine new: called CC=5 SCAR=16.2
Pod documented
31 6 my($pkg,%args) = @_;
32 6 ▶ $pkg = ref $pkg || $pkg;
Condition33%1/3
ref $pkg || $pkg
l!l&&r!l&&!r
060
MC/DC0%0/2
ref $pkg || $pkg
ref $pkg $pkg
Truth table33%1/3
A || B
A= ref $pkgB= $pkg
ABresult
1X1
011
000
33 6 my $self = bless({},$pkg);
34  
35 6 foreach my $type ('in','out','err') {
36 17 ▶ if (exists $args{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{$type}) 7 10 17
37 7 ▶ croak "".__PACKAGE__."::new expects coderefs" if (ref $args{$type} ne 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
if ref $args{$type} ne "CODE" 1 6 7
38 6 $self->{$type} = $args{$type};
39 }
40 }
41
42 5 return $self;
43 }
44  
45 #-----------------------------------------------------------------
46 #
47 # run - execute a list of shell commands in a separate process
48 # and redirect input/output to the closures provided to new()
49 #
50  
51 7 ▶sub run {
Subroutine run: called CC=13 SCAR=25.7
Pod documented
52 7 my($self,@args) = @_;
53
54 # note: in theory, it should work to write:
55 # my $pid = open3($child_in, $child_out, $child_err, @arguments)
56 # but that does not work (bug?). $child_err is then undefined
57 # (in perldoc for open2, the explanation is that stderr=stdout if $child_out == $child_err, which they do when they are both undefined)
58  
59 # TODO: support interactive ipc with child process?
60
61 7 ▶ my $pid = open3(\*CHILD_IN, \*CHILD_OUT, \*CHILD_ERR, @args)
Branch: 1 branch
BranchTrueFalseTotal
my $pid = open3(\*CHILD_IN, \*CHILD_OUT, \*CHILD_ERR, @args) or confess('ERROR: failed to execute command [' . join(" ", @args) . ']') 7 0 7
62 or confess "ERROR: failed to execute command [".join(" ",@args)."]";
63  
64 7 my $reader = IO::Select->new();
65  
66 7 my $child_in = \*CHILD_IN;
67 7 my $child_out = \*CHILD_OUT;
68 7 my $child_err = \*CHILD_ERR;
69 # $child_in->autoflush; IPC::Open3 does it already
70 7 $child_out->autoflush;
71 7 $child_err->autoflush;
72  
73 # listen to stdout and stderr, or close them
74 7 ▶ if (exists $self->{out}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'out'}) { } 3 4 7
75 3 $reader->add($child_out);
76 } else {
77 4 $child_out->close();
78 }
79  
80 7 ▶ if (exists $self->{err}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'err'}) { } 3 4 7
81 3 $reader->add($child_err);
82 } else {
83 4 $child_err->close();
84 }
85  
86 # forward stdin to provided function, or close it
87 7 ▶ if (exists $self->{in}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'in'}) { } 3 4 7
88 3 &{$self->{in}}($child_in);
89 } else {
90 4 $child_in->close();
91 }
92  
93 # parse output of cvs command
94 7 ▶ if ($reader->handles) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reader->handles) 4 3 7
95 4 ▶ while (my @ready = $reader->can_read()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@ready) = $reader->can_read) 12 4 16
96 12 foreach my $fh (@ready) {
97 14 my $line = <$fh>;
98 14 ▶ if (!defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $line) { } 6 8 14
99 # reached EOF on this filehandle
100 6 $reader->remove($fh);
101 6 $fh->close();
102 } else {
103 8 chomp $line;
104 8 ▶ if ($child_out->opened && fileno($fh) == fileno(\*CHILD_OUT)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($child_out->opened and fileno $fh == fileno \*CHILD_OUT) { } 4 4 8
elsif ($child_err->opened and fileno $fh == fileno \*CHILD_ERR) { } 4 0 4
Condition100%3/3
$child_out->opened and fileno $fh == fileno \*CHILD_OUT
!ll&&!rl&&r
224
MC/DC100%2/2
$child_out->opened and fileno $fh == fileno \*CHILD_OUT
$child_out->opened fileno $fh == fileno \*CHILD_OUT
Truth table100%3/3
A and B
A= $child_out->openedB= fileno $fh == fileno \*CHILD_OUT
ABresult
0X0
100
111
105 4 &{$self->{out}}($line);
106 ▶ } elsif ($child_err->opened && fileno($fh) == fileno(\*CHILD_ERR)) {
Condition33%1/3
$child_err->opened and fileno $fh == fileno \*CHILD_ERR
!ll&&!rl&&r
004
MC/DC0%0/2
$child_err->opened and fileno $fh == fileno \*CHILD_ERR
$child_err->opened fileno $fh == fileno \*CHILD_ERR
Truth table33%1/3
A and B
A= $child_err->openedB= fileno $fh == fileno \*CHILD_ERR
ABresult
0X0
100
111
107 4 &{$self->{err}}($line);
108 } else {
109 0 confess "BUG: got an unexpected filehandle:".Dumper($fh);
110 }
111 }
112 }
113 }
114 }
115  
116 # wait for child process to die
117 7 waitpid($pid, 0);
118  
119 7 return $self;
120 }
121  
122 2 1;
123  
124 __END__
↑ Summary