blib/lib/IPC/Run/IO.pm

filter: Statementstmt 97.6% 209 / 214 Branchbran 72.3% 94 / 130 Conditioncond 53.4% 23 / 43 MC/DCmcdc 38.4% 10 / 26 Subroutinesub 100% 30 / 30 Podpod 100% 12 / 12 totaltotal 83.0% 378 / 455 CC 81 SCAR 18.8
CC
81
Cov
84%
BEGIN
15.5
_do_open
11.0
_new_internal
10.1
CRAP 6.5
« blib/lib/IPC/Run/Debug.pm ↑ Summary blib/lib/IPC/Run/Timer.pm »
1 package IPC::Run::IO;
2  
3 =head1 NAME
4  
5 IPC::Run::IO -- I/O channels for IPC::Run.
6  
7 =head1 SYNOPSIS
8  
9 B<NOT IMPLEMENTED YET ON Win32! Win32 does not allow select() on
10 normal file descriptors; IPC::RUN::IO needs to use IPC::Run::Win32Helper
11 to do this.>
12  
13 use IPC::Run qw( io );
14  
15 ## The sense of '>' and '<' is opposite of perl's open(),
16 ## but agrees with IPC::Run.
17 $io = io( "filename", '>', \$recv );
18 $io = io( "filename", 'r', \$recv );
19  
20 ## Append to $recv:
21 $io = io( "filename", '>>', \$recv );
22 $io = io( "filename", 'ra', \$recv );
23  
24 $io = io( "filename", '<', \$send );
25 $io = io( "filename", 'w', \$send );
26  
27 $io = io( "filename", '<<', \$send );
28 $io = io( "filename", 'wa', \$send );
29  
30 ## Handles / IO objects that the caller opens:
31 $io = io( \*HANDLE, '<', \$send );
32  
33 $f = IO::Handle->new( ... ); # Any subclass of IO::Handle
34 $io = io( $f, '<', \$send );
35  
36 require IPC::Run::IO;
37 $io = IPC::Run::IO->new( ... );
38  
39 ## Then run(), harness(), or start():
40 run $io, ...;
41  
42 ## You can, of course, use io() or IPC::Run::IO->new() as an
43 ## argument to run(), harness, or start():
44 run io( ... );
45  
46 =head1 DESCRIPTION
47  
48 This class and module allows filehandles and filenames to be harnessed for
49 I/O when used IPC::Run, independent of anything else IPC::Run is doing
50 (except that errors & exceptions can affect all things that IPC::Run is
51 doing).
52  
53 =head1 SUBCLASSING
54  
55 INCOMPATIBLE CHANGE: due to the awkwardness introduced in ripping pseudohashes
56 out of Perl, this class I<no longer> uses the fields pragma.
57  
58 =cut
59  
60 ## This class is also used internally by IPC::Run in a very intimate way,
61 ## since this is a partial factoring of code from IPC::Run plus some code
62 ## needed to do standalone channels. This factoring process will continue
63 ## at some point. Don't know how far how fast.
64  
65 228 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 228 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 228 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 228 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 228 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
70  
71 228 ▶use IPC::Run::Debug;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 228 ▶use IPC::Run qw( Win32_MODE );
Subroutine BEGIN: called CC=1 SCAR=0.0
73  
74 228 ▶use vars qw{$VERSION};
Subroutine BEGIN: called CC=1 SCAR=0.0
75  
76 BEGIN {
77 228 ▶ $VERSION = '20260401.0';
Subroutine BEGIN: called CC=5 SCAR=27.4
78 228 ▶ if (Win32_MODE) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Win32_MODE()) 0 228 228
79 0 ▶ eval "use IPC::Run::Win32Helper; require IPC::Run::Win32IO; 1"
Branch: 1 branch
BranchTrueFalseTotal
eval "use IPC::Run::Win32Helper; require IPC::Run::Win32IO; 1" or $@ and die or die "$!" 0 0 0
Condition0%0/5
eval "use IPC::Run::Win32Helper; require IPC::Run::Win32IO; 1" or $@ and die
l!l&&r!l&&!r
000
$@ and die
!ll
00
MC/DC0%0/2
eval "use IPC::Run::Win32Helper; require IPC::Run::Win32IO; 1" or $@ and die
eval "use IPC::Run::Win32Helper; require IPC::Run::Win32IO; 1" $@
Truth table0%0/3
A or B
A= eval "use IPC::Run::Win32Helper; require IPC::Run::Win32IO; 1"B= $@
ABresult
1X1
011
000
80 or ( $@ && die )
81 or die "$!";
82 }
83 }
84  
85 sub _empty($);
86 228 *_empty = \&IPC::Run::_empty;
87  
88 =head1 SUBROUTINES
89  
90 =over 4
91  
92 =item new
93  
94 I think it takes >> or << along with some other data.
95  
96 TODO: Needs more thorough documentation. Patches welcome.
97  
98 =cut
99  
100 sub new {
101 9 ▶ my $class = shift;
Subroutine new: called CC=6 SCAR=18.9
Pod documented
102 9 ▶ $class = ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
090
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
103  
104 9 my ( $external, $type, $internal ) = ( shift, shift, pop );
105  
106 9 ▶ croak "$class: '$type' is not a valid I/O operator"
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ /^(?:<<?|>>?)$/ 1 8 9
107 unless $type =~ /^(?:<<?|>>?)$/;
108  
109 8 my IPC::Run::IO $self = $class->_new_internal( $type, undef, undef, $internal, undef, @_ );
110  
111 8 ▶ if ( !ref $external ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $external) { } 7 1 8
elsif (ref $external eq 'GLOB' or UNIVERSAL::isa($external, 'IO::Handle')) { } 1 0 1
112 7 $self->{FILENAME} = $external;
113 }
114 ▶ elsif ( ref $external eq 'GLOB' || UNIVERSAL::isa( $external, 'IO::Handle' ) ) {
Condition33%1/3
ref $external eq "GLOB" or UNIVERSAL::isa($external, "IO::Handle")
l!l&&r!l&&!r
100
MC/DC0%0/2
ref $external eq "GLOB" or UNIVERSAL::isa($external, "IO::Handle")
ref $external eq "GLOB" UNIVERSAL::isa($external, "IO::Handle")
Truth table33%1/3
A or B
A= ref $external eq "GLOB"B= UNIVERSAL::isa($external, "IO::Handle")
ABresult
1X1
011
000
115 1 $self->{HANDLE} = $external;
116 1 $self->{DONT_CLOSE} = 1;
117 }
118 else {
119 0 croak "$class: cannot accept " . ref($external) . " to do I/O with";
120 }
121  
122 8 return $self;
123 }
124  
125 ## IPC::Run uses this ctor, since it preparses things and needs more
126 ## smarts.
127 sub _new_internal {
128 4324 ▶ my $class = shift;
Subroutine _new_internal: called CC=10 SCAR=23.2
129 4324 ▶ $class = ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
043240
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
130  
131 4324 ▶ $class = "IPC::Run::Win32IO"
Branch: 1 branch
BranchTrueFalseTotal
if &Win32_MODE() and $class eq "IPC::Run::IO" 0 4324 4324
Condition33%1/3
&Win32_MODE() and $class eq "IPC::Run::IO"
!ll&&!rl&&r
432400
MC/DC0%0/2
&Win32_MODE() and $class eq "IPC::Run::IO"
&Win32_MODE() $class eq "IPC::Run::IO"
Truth table33%1/3
A and B
A= &Win32_MODE()B= $class eq "IPC::Run::IO"
ABresult
0X0
100
111
132 if Win32_MODE && $class eq "IPC::Run::IO";
133  
134 4324 my IPC::Run::IO $self;
135 4324 $self = bless {}, $class;
136  
137 4324 my ( $type, $kfd, $pty_id, $internal, $binmode, @filters ) = @_;
138  
139 # Older perls (<=5.00503, at least) don't do list assign to
140 # pseudo-hashes well.
141 4324 $self->{TYPE} = $type;
142 4324 $self->{KFD} = $kfd;
143 4324 $self->{PTY_ID} = $pty_id;
144 4324 $self->binmode($binmode);
145 4324 $self->{FILTERS} = [@filters];
146  
147 ## Add an adapter to the end of the filter chain (which is usually just the
148 ## read/writer sub pushed by IPC::Run) to the DEST or SOURCE, if need be.
149 4324 ▶ if ( $self->op =~ />/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->op =~ />/) { } 2820 1504 4324
150 2820 ▶ croak "'$_' missing a destination" if _empty $internal;
Branch: 1 branch
BranchTrueFalseTotal
if &_empty($internal) 0 2820 2820
151 2820 $self->{DEST} = $internal;
152 2820 ▶ if ( UNIVERSAL::isa( $self->{DEST}, 'CODE' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($self->{"DEST"}, "CODE")) 116 2704 2820
153 ## Put a filter on the end of the filter chain to pass the
154 ## output on to the CODE ref. For SCALAR refs, the last
155 ## filter in the chain writes directly to the scalar itself. See
156 ## _init_filters(). For CODE refs, however, we need to adapt from
157 ## the SCALAR to calling the CODE.
158 unshift(
159 116 @{ $self->{FILTERS} },
160 sub {
161 212 ▶ my ($in_ref) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.0
162  
163 212 ▶ return IPC::Run::input_avail() && do {
Condition66%2/3
&IPC::Run::input_avail() && do { $self->{'DEST'}->($$in_ref); $$in_ref = ''; 1 }
!ll&&!rl&&r
1080104
MC/DC50%1/2
&IPC::Run::input_avail() && do { $self->{'DEST'}->($$in_ref); $$in_ref = ''; 1 }
&IPC::Run::input_avail() do { $self->{'DEST'}->($$in_ref); $$in_ref = ''; 1 }
Truth table66%2/3
A && B
A= &IPC::Run::input_avail()B= do { $self->{'DEST'}->($$in_ref); $$in_ref = ''; 1 }
ABresult
0X0
100
111
164 104 $self->{DEST}->($$in_ref);
165 104 $$in_ref = '';
166 104 1;
167 }
168 }
169 116 );
170 }
171 }
172 else {
173 1504 ▶ croak "'$_' missing a source" if _empty $internal;
Branch: 1 branch
BranchTrueFalseTotal
if &_empty($internal) 0 1504 1504
174 1504 $self->{SOURCE} = $internal;
175 1504 ▶ if ( UNIVERSAL::isa( $internal, 'CODE' ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($internal, 'CODE')) { } 71 1433 1504
elsif (UNIVERSAL::isa($internal, 'SCALAR')) { } 1045 388 1433
176 push(
177 71 @{ $self->{FILTERS} },
178 sub {
179 88 ▶ my ( $in_ref, $out_ref ) = @_;
Subroutine __ANON__: called CC=5 SCAR=16.2
180 88 ▶ return 0 if length $$out_ref;
Branch: 1 branch
BranchTrueFalseTotal
if length $$out_ref 0 88 88
181  
182 return undef
183 88 ▶ if $self->{SOURCE_EMPTY};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"SOURCE_EMPTY"} 0 88 88
184  
185 88 my $in = $internal->();
186 88 ▶ unless ( defined $in ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $in) 49 39 88
187 49 $self->{SOURCE_EMPTY} = 1;
188 49 return undef;
189 }
190 39 ▶ return 0 unless length $in;
Branch: 1 branch
BranchTrueFalseTotal
unless length $in 7 32 39
191 32 $$out_ref = $in;
192  
193 32 return 1;
194 }
195 71 );
196 }
197 elsif ( UNIVERSAL::isa( $internal, 'SCALAR' ) ) {
198 push(
199 1045 @{ $self->{FILTERS} },
200 sub {
201 3148 ▶ my ( $in_ref, $out_ref ) = @_;
Subroutine __ANON__: called CC=8 SCAR=20.8
202 3148 ▶ return 0 if length $$out_ref;
Branch: 1 branch
BranchTrueFalseTotal
if length $$out_ref 0 3148 3148
203  
204 ## pump() clears auto_close_ins, finish() sets it.
205 return $self->{HARNESS}->{auto_close_ins} ? undef : 0
206 3148 ▶ if IPC::Run::_empty ${ $self->{SOURCE} }
Branch: 1 branch
BranchTrueFalseTotal
$self->{'HARNESS'}{'auto_close_ins'} ? : 648 1662 2310
207 3148 ▶ || $self->{SOURCE_EMPTY};
Branch: 1 branch
BranchTrueFalseTotal
if &IPC::Run::_empty(${$$self{"SOURCE"};}) or $self->{"SOURCE_EMPTY"} 2310 838 3148
Condition100%3/3
&IPC::Run::_empty(${$$self{"SOURCE"};}) or $self->{"SOURCE_EMPTY"}
l!l&&r!l&&!r
1975335838
MC/DC100%2/2
&IPC::Run::_empty(${$$self{"SOURCE"};}) or $self->{"SOURCE_EMPTY"}
&IPC::Run::_empty(${$$self{"SOURCE"};}) $self->{"SOURCE_EMPTY"}
Truth table100%3/3
A or B
A= &IPC::Run::_empty(${$$self{"SOURCE"};})B= $self->{"SOURCE_EMPTY"}
ABresult
1X1
011
000
208  
209 ## When clear_ins is set (start/pump mode), limit the chunk
210 ## copied per filter invocation. Without this limit, all of
211 ## $$internal is moved to the internal pipe buffer in one
212 ## shot. If the caller keeps appending to $$internal faster
213 ## than the child can consume data, the intermediate buffer
214 ## grows without bound -- exponential memory growth.
215 ## See https://github.com/cpan-authors/IPC-Run/issues/154
216 838 my $max_chunk = 65536;
217 838 ▶ if ( $self->{HARNESS}->{clear_ins}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'HARNESS'}{'clear_ins'} and length $$internal > $max_chunk) { } 14 824 838
Condition100%3/3
$self->{"HARNESS"}{"clear_ins"} and length $$internal > $max_chunk
!ll&&!rl&&r
56625814
MC/DC100%2/2
$self->{"HARNESS"}{"clear_ins"} and length $$internal > $max_chunk
$self->{"HARNESS"}{"clear_ins"} length $$internal > $max_chunk
Truth table100%3/3
A and B
A= $self->{"HARNESS"}{"clear_ins"}B= length $$internal > $max_chunk
ABresult
0X0
100
111
218 && length($$internal) > $max_chunk )
219 {
220 14 $$out_ref = substr( $$internal, 0, $max_chunk );
221 14 eval { substr( $$internal, 0, $max_chunk, '' ) };
222 ## SOURCE_EMPTY intentionally not set here: more data
223 ## remains in $$internal and will be picked up on the
224 ## next filter invocation once $$out_ref has drained.
225 }
226 else {
227 824 $$out_ref = $$internal;
228 258 eval { $$internal = '' }
229 824 ▶ if $self->{HARNESS}->{clear_ins};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"HARNESS"}{"clear_ins"} 258 566 824
230  
231 824 $self->{SOURCE_EMPTY} = $self->{HARNESS}->{auto_close_ins};
232 }
233  
234 838 return 1;
235 }
236 1045 );
237 }
238 }
239  
240 4324 return $self;
241 }
242  
243 =item filename
244  
245 Gets/sets the filename. Returns the value after the name change, if
246 any.
247  
248 =cut
249  
250 sub filename {
251 4 ▶ my IPC::Run::IO $self = shift;
Subroutine filename: called CC=2 SCAR=7.1
Pod documented
252 4 ▶ $self->{FILENAME} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 4 4
253 4 return $self->{FILENAME};
254 }
255  
256 =item init
257  
258 Does initialization required before this can be run. This includes open()ing
259 the file, if necessary, and clearing the destination scalar if necessary.
260  
261 =cut
262  
263 sub init {
264 2 ▶ my IPC::Run::IO $self = shift;
Subroutine init: called CC=7 SCAR=19.7
Pod documented
265  
266 2 $self->{SOURCE_EMPTY} = 0;
267 1 ${ $self->{DEST} } = ''
268 2 ▶ if $self->mode =~ /r/ && ref $self->{DEST} eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if $self->mode =~ /r/ and ref $self->{"DEST"} eq "SCALAR" 1 1 2
Condition66%2/3
$self->mode =~ /r/ and ref $self->{"DEST"} eq "SCALAR"
!ll&&!rl&&r
101
MC/DC50%1/2
$self->mode =~ /r/ and ref $self->{"DEST"} eq "SCALAR"
$self->mode =~ /r/ ref $self->{"DEST"} eq "SCALAR"
Truth table66%2/3
A and B
A= $self->mode =~ /r/B= ref $self->{"DEST"} eq "SCALAR"
ABresult
0X0
100
111
269  
270 2 ▶ $self->open if defined $self->filename;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->filename 2 0 2
271 2 $self->{FD} = $self->fileno;
272  
273 2 ▶ if ( !$self->{FILTERS} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'FILTERS'}) { } 0 2 2
274 0 $self->{FBUFS} = undef;
275 }
276 else {
277 2 @{ $self->{FBUFS} } = map {
278 3 my $s = "";
279 3 \$s;
280 2 } ( @{ $self->{FILTERS} }, '' );
281  
282 $self->{FBUFS}->[0] = $self->{DEST}
283 2 ▶ if $self->{DEST} && ref $self->{DEST} eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEST"} and ref $self->{"DEST"} eq "SCALAR" 1 1 2
Condition66%2/3
$self->{"DEST"} and ref $self->{"DEST"} eq "SCALAR"
!ll&&!rl&&r
101
MC/DC50%1/2
$self->{"DEST"} and ref $self->{"DEST"} eq "SCALAR"
$self->{"DEST"} ref $self->{"DEST"} eq "SCALAR"
Truth table66%2/3
A and B
A= $self->{"DEST"}B= ref $self->{"DEST"} eq "SCALAR"
ABresult
0X0
100
111
284 2 push @{ $self->{FBUFS} }, $self->{SOURCE};
285 }
286  
287 2 return undef;
288 }
289  
290 =item open
291  
292 If a filename was passed in, opens it. Determines if the handle is open
293 via fileno(). Throws an exception on error.
294  
295 =cut
296  
297 228 my %open_flags = (
298 '>' => O_RDONLY,
299 '>>' => O_RDONLY,
300 '<' => O_WRONLY | O_CREAT | O_TRUNC,
301 '<<' => O_WRONLY | O_CREAT | O_APPEND,
302 );
303  
304 sub open {
305 2 ▶ my IPC::Run::IO $self = shift;
Subroutine open: called CC=5 SCAR=17.2
Pod documented
306  
307 croak "IPC::Run::IO: Can't open() a file with no name"
308 2 ▶ unless defined $self->{FILENAME};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"FILENAME"} 0 2 2
309 2 ▶ $self->{HANDLE} = gensym unless $self->{HANDLE};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"HANDLE"} 2 0 2
310  
311 2 ▶ _debug "opening '", $self->filename, "' mode '", $self->mode, "'"
Branch: 1 branch
BranchTrueFalseTotal
if &_debugging_data() 0 2 2
312 if _debugging_data;
313 sysopen(
314 $self->{HANDLE},
315 $self->filename,
316 2 ▶ $open_flags{ $self->op },
Branch: 1 branch
BranchTrueFalseTotal
sysopen $self->{"HANDLE"}, $self->filename, $open_flags{$self->op} or croak(qq[IPC::Run::IO: $! opening '$self->{"FILENAME"}', mode '] . $self->mode . q[']) 0 2 2
317 ) or croak "IPC::Run::IO: $! opening '$self->{FILENAME}', mode '" . $self->mode . "'";
318  
319 2 return undef;
320 }
321  
322 =item open_pipe
323  
324 If this is a redirection IO object, this opens the pipe in a platform
325 independent manner.
326  
327 =cut
328  
329 sub _do_open {
330 3709 ▶ my $self = shift;
Subroutine _do_open: called CC=11 SCAR=24.0
331 3709 my ( $child_debug_fd, $parent_handle ) = @_;
332  
333 3709 ▶ if ( $self->dir eq "<" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dir eq '<') { } 1196 2513 3709
334 1196 ▶ if ( $self->{TYPE} eq '<blocking_pipe' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'TYPE'} eq '<blocking_pipe') { } 63 1133 1196
335 63 ( $self->{TFD}, $self->{FD} ) = IPC::Run::_pipe;
336 }
337 else {
338 1133 ( $self->{TFD}, $self->{FD} ) = IPC::Run::_pipe_nb;
339 }
340 1196 ▶ if ($parent_handle) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent_handle) 98 1098 1196
341 98 ▶ my $fh = ref $parent_handle eq 'SCALAR' ? do { require Symbol; Symbol::gensym() } : $parent_handle;
Branch: 1 branch
BranchTrueFalseTotal
ref $parent_handle eq 'SCALAR' ? : 2 96 98
342 98 ▶ CORE::open $fh, ">&=$self->{FD}"
Branch: 1 branch
BranchTrueFalseTotal
CORE::open $fh, qq[>&=$self->{"FD"}] or croak("$! duping write end of pipe for caller") 0 98 98
343 or croak "$! duping write end of pipe for caller";
344 98 ▶ $$parent_handle = $fh if ref $parent_handle eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $parent_handle eq "SCALAR" 2 96 98
345 }
346 }
347 else {
348 2513 ( $self->{FD}, $self->{TFD} ) = IPC::Run::_pipe;
349 2513 ▶ if ($parent_handle) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent_handle) 61 2452 2513
350 61 ▶ my $fh = ref $parent_handle eq 'SCALAR' ? do { require Symbol; Symbol::gensym() } : $parent_handle;
Branch: 1 branch
BranchTrueFalseTotal
ref $parent_handle eq 'SCALAR' ? : 3 58 61
351 61 ▶ CORE::open $fh, "<&=$self->{FD}"
Branch: 1 branch
BranchTrueFalseTotal
CORE::open $fh, qq[<&=$self->{"FD"}] or croak("$! duping read end of pipe for caller") 0 61 61
352 or croak "$! duping read end of pipe for caller";
353 61 ▶ $$parent_handle = $fh if ref $parent_handle eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $parent_handle eq "SCALAR" 3 58 61
354 }
355 }
356 }
357  
358 sub open_pipe {
359 3709 ▶ my IPC::Run::IO $self = shift;
Subroutine open_pipe: called CC=3 SCAR=11.0
Pod documented
360  
361 ## Hmmm, Maybe allow named pipes one day. But until then...
362 croak "IPC::Run::IO: Can't pipe() when a file name has been set"
363 3709 ▶ if defined $self->{FILENAME};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"FILENAME"} 0 3709 3709
364  
365 3709 $self->_do_open(@_);
366  
367 ## return ( child_fd, parent_fd )
368 return $self->dir eq "<"
369 ? ( $self->{TFD}, $self->{FD} )
370 3709 ▶ : ( $self->{FD}, $self->{TFD} );
Branch: 1 branch
BranchTrueFalseTotal
$self->dir eq '<' ? : 1196 2513 3709
371 }
372  
373 sub _cleanup { ## Called from Run.pm's _cleanup
374 3875 ▶ my $self = shift;
Subroutine _cleanup: called CC=1 SCAR=0.0
375 3875 undef $self->{FAKE_PIPE};
376 }
377  
378 =item close
379  
380 Closes the handle. Throws an exception on failure.
381  
382  
383 =cut
384  
385 sub close {
386 3227 ▶ my IPC::Run::IO $self = shift;
Subroutine close: called CC=4 SCAR=14.5
Pod documented
387  
388 3227 ▶ if ( defined $self->{HANDLE} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'HANDLE'}) { } 2 3225 3227
389 close $self->{HANDLE}
390 or croak(
391 "IPC::Run::IO: $! closing "
392 . (
393 defined $self->{FILENAME}
394 2 ▶ ? "'$self->{FILENAME}'"
Branch: 2 branches
BranchTrueFalseTotal
CORE::close $self->{"HANDLE"} or croak("IPC::Run::IO: $! closing " . (defined $self->{"FILENAME"} ? qq['$self->{"FILENAME"}'] : "handle")) 0 2 2
defined $self->{'FILENAME'} ? : 0 0 0
395 : "handle"
396 )
397 );
398 }
399 else {
400 3225 IPC::Run::_close( $self->{FD} );
401 }
402  
403 3227 $self->{FD} = undef;
404  
405 3227 return undef;
406 }
407  
408 =item fileno
409  
410 Returns the fileno of the handle. Throws an exception on failure.
411  
412  
413 =cut
414  
415 sub fileno {
416 4 ▶ my IPC::Run::IO $self = shift;
Subroutine fileno: called CC=3 SCAR=12.5
Pod documented
417  
418 4 my $fd = fileno $self->{HANDLE};
419 croak(
420 "IPC::Run::IO: $! "
421 . (
422 defined $self->{FILENAME}
423 4 ▶ ? "'$self->{FILENAME}'"
Branch: 2 branches
BranchTrueFalseTotal
unless defined $fd 0 4 4
defined $self->{'FILENAME'} ? : 0 0 0
424 : "handle"
425 )
426 ) unless defined $fd;
427  
428 4 return $fd;
429 }
430  
431 =item mode
432  
433 Returns the operator in terms of 'r', 'w', and 'a'. There is a state
434 'ra', unlike Perl's open(), which indicates that data read from the
435 handle or file will be appended to the output if the output is a scalar.
436 This is only meaningful if the output is a scalar, it has no effect if
437 the output is a subroutine.
438  
439 The redirection operators can be a little confusing, so here's a reference
440 table:
441  
442 > r Read from handle in to process
443 < w Write from process out to handle
444 >> ra Read from handle in to process, appending it to existing
445 data if the destination is a scalar.
446 << wa Write from process out to handle, appending to existing
447 data if IPC::Run::IO opened a named file.
448  
449 =cut
450  
451 sub mode {
452 10 ▶ my IPC::Run::IO $self = shift;
Subroutine mode: called CC=4 SCAR=13.9
Pod documented
453  
454 10 ▶ croak "IPC::Run::IO: unexpected arguments for mode(): @_" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 10 10
455  
456 ## TODO: Optimize this
457 10 ▶ return ( $self->{TYPE} =~ /</ ? 'w' : 'r' ) . ( $self->{TYPE} =~ /<<|>>/ ? 'a' : '' );
Branch: 2 branches
BranchTrueFalseTotal
$self->{'TYPE'} =~ /</ ? : 5 5 10
$self->{'TYPE'} =~ /<<|>>/ ? : 2 8 10
458 }
459  
460 =item op
461  
462 Returns the operation: '<', '>', '<<', '>>'. See L</mode> if you want
463 to spell these 'r', 'w', etc.
464  
465 =cut
466  
467 sub op {
468 4326 ▶ my IPC::Run::IO $self = shift;
Subroutine op: called CC=2 SCAR=7.1
Pod documented
469  
470 4326 ▶ croak "IPC::Run::IO: unexpected arguments for op(): @_" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 4326 4326
471  
472 4326 return $self->{TYPE};
473 }
474  
475 =item binmode
476  
477 Sets/gets whether this pipe is in binmode or not. No effect off of Win32
478 OSs, of course, and on Win32, no effect after the harness is start()ed.
479  
480 =cut
481  
482 sub binmode {
483 4324 ▶ my IPC::Run::IO $self = shift;
Subroutine binmode: called CC=2 SCAR=7.1
Pod documented
484  
485 4324 ▶ $self->{BINMODE} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 4324 0 4324
486  
487 4324 return $self->{BINMODE};
488 }
489  
490 =item dir
491  
492 Returns the first character of $self->op. This is either "<" or ">".
493  
494 =cut
495  
496 sub dir {
497 23576 ▶ my IPC::Run::IO $self = shift;
Subroutine dir: called CC=2 SCAR=7.1
Pod documented
498  
499 23576 ▶ croak "IPC::Run::IO: unexpected arguments for dir(): @_" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 23576 23576
500  
501 23576 return substr $self->{TYPE}, 0, 1;
502 }
503  
504 ##
505 ## Filter Scaffolding
506 ##
507 #my $filter_op ; ## The op running a filter chain right now
508 #my $filter_num; ## Which filter is being run right now.
509  
510 use vars (
511 228 '$filter_op', ## The op running a filter chain right now
512 '$filter_num' ## Which filter is being run right now.
513 228 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
514  
515 sub _init_filters {
516 4465 ▶ my IPC::Run::IO $self = shift;
Subroutine _init_filters: called CC=6 SCAR=17.9
517  
518 4465 ▶ confess "\$self not an IPC::Run::IO" unless UNIVERSAL::isa( $self, "IPC::Run::IO" );
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($self, "IPC::Run::IO") 0 4465 4465
519 4465 $self->{FBUFS} = [];
520  
521 $self->{FBUFS}->[0] = $self->{DEST}
522 4465 ▶ if $self->{DEST} && ref $self->{DEST} eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEST"} and ref $self->{"DEST"} eq "SCALAR" 2363 2102 4465
Condition100%3/3
$self->{"DEST"} and ref $self->{"DEST"} eq "SCALAR"
!ll&&!rl&&r
16744282363
MC/DC100%2/2
$self->{"DEST"} and ref $self->{"DEST"} eq "SCALAR"
$self->{"DEST"} ref $self->{"DEST"} eq "SCALAR"
Truth table100%3/3
A and B
A= $self->{"DEST"}B= ref $self->{"DEST"} eq "SCALAR"
ABresult
0X0
100
111
523  
524 4465 ▶ return unless $self->{FILTERS} && @{ $self->{FILTERS} };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"FILTERS"} and @{$self->{"FILTERS"};} 2882 1583 4465
Condition66%2/3
$self->{"FILTERS"} and @{$self->{"FILTERS"};}
!ll&&!rl&&r
028821583
MC/DC50%1/2
$self->{"FILTERS"} and @{$self->{"FILTERS"};}
$self->{"FILTERS"} @{$self->{"FILTERS"};}
Truth table66%2/3
A and B
A= $self->{"FILTERS"}B= @{$self->{"FILTERS"};}
ABresult
0X0
100
111
525  
526 1583 push @{ $self->{FBUFS} }, map {
527 3869 my $s = "";
528 3869 \$s;
529 1583 } ( @{ $self->{FILTERS} }, '' );
530  
531 1583 push @{ $self->{FBUFS} }, $self->{SOURCE};
532 }
533  
534 =item poll
535  
536 TODO: Needs confirmation that this is correct. Was previously undocumented.
537  
538 I believe this is polling the IO for new input and then returns undef if there will never be any more input, 0 if there is none now, but there might be in the future, and TRUE if more input was gotten.
539  
540 =cut
541  
542 sub poll {
543 16204 ▶ my IPC::Run::IO $self = shift;
Subroutine poll: called CC=8 SCAR=20.9
Pod documented
544 16204 my ($harness) = @_;
545  
546 16204 ▶ if ( defined $self->{FD} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"FD"}) 16158 46 16204
547 16158 my $d = $self->dir;
548 16158 ▶ if ( $d eq "<" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($d eq '<') { } 2178 13980 16158
elsif ($d eq '>') { } 13980 0 13980
549 2178 ▶ if ( vec $harness->{WOUT}, $self->{FD}, 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (vec $harness->{"WOUT"}, $self->{"FD"}, 1) 1528 650 2178
550 1528 ▶ _debug_desc_fd( "filtering data to", $self )
Branch: 1 branch
BranchTrueFalseTotal
if &_debugging_details() 0 1528 1528
551 if _debugging_details;
552 1528 return $self->_do_filters($harness);
553 }
554 }
555 elsif ( $d eq ">" ) {
556 13980 ▶ if ( vec $harness->{ROUT}, $self->{FD}, 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (vec $harness->{"ROUT"}, $self->{"FD"}, 1) 4207 9773 13980
557 4207 ▶ _debug_desc_fd( "filtering data from", $self )
Branch: 1 branch
BranchTrueFalseTotal
if &_debugging_details() 0 4207 4207
558 if _debugging_details;
559 4207 return $self->_do_filters($harness);
560 }
561 }
562 }
563 10469 return 0;
564 }
565  
566 sub _do_filters {
567 8689 ▶ my IPC::Run::IO $self = shift;
Subroutine _do_filters: called CC=5 SCAR=16.9
568  
569 8689 ( $self->{HARNESS} ) = @_;
570  
571 8689 my ( $saved_op, $saved_num ) = ( $IPC::Run::filter_op, $IPC::Run::filter_num );
572 8689 $IPC::Run::filter_op = $self;
573 8689 $IPC::Run::filter_num = -1;
574 8689 my $redos = 0;
575 8689 my $r;
576 {
577 8689 $@ = '';
578 8689 $r = eval { IPC::Run::get_more_input(); };
579  
580 # Detect Resource temporarily unavailable and re-try 200 times (2 seconds), assuming select behaves (which it doesn't always? need ref)
581 8689 ▶ if ( ( $@ || '' ) =~ $IPC::Run::_EAGAIN && $redos++ < 200 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($@ || "") =~ /$IPC::Run::_EAGAIN/ and $redos++ < 200) 0 8689 8689
Condition40%2/5
($@ || "") =~ /$IPC::Run::_EAGAIN/ and $redos++ < 200
!ll&&!rl&&r
868900
$@ || ''
l!l
08689
MC/DC0%0/2
($@ || "") =~ /$IPC::Run::_EAGAIN/ and $redos++ < 200
$@ $redos++ < 200
Truth table33%1/3
A and B
A= $@B= $redos++ < 200
ABresult
0X0
100
111
582 0 select( undef, undef, undef, 0.01 );
583 0 redo;
584 }
585 }
586 8689 ( $IPC::Run::filter_op, $IPC::Run::filter_num ) = ( $saved_op, $saved_num );
587 8689 $self->{HARNESS} = undef;
588 8689 ▶ die "ack ", $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 8689 8689
589 8689 return $r;
590 }
591  
592 =back
593  
594 =head1 AUTHOR
595  
596 Barrie Slaymaker <barries@slaysys.com>
597  
598 =head1 TODO
599  
600 Implement bidirectionality.
601  
602 =cut
603  
604 228 1;
« blib/lib/IPC/Run/Debug.pm ↑ Summary blib/lib/IPC/Run/Timer.pm »