blib/lib/IO/Pipe/Producer.pm

filter: Statementstmt 74.3% 81 / 109 Branchbran 78.9% 30 / 38 Conditioncond 70.5% 12 / 17 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 90.9% 10 / 11 Podpod 33.3% 1 / 3 totaltotal 73.4% 138 / 188 CC 26 SCAR 30.5
CC
26
Cov
75%
getSubroutineProducer
34.3
__ANON__
6.0
new
4.1
CRAP 21.1
↑ Summary
1 package IO::Pipe::Producer;
2  
3 10 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 10 our @ISA = qw(IO::Pipe);
9 10 ▶use base qw(IO::Pipe);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 10 our $VERSION = '2.02';
12  
13 #NOTICE
14 #
15 #This software and ancillary information (herein called "SOFTWARE") called
16 #Producer.pm is made available under the terms described here. The
17 #SOFTWARE has been approved for release with associated LA-CC number
18 #LA-CC-05-060.
19 #
20 #Unless otherwise indicated, this software has been authored by an employee or
21 #employees of the University of California, operator of the Los Alamos National
22 #Laboratory under Contract No. W-7405-ENG-36 with the U.S. Department of
23 #Energy. The U.S. government has rights to use, reproduce, and distribute this
24 #SOFTWARE. The public may copy, distribute, prepare derivative works and
25 #publicly display this SOFTWARE without charge, provided that this notice and
26 #any statement of authorship are reproduced on all copies. Neither the
27 #government nor the university makes any warranty, express or implied, or
28 #assumes any liability or responsibility for the use of this SOFTWARE.
29 #
30 #If SOFTWARE is modified to produce derivative works, such modified SOFTWARE
31 #should be clearly marked, so as not to confuse it with the version available
32 #from LANL.
33  
34  
35 #Constructor
36  
37 sub new
38 54 ▶ {
Subroutine new: called CC=4 SCAR=14.0
Pod documented
39 #Get the class name
40 54 my $class = shift(@_);
41 #Instantiate an instance of the super class and bless into this class
42 54 my $self = bless($class->SUPER::new(),$class);
43 # Constructing a IO::Pipe results in an "Illegal seek" - clear that out
44 54 $! = undef;
45 #If a subroutine call was supplied
46 54 ▶ if(scalar(@_))
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) 15 39 54
47 {
48 #Declare file handles for STDOUT and STDERR
49 15 my($fh,$eh);
50 #If new was called in list context
51 15 ▶ if(wantarray)
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 7 8 15
52 {
53 #Fill the handles with the outputs from the subroutine
54 7 ($fh,$eh) = $self->getSubroutineProducer(@_);
55 #Bless referents to the file handles
56 6 my($bfh,$beh) = (bless($fh,$class),bless($eh,$class));
57 # Constructing a IO::Pipe results in an "Illegal seek" - clear it
58 6 $! = undef;
59 6 return($bfh,$beh);
60 }
61 #Fill the STDOUT handle with the output from the subroutine
62 8 $fh = $self->getSubroutineProducer(@_);
63 #Return blessed referent to the STDOUT handle
64 7 my $bfh = bless($fh,$class);
65 # Constructing a IO::Pipe results in an "Illegal seek" - clear it
66 7 $! = undef;
67 7 return($bfh);
68 }
69 #Return a blessed referent of the object hash
70 39 ▶ if(wantarray)
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 39 39
71 {
72 #Return a second blessed referent
73 0 my $self2 = bless($class->SUPER::new(),$class);
74 # Constructing a IO::Pipe results in an "Illegal seek" - clear it
75 0 $! = undef;
76 0 return($self,$self2);
77 }
78 39 return($self);
79 }
80  
81  
82  
83 #This method is also a constructor
84 sub getSubroutineProducer
85 54 ▶ {
Subroutine getSubroutineProducer: called CC=21 SCAR=35.3
Pod undocumented
86 #Read in subroutine reference
87 54 my $self = shift;
88 54 my $producer_sub = shift;
89 54 my @params = @_;
90 54 my($pid,$error);
91  
92 54 ▶ if(!defined($producer_sub) || ref($producer_sub) ne 'CODE')
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $producer_sub or ref $producer_sub ne "CODE") 0 54 54
Condition33%1/3
not defined $producer_sub or ref $producer_sub ne "CODE"
l!l&&r!l&&!r
0054
MC/DC0%0/2
not defined $producer_sub or ref $producer_sub ne "CODE"
not defined $producer_sub ref $producer_sub ne "CODE"
Truth table33%1/3
not(A) or B
A= not defined $producer_subB= ref $producer_sub ne "CODE"
ABresult
1X1
011
000
93 {
94 0 $error = "ERROR:Producer.pm:getSubroutineProducer:A referenced " .
95 "subroutine is required as the first argument to " .
96 "getSubroutineProducer.";
97 0 $Producer::errstr = $error;
98 0 carp($error);
99 0 return(undef);
100 }
101  
102 #Create a pipe
103 54 my $stdout_pipe = $self->SUPER::new();
104 54 my($stderr_pipe);
105 54 ▶ $stderr_pipe = $self->SUPER::new() if(wantarray);
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 28 26 54
106  
107 54 ▶ my $caller_sub = (caller(1))[3] || "none";
Condition100%2/2
(caller 1)[3] || 'none'
l!l
3519
MC/DC100%1/1
(caller 1)[3] || 'none'
(caller 1)[3]
Truth table100%2/2
A
A= (caller 1)[3]
Aresult
11
00
108 54 my $issys = $caller_sub eq "IO::Pipe::Producer::getSystemProducer";
109  
110 #Fork off the Producer
111 54 ▶ if(defined($pid = fork()))
Branch: 1 branch
BranchTrueFalseTotal
if (defined($pid = fork)) { } 54 0 54
112 {
113 54 ▶ if($pid)
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) { } 45 9 54
114 {
115 ##
116 ## Parent
117 ##
118  
119 45 $! = undef;
120 #Create a read file handle
121 45 $stdout_pipe->reader();
122 45 ▶ $stderr_pipe->reader() if(wantarray);
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 23 22 45
123  
124 #Return the read file handle to the consumer
125 45 ▶ if(wantarray)
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 23 22 45
126 23 {return(bless($stdout_pipe,ref($self)),
127 bless($stderr_pipe,ref($self)),
128 $pid)}
129 22 return(bless($stdout_pipe,ref($self)));
130 }
131 else
132 {
133 ##
134 ## Child
135 ##
136  
137 #Create a write file handle for the Producer
138 9 $stdout_pipe->writer();
139 9 $stdout_pipe->autoflush;
140 9 ▶ $stderr_pipe->writer() if(defined($stderr_pipe));
Branch: 1 branch
BranchTrueFalseTotal
if defined $stderr_pipe 5 4 9
141 9 ▶ $stderr_pipe->autoflush if(defined($stderr_pipe));
Branch: 1 branch
BranchTrueFalseTotal
if defined $stderr_pipe 5 4 9
142  
143 # $! = undef;
144 #Redirect standard outputs to the pipes or kill the child
145 9 ▶ if(!open(STDOUT,">&",\${$stdout_pipe}))
Branch: 2 branches
BranchTrueFalseTotal
if (not open(STDOUT, '>&', \${$stdout_pipe;})) { } 0 9 9
elsif (defined $stderr_pipe and not open(STDERR, '>&', \${$stderr_pipe;})) { } 0 13 13
146 {
147 0 $error = "ERROR:Producer.pm:getSubroutineProducer:Can't " .
148 "redirect stdout to pipe: [" .
149 select($stdout_pipe) .
150 "]. $!";
151 0 $Producer::errstr = $error;
152 0 croak($error);
153 }
154 5 ▶ elsif(defined($stderr_pipe) && !open(STDERR,">&",\${$stderr_pipe}))
Condition66%2/3
defined $stderr_pipe and not open(STDERR, ">&", \${$stderr_pipe;})
!ll&&!rl&&r
450
MC/DC0%0/2
defined $stderr_pipe and not open(STDERR, ">&", \${$stderr_pipe;})
defined $stderr_pipe not open(STDERR, ">&", \${$stderr_pipe;})
Truth table66%2/3
A and not(B)
A= defined $stderr_pipeB= not open(STDERR, ">&", \${$stderr_pipe;})
ABresult
0X0
100
111
155 {
156 0 $error = "ERROR:Producer.pm:getSubroutineProducer:Can't " .
157 "redirect stderr to pipe: [" .
158 select($stderr_pipe) .
159 "]. $!";
160 0 $Producer::errstr = $error;
161 0 croak($error);
162 }
163  
164 #Track runtime errors/warnings (compile/system/etc) for inclusion
165 #in error stream
166 $SIG{__WARN__} =
167 sub
168 1 ▶ {
Subroutine __ANON__: called CC=2 SCAR=7.6
169 1 my $errin = join('',@_);
170 1 chomp($errin);
171 1 my $err = "WARNING:IO::Pipe::Producer: [$errin].";
172 1 ▶ if(defined($stderr_pipe))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stderr_pipe) { } 1 0 1
173 1 {print STDERR ($err)}
174 else
175 {
176 0 chop($err);
177 0 carp($err);
178 }
179 9 };
180  
181 $SIG{__DIE__} =
182 sub
183 0 ▶ {
Subroutine __ANON__: not called CC=2 SCAR=17.9
184 0 my $errin = join('',@_);
185 0 chomp($errin);
186 0 my $err = "ERROR:IO::Pipe::Producer: [$errin].";
187 0 $@ = '';
188 0 ▶ if(defined($stderr_pipe))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stderr_pipe) { } 0 0 0
189 0 {print STDERR ($err)}
190 else
191 {
192 0 chop($err);
193 0 carp($err);
194 }
195 #Calling die() suppresses output of unwrapped fatal errors.
196 0 die();
197 9 };
198  
199 9 my $exit_code = 0;
200  
201 #Call the subroutine passed in & get it's return value
202 9 my $raw_exit_code = int($producer_sub->(@params));
203  
204 9 ▶ if($issys && defined($raw_exit_code) && $raw_exit_code != -1)
Branch: 1 branch
BranchTrueFalseTotal
if ($issys and defined $raw_exit_code and $raw_exit_code != -1) 3 6 9
Condition83%5/6
$issys and defined $raw_exit_code and $raw_exit_code != -1
!ll&&!rl&&r
423
$issys and defined $raw_exit_code
!ll&&!rl&&r
405
MC/DC66%2/3
$issys and defined $raw_exit_code and $raw_exit_code != -1
$issys defined $raw_exit_code $raw_exit_code != -1
Truth table75%3/4
A and B and C
A= $issysB= defined $raw_exit_codeC= $raw_exit_code != -1
ABCresult
0XX0
10X0
1100
1111
205 3 {$exit_code = $raw_exit_code >> 8}
206  
207 9 ▶ if(!defined($raw_exit_code) || $raw_exit_code == -1)
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $raw_exit_code or $raw_exit_code == -1) 2 7 9
Condition66%2/3
not defined $raw_exit_code or $raw_exit_code == -1
l!l&&r!l&&!r
027
MC/DC50%1/2
not defined $raw_exit_code or $raw_exit_code == -1
not defined $raw_exit_code $raw_exit_code == -1
Truth table66%2/3
not(A) or B
A= not defined $raw_exit_codeB= $raw_exit_code == -1
ABresult
1X1
011
000
208 {
209 2 ▶ if(defined($stderr_pipe))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stderr_pipe) 1 1 2
210 {
211 1 $error = "Unable to determine system call exit status";
212 1 $Producer::errstr = $error;
213 1 carp($error);
214 }
215 2 $exit_code = 255;
216 }
217  
218 #Close the writer pipes
219 9 close($stdout_pipe);
220 9 ▶ close($stderr_pipe) if(defined($stderr_pipe));
Branch: 1 branch
BranchTrueFalseTotal
if defined $stderr_pipe 5 4 9
221  
222 #Exit with the exit status
223 9 exit($exit_code);
224 }
225 }
226 else
227 {
228 0 $error = "ERROR:Producer.pm:getSubroutineProducer:fork() didn't work!";
229 0 $Producer::errstr = $error;
230 0 carp($error);
231 0 return(undef);
232 }
233 }
234  
235  
236 sub getSystemProducer
237 20 ▶ {
Subroutine getSystemProducer: called CC=1 SCAR=0.0
Pod undocumented
238 20 my $self = shift;
239 20 ▶ return($self->getSubroutineProducer(sub {system(@_)},@_));
Subroutine __ANON__: called CC=1 SCAR=0.0
240 }
241  
242  
243 10 1;
244 __END__
↑ Summary