lib/IPC/RunExternal.pm

filter: Statementstmt 62.3% 73 / 117 Branchbran 25.0% 10 / 40 Conditioncond 6.6% 1 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 100% 11 / 11 Podpod 100% 1 / 1 totaltotal 49.4% 96 / 194 SCAR 48.3
CC
27
Cov
49%
runexternal
263.9
BEGIN
1.0
BEGIN
1.0
CRAP 124.6
« blib/lib/IPC/RunExternal.pm ↑ Summary
1 ## no critic (Documentation::RequirePodAtEnd)
2 ## no critic (Documentation::RequirePodSections)
3 ## no critic (ControlStructures::ProhibitDeepNests)
4  
5 package IPC::RunExternal;
6  
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use 5.010000;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # ABSTRACT: Execute an external command conveniently by hiding the details of IPC::Open3.
12  
13 1 our $VERSION = '0.100'; # VERSION: generated by DZP::OurPkgVersion
14  
15  
16 1 ▶use English '-no_match_vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use IPC::Open3;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use IO::Select; # for select
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Symbol 'gensym'; # for gensym
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 BEGIN {
23 1 ▶ use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶ our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 1 @ISA = qw(Exporter DynaLoader);
27 1 @EXPORT = qw(runexternal);
28 1 %EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
29 1 @EXPORT_OK = qw(runexternal);
30 }
31 1 our @EXPORT_OK;
32  
33 # CONSTANTS for this module
34 1 my $TRUE = 1;
35 1 my $FALSE = 0;
36 1 my $EMPTY_STR = q{};
37  
38 1 my $DEFAULT_PRINT_PROGRESS_INDICATOR = $FALSE;
39 1 my $DEFAULT_PROGRESS_INDICATOR_CHARACTER = q{.};
40 1 my $DEFAULT_EXECUTE_EVERY_SECOND_ROUTINE_POINTER = $FALSE;
41 1 my $EXIT_STATUS_OK = 1;
42 1 my $EXIT_STATUS_TIMEOUT = 0;
43 1 my $EXIT_STATUS_FAILED = -1;
44 1 my $SIGKILL = 9;
45  
46 # GLOBALS
47 # No global variables
48  
49  
50  
51  
52 sub runexternal {
53  
54 # Parameters
55 7 ▶ my ($command, $input, $timeout, $parameter_tags) = @_;
Subroutine runexternal: called CC=27 SCAR=55.8
Pod documented
56  
57 7 ▶ if(!defined $command) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $command) 0 7 7
58 0 croak('Parameter \'command\' is not initialized!');
59 }
60 7 ▶ if(!defined $input) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $input) 0 7 7
61 0 croak('Parameter \'input\' is not initialized!');
62 }
63 7 ▶ if($timeout < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timeout < 0) 0 7 7
64 0 croak('Parameter \'timeout\' is not valid!');
65 }
66  
67 7 my $print_progress_indicator = $DEFAULT_PRINT_PROGRESS_INDICATOR;
68 7 ▶ if(exists $parameter_tags->{'print_progress_indicator'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $parameter_tags->{"print_progress_indicator"}) 0 7 7
69 0 ▶ if($parameter_tags->{'print_progress_indicator'} == $FALSE ||
Branch: 1 branch
BranchTrueFalseTotal
if ($parameter_tags->{'print_progress_indicator'} == $FALSE or $parameter_tags->{'print_progress_indicator'} == $TRUE) { } 0 0 0
Condition0%0/3
$parameter_tags->{"print_progress_indicator"} == $FALSE or $parameter_tags->{"print_progress_indicator"} == $TRUE
l!l&&r!l&&!r
000
MC/DC0%0/2
$parameter_tags->{"print_progress_indicator"} == $FALSE or $parameter_tags->{"print_progress_indicator"} == $TRUE
$parameter_tags->{"print_progress_indicator"} == $FALSE $parameter_tags->{"print_progress_indicator"} == $TRUE
Truth table0%0/3
A or B
A= $parameter_tags->{"print_progress_indicator"} == $FALSEB= $parameter_tags->{"print_progress_indicator"} == $TRUE
ABresult
1X1
011
000
70 $parameter_tags->{'print_progress_indicator'} == $TRUE) {
71 0 $print_progress_indicator = $parameter_tags->{'print_progress_indicator'};
72 }
73 else {
74 0 croak('Parameter \'print_progress_indicator\' is not valid (must be 1/0)!');
75 }
76 }
77  
78 7 my $progress_indicator_char = $DEFAULT_PROGRESS_INDICATOR_CHARACTER;
79 7 ▶ if(exists $parameter_tags->{'progress_indicator_char'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $parameter_tags->{"progress_indicator_char"}) 1 6 7
80 1 $progress_indicator_char = $parameter_tags->{'progress_indicator_char'};
81 }
82  
83 7 my $execute_every_second = $DEFAULT_EXECUTE_EVERY_SECOND_ROUTINE_POINTER;
84 7 ▶ if(exists $parameter_tags->{'execute_every_second'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $parameter_tags->{"execute_every_second"}) 0 7 7
85 0 ▶ if(ref($parameter_tags->{'execute_every_second'}) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $parameter_tags->{'execute_every_second'} eq 'CODE') { } 0 0 0
86 0 $execute_every_second = $parameter_tags->{'execute_every_second'};
87 }
88 else {
89 0 croak('Parameter execute_every_second is not a code reference!');
90 }
91 }
92  
93 # Variables
94 7 my $command_exit_status = $EXIT_STATUS_OK;
95 7 my $output_std = $EMPTY_STR;
96 7 my $output_error = $EMPTY_STR;
97 7 my $output_all = $EMPTY_STR;
98  
99 # Validity check
100 7 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
if ($command ne $EMPTY_STR and $timeout >= 0) { } 7 0 7
Condition33%1/3
$command ne $EMPTY_STR and $timeout >= 0
!ll&&!rl&&r
007
MC/DC0%0/2
$command ne $EMPTY_STR and $timeout >= 0
$command ne $EMPTY_STR $timeout >= 0
Truth table33%1/3
A and B
A= $command ne $EMPTY_STRB= $timeout >= 0
ABresult
0X0
100
111
101 $command ne $EMPTY_STR
102 #&& defined($input)
103 && $timeout >= 0
104 )
105 {
106 7 $OUTPUT_AUTOFLUSH = $TRUE; # Equals to var $|. Flushes always after writing.
107 7 my ($infh,$outfh,$errfh); # these are the FHs for our child
108 7 $errfh = gensym(); # we create a symbol for the errfh
109 # because open3 will not do that for us
110 7 my $pid;
111 # Read Perl::Critic::Policy::ErrorHandling::RequireCheckingReturnValueOfEval
112 # for an evil eval weakness and a dasdardly difficult eval handling.
113 7 my $eval_ok = 1;
114 eval {
115 7 $pid = open3($infh, $outfh, $errfh, $command);
116 # To cover the possiblity that an operation may correctly return a
117 # false value, end the block with "1":
118 0 1;
119 7 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $pid = open3($infh, $outfh, $errfh, $command); 1 } } or do { $eval_ok = 0 } 7 0 7
120 7 $eval_ok = 0;
121 };
122 7 ▶ if($eval_ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($eval_ok) { } 0 7 7
123 0 print $infh $input;
124 0 close $infh;
125 0 my $sel = new IO::Select; # create a select object to notify
126 # us on reads on our FHs
127 0 $sel->add($outfh,$errfh); # add the FHs we're interested in
128 0 my $out_handles_open = 2;
129 0 ▶ for(my $slept_secs = -1; $out_handles_open > 0 && $slept_secs < $timeout; $slept_secs++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($out_handles_open > 0 and $slept_secs < $timeout) 0 0 0
Condition0%0/3
$out_handles_open > 0 and $slept_secs < $timeout
!ll&&!rl&&r
000
MC/DC0%0/2
$out_handles_open > 0 and $slept_secs < $timeout
$out_handles_open > 0 $slept_secs < $timeout
Truth table0%0/3
A and B
A= $out_handles_open > 0B= $slept_secs < $timeout
ABresult
0X0
100
111
130 0 ▶ while(my @ready = $sel->can_read(1)) { # read ready, timeout after 1 second.
Branch: 1 branch
BranchTrueFalseTotal
if (my(@ready) = $sel->can_read(1)) 0 0 0
131 0 foreach my $fh (@ready) {
132 0 my $line = <$fh>; # read one line from this fh
133 0 ▶ if( !(defined $line) ){ # EOF on this FH
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 0 0 0
134 0 $sel->remove($fh); # remove it from the list
135 0 $out_handles_open -= 1;
136 0 next; # and go handle the next FH
137 }
138 0 ▶ if($fh == $outfh) { # if we read from the outfh
Branch: 2 branches
BranchTrueFalseTotal
if ($fh == $outfh) { } 0 0 0
elsif ($fh == $errfh) { } 0 0 0
139 0 $output_std .= $line;
140 0 $output_all .= $line;
141 } elsif($fh == $errfh) { # do the same for errfh
142 0 $output_error .= $line;
143 0 $output_all .= $line;
144 } else { # we read from something else?!?!
145 0 croak "Shouldn't be here!\n";
146 }
147 }
148 }
149 0 ▶ if($timeout == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timeout == 0) 0 0 0
150 # No timeout, so we lower the counter by one to keep it forever under 0.
151 # Only the closing of the output handles ($out_handles_open == 0) can break the loop
152 0 $slept_secs--;
153 }
154 0 ▶ if($print_progress_indicator == $TRUE && $out_handles_open > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($print_progress_indicator == $TRUE and $out_handles_open > 0) 0 0 0
Condition0%0/3
$print_progress_indicator == $TRUE and $out_handles_open > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$print_progress_indicator == $TRUE and $out_handles_open > 0
$print_progress_indicator == $TRUE $out_handles_open > 0
Truth table0%0/3
A and B
A= $print_progress_indicator == $TRUEB= $out_handles_open > 0
ABresult
0X0
100
111
155 0 print STDOUT $progress_indicator_char;
156 }
157 0 ▶ if($execute_every_second && $out_handles_open > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($execute_every_second and $out_handles_open > 0) 0 0 0
Condition0%0/3
$execute_every_second and $out_handles_open > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$execute_every_second and $out_handles_open > 0
$execute_every_second $out_handles_open > 0
Truth table0%0/3
A and B
A= $execute_every_secondB= $out_handles_open > 0
ABresult
0X0
100
111
158 0 &$execute_every_second($slept_secs);
159 }
160 }
161 # It is safe to kill in all circumstances.
162 # Anyway, we must reap the child process.
163 0 my $killed = kill $SIGKILL, $pid;
164 0 my $command_return_status = $CHILD_ERROR >> 8;
165 0 ▶ if($out_handles_open > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($out_handles_open > 0) 0 0 0
166 0 $output_error .= 'Timeout';
167 0 $output_all .= 'Timeout';
168 0 $command_exit_status = $EXIT_STATUS_TIMEOUT;
169 }
170 }
171 else {
172 # open3 failed!
173 7 $output_error .= 'Could not run command';
174 7 $output_all .= 'Could not run command';
175 7 $command_exit_status = $EXIT_STATUS_FAILED;
176 }
177 }
178 else {
179 # Parameter check
180 0 $output_error .= 'Invalid parameters';
181 0 $output_all .= 'Invalid parameters';
182 0 $command_exit_status = $EXIT_STATUS_FAILED;
183 }
184  
185 7 return ($command_exit_status, $output_std, $output_error, $output_all);
186 }
187  
188 1 1;
189  
190 __END__
« blib/lib/IPC/RunExternal.pm ↑ Summary