blib/lib/IO/Pipe/Producer.pm

filter: Statementstmt 79.4% 58 / 73 Branchbran 82.1% 23 / 28 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 9 / 9 Podpod 33.3% 1 / 3 totaltotal 76.4% 94 / 123 CC 17 SCAR 24.8
CC
17
Cov
79%
getSubroutineProducer
19.8
new
4.0
BEGIN
1.0
CRAP 12.0
↑ Summary
1 package IO::Pipe::Producer;
2  
3 7 ▶use 5.012003;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 7 our @ISA = qw(IO::Pipe);
9 7 ▶use base qw(IO::Pipe);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 7 our $VERSION = '2.0';
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 {
39 #Get the class name
40 27 ▶ my $class = shift(@_);
Subroutine new: called CC=4 SCAR=13.9
Pod documented
41 #Instantiate an instance of the super class
42 27 my $self = $class->SUPER::new();
43 #Bless the instantiation into this class so we can call our own methods
44 27 bless($self,$class);
45 #If a subroutine call was supplied
46 27 ▶ if(scalar(@_))
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) 9 18 27
47 {
48 #Declare file handles for STDOUT and STDERR
49 9 my($fh,$eh);
50 #If new was called in list context
51 9 ▶ if(wantarray)
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 4 5 9
52 {
53 #Fill the handles with the outputs from the subroutine
54 4 ($fh,$eh) = $self->getSubroutineProducer(@_);
55 #Return blessed referents to the file handles
56 3 return(bless($fh,$class),bless($eh,$class));
57 }
58 #Fill the STDOUT handle with the output from the subroutine
59 5 $fh = $self->getSubroutineProducer(@_);
60 #Return blessed referent to the STDOUT handle
61 4 return(bless($fh,$class));
62 }
63 #Return a blessed referent of the object hash
64 18 ▶ if(wantarray)
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 18 18
65 0 {return($self,bless($class->SUPER::new(),$class))}
66 18 return($self);
67 }
68  
69  
70  
71 #This method is also a constructor
72 sub getSubroutineProducer
73 {
74 #Read in subroutine reference
75 27 ▶ my $self = shift;
Subroutine getSubroutineProducer: called CC=14 SCAR=29.8
Pod undocumented
76 27 my $producer_sub = shift;
77 27 my @params = @_;
78 27 my($pid,$error);
79  
80 27 ▶ 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 27 27
Condition33%1/3
not defined $producer_sub or ref $producer_sub ne "CODE"
l!l&&r!l&&!r
0027
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
81 {
82 0 $error = "ERROR:Producer.pm:getSubroutineProducer:A referenced " .
83 "subroutine is required as the first argument to " .
84 "getSubroutineProducer.";
85 0 $Producer::errstr = $error;
86 0 carp($error);
87 0 return(undef);
88 }
89  
90 #Create a pipe
91 27 my $stdout_pipe = $self->SUPER::new();
92 27 my($stderr_pipe);
93 27 ▶ $stderr_pipe = $self->SUPER::new() if(wantarray);
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 12 15 27
94  
95 #Fork off the Producer
96 27 ▶ if(defined($pid = fork()))
Branch: 1 branch
BranchTrueFalseTotal
if (defined($pid = fork)) { } 27 0 27
97 {
98 27 ▶ if($pid)
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) { } 21 6 27
99 {
100 ##
101 ## Parent
102 ##
103  
104 #Create a read file handle
105 21 $stdout_pipe->reader();
106 21 ▶ $stderr_pipe->reader() if(wantarray);
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 9 12 21
107  
108 #Return the read file handle to the consumer
109 21 ▶ if(wantarray)
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 9 12 21
110 9 {return(bless($stdout_pipe,ref($self)),
111 bless($stderr_pipe,ref($self)))}
112 12 return(bless($stdout_pipe,ref($self)));
113 }
114 else
115 {
116 ##
117 ## Child
118 ##
119  
120 #Create a write file handle for the Producer
121 6 $stdout_pipe->writer();
122 6 $stdout_pipe->autoflush;
123 6 ▶ $stderr_pipe->writer() if(defined($stderr_pipe));
Branch: 1 branch
BranchTrueFalseTotal
if defined $stderr_pipe 3 3 6
124 6 ▶ $stderr_pipe->autoflush if(defined($stderr_pipe));
Branch: 1 branch
BranchTrueFalseTotal
if defined $stderr_pipe 3 3 6
125  
126 #Redirect standard outputs to the pipes or kill the child
127 6 ▶ if(!open(STDOUT,">&",\${$stdout_pipe}))
Branch: 2 branches
BranchTrueFalseTotal
if (not open(STDOUT, '>&', \${$stdout_pipe;})) { } 0 6 6
elsif (defined $stderr_pipe and not open(STDERR, '>&', \${$stderr_pipe;})) { } 0 9 9
128 {
129 0 $error = "ERROR:Producer.pm:getSubroutineProducer:Can't " .
130 "redirect stdout to pipe: [" .
131 select($stdout_pipe) .
132 "]. $!";
133 0 $Producer::errstr = $error;
134 0 croak($error);
135 }
136 3 ▶ elsif(defined($stderr_pipe) && !open(STDERR,">&",\${$stderr_pipe}))
Condition66%2/3
defined $stderr_pipe and not open(STDERR, ">&", \${$stderr_pipe;})
!ll&&!rl&&r
330
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
137 {
138 0 $error = "ERROR:Producer.pm:getSubroutineProducer:Can't " .
139 "redirect stderr to pipe: [" .
140 select($stderr_pipe) .
141 "]. $!";
142 0 $Producer::errstr = $error;
143 0 croak($error);
144 }
145  
146 #Call the subroutine passed in (ignore it's return value)
147 6 $producer_sub->(@params);
148  
149 #Close the writer pipes
150 6 close($stdout_pipe);
151 6 ▶ close($stderr_pipe) if(defined($stderr_pipe));
Branch: 1 branch
BranchTrueFalseTotal
if defined $stderr_pipe 3 3 6
152  
153 #Successfully exiting the child process
154 6 exit(0);
155 }
156 }
157 else
158 {
159 0 $error = "ERROR:Producer.pm:getSubroutineProducer:fork() didn't work!";
160 0 $Producer::errstr = $error;
161 0 carp($error);
162 0 return(undef);
163 }
164 }
165  
166  
167 sub getSystemProducer
168 {
169 5 ▶ my $self = shift;
Subroutine getSystemProducer: called CC=1 SCAR=0.0
Pod undocumented
170 5 ▶ return($self->getSubroutineProducer(sub {system(@_)},@_));
Subroutine __ANON__: called CC=1 SCAR=0.0
171 }
172  
173  
174 7 1;
175 __END__
↑ Summary