blib/lib/IPC/Run/IO.pm

filter: Statementstmt 97.6% 210 / 215 Branchbran 72.3% 94 / 130 Conditioncond 57.1% 28 / 49 MC/DCmcdc 41.3% 12 / 29 Subroutinesub 100% 30 / 30 Podpod 100% 12 / 12 totaltotal 83.0% 386 / 465 CC 83 SCAR 18.9
CC
83
Cov
84%
BEGIN
15.5
_do_open
11.0
_new_internal
10.1
CRAP 6.6
« 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 231 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 231 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 231 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 231 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 231 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
70  
71 231 ▶use IPC::Run::Debug;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 231 ▶use IPC::Run qw( Win32_MODE );
Subroutine BEGIN: called CC=1 SCAR=0.0
73  
74 231 ▶use vars qw{$VERSION};
Subroutine BEGIN: called CC=1 SCAR=0.0
75  
76 231 ▶BEGIN {
Subroutine BEGIN: called CC=5 SCAR=27.4
77 231 $VERSION = '20260402.0';
78 231 ▶ if (Win32_MODE) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Win32_MODE()) 0 231 231
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 231 *_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 9 ▶sub new {
Subroutine new: called CC=6 SCAR=18.9
Pod documented
101 9 my $class = shift;
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 4334 ▶sub _new_internal {
Subroutine _new_internal: called CC=10 SCAR=23.2
128 4334 my $class = shift;
129 4334 ▶ $class = ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
043340
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 4334 ▶ $class = "IPC::Run::Win32IO"
Branch: 1 branch
BranchTrueFalseTotal
if &Win32_MODE() and $class eq "IPC::Run::IO" 0 4334 4334
Condition33%1/3
&Win32_MODE() and $class eq "IPC::Run::IO"
!ll&&!rl&&r
433400
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 4334 my IPC::Run::IO $self;
135 4334 $self = bless {}, $class;
136  
137 4334 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 4334 $self->{TYPE} = $type;
142 4334 $self->{KFD} = $kfd;
143 4334 $self->{PTY_ID} = $pty_id;
144 4334 $self->binmode($binmode);
145 4334 $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 4334 ▶ if ( $self->op =~ />/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->op =~ />/) { } 2825 1509 4334
150 2825 ▶ croak "'$_' missing a destination" if _empty $internal;
Branch: 1 branch
BranchTrueFalseTotal
if &_empty($internal) 0 2825 2825
151 2825 $self->{DEST} = $internal;
152 2825 ▶ if ( UNIVERSAL::isa( $self->{DEST}, 'CODE' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($self->{"DEST"}, "CODE")) 116 2709 2825
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 212 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
161 212 my ($in_ref) = @_;
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 1509 ▶ croak "'$_' missing a source" if _empty $internal;
Branch: 1 branch
BranchTrueFalseTotal
if &_empty($internal) 0 1509 1509
174 1509 $self->{SOURCE} = $internal;
175 1509 ▶ if ( UNIVERSAL::isa( $internal, 'CODE' ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($internal, 'CODE')) { } 71 1438 1509
elsif (UNIVERSAL::isa($internal, 'SCALAR')) { } 1050 388 1438
176 push(
177 71 @{ $self->{FILTERS} },
178 88 ▶ sub {
Subroutine __ANON__: called CC=7 SCAR=19.6
179 88 my ( $in_ref, $out_ref ) = @_;
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 ## Call in list context so that callbacks returning
186 ## an empty array (return @a where @a is empty) are
187 ## distinguished from returning the string "0". In
188 ## scalar context both would yield 0, but an empty
189 ## list means "no more input".
190 88 my @in = $internal->();
191 88 ▶ if ( !@in || ( @in == 1 && !defined $in[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @in or @in == 1 and not defined $in[0]) 55 33 88
Condition83%5/6
not @in or @in == 1 and not defined $in[0]
l!l&&r!l&&!r
124333
@in == 1 and not defined $in[0]
!ll&&!rl&&r
03343
MC/DC66%2/3
not @in or @in == 1 and not defined $in[0]
not @in @in == 1 not defined $in[0]
Truth table75%3/4
not(A) or B and not(C)
A= not @inB= @in == 1C= not defined $in[0]
ABCresult
1XX1
0111
00X0
0100
192 55 $self->{SOURCE_EMPTY} = 1;
193 55 return undef;
194 }
195 33 my $in = join '', @in;
196 33 ▶ return 0 unless length $in;
Branch: 1 branch
BranchTrueFalseTotal
unless length $in 7 26 33
197 26 $$out_ref = $in;
198  
199 26 return 1;
200 }
201 71 );
202 }
203 elsif ( UNIVERSAL::isa( $internal, 'SCALAR' ) ) {
204 push(
205 1050 @{ $self->{FILTERS} },
206 3137 ▶ sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
207 3137 my ( $in_ref, $out_ref ) = @_;
208 3137 ▶ return 0 if length $$out_ref;
Branch: 1 branch
BranchTrueFalseTotal
if length $$out_ref 0 3137 3137
209  
210 ## pump() clears auto_close_ins, finish() sets it.
211 return $self->{HARNESS}->{auto_close_ins} ? undef : 0
212 3137 ▶ if IPC::Run::_empty ${ $self->{SOURCE} }
Branch: 1 branch
BranchTrueFalseTotal
$self->{'HARNESS'}{'auto_close_ins'} ? : 651 1645 2296
213 3137 ▶ || $self->{SOURCE_EMPTY};
Branch: 1 branch
BranchTrueFalseTotal
if &IPC::Run::_empty(${$$self{"SOURCE"};}) or $self->{"SOURCE_EMPTY"} 2296 841 3137
Condition100%3/3
&IPC::Run::_empty(${$$self{"SOURCE"};}) or $self->{"SOURCE_EMPTY"}
l!l&&r!l&&!r
1959337841
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
214  
215 ## When clear_ins is set (start/pump mode), limit the chunk
216 ## copied per filter invocation. Without this limit, all of
217 ## $$internal is moved to the internal pipe buffer in one
218 ## shot. If the caller keeps appending to $$internal faster
219 ## than the child can consume data, the intermediate buffer
220 ## grows without bound -- exponential memory growth.
221 ## See https://github.com/cpan-authors/IPC-Run/issues/154
222 841 my $max_chunk = 65536;
223 841 ▶ if ( $self->{HARNESS}->{clear_ins}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'HARNESS'}{'clear_ins'} and length $$internal > $max_chunk) { } 14 827 841
Condition100%3/3
$self->{"HARNESS"}{"clear_ins"} and length $$internal > $max_chunk
!ll&&!rl&&r
56825914
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
224 && length($$internal) > $max_chunk )
225 {
226 14 $$out_ref = substr( $$internal, 0, $max_chunk );
227 14 eval { substr( $$internal, 0, $max_chunk, '' ) };
228 ## SOURCE_EMPTY intentionally not set here: more data
229 ## remains in $$internal and will be picked up on the
230 ## next filter invocation once $$out_ref has drained.
231 }
232 else {
233 827 $$out_ref = $$internal;
234 259 eval { $$internal = '' }
235 827 ▶ if $self->{HARNESS}->{clear_ins};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"HARNESS"}{"clear_ins"} 259 568 827
236  
237 827 $self->{SOURCE_EMPTY} = $self->{HARNESS}->{auto_close_ins};
238 }
239  
240 841 return 1;
241 }
242 1050 );
243 }
244 }
245  
246 4334 return $self;
247 }
248  
249 =item filename
250  
251 Gets/sets the filename. Returns the value after the name change, if
252 any.
253  
254 =cut
255  
256 4 ▶sub filename {
Subroutine filename: called CC=2 SCAR=7.1
Pod documented
257 4 my IPC::Run::IO $self = shift;
258 4 ▶ $self->{FILENAME} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 4 4
259 4 return $self->{FILENAME};
260 }
261  
262 =item init
263  
264 Does initialization required before this can be run. This includes open()ing
265 the file, if necessary, and clearing the destination scalar if necessary.
266  
267 =cut
268  
269 2 ▶sub init {
Subroutine init: called CC=7 SCAR=19.7
Pod documented
270 2 my IPC::Run::IO $self = shift;
271  
272 2 $self->{SOURCE_EMPTY} = 0;
273 1 ${ $self->{DEST} } = ''
274 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
275  
276 2 ▶ $self->open if defined $self->filename;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->filename 2 0 2
277 2 $self->{FD} = $self->fileno;
278  
279 2 ▶ if ( !$self->{FILTERS} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'FILTERS'}) { } 0 2 2
280 0 $self->{FBUFS} = undef;
281 }
282 else {
283 2 @{ $self->{FBUFS} } = map {
284 3 my $s = "";
285 3 \$s;
286 2 } ( @{ $self->{FILTERS} }, '' );
287  
288 $self->{FBUFS}->[0] = $self->{DEST}
289 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
290 2 push @{ $self->{FBUFS} }, $self->{SOURCE};
291 }
292  
293 2 return undef;
294 }
295  
296 =item open
297  
298 If a filename was passed in, opens it. Determines if the handle is open
299 via fileno(). Throws an exception on error.
300  
301 =cut
302  
303 231 my %open_flags = (
304 '>' => O_RDONLY,
305 '>>' => O_RDONLY,
306 '<' => O_WRONLY | O_CREAT | O_TRUNC,
307 '<<' => O_WRONLY | O_CREAT | O_APPEND,
308 );
309  
310 2 ▶sub open {
Subroutine open: called CC=5 SCAR=17.2
Pod documented
311 2 my IPC::Run::IO $self = shift;
312  
313 croak "IPC::Run::IO: Can't open() a file with no name"
314 2 ▶ unless defined $self->{FILENAME};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"FILENAME"} 0 2 2
315 2 ▶ $self->{HANDLE} = gensym unless $self->{HANDLE};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"HANDLE"} 2 0 2
316  
317 2 ▶ _debug "opening '", $self->filename, "' mode '", $self->mode, "'"
Branch: 1 branch
BranchTrueFalseTotal
if &_debugging_data() 0 2 2
318 if _debugging_data;
319 sysopen(
320 $self->{HANDLE},
321 $self->filename,
322 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[']) 2 0 2
323 ) or croak "IPC::Run::IO: $! opening '$self->{FILENAME}', mode '" . $self->mode . "'";
324  
325 2 return undef;
326 }
327  
328 =item open_pipe
329  
330 If this is a redirection IO object, this opens the pipe in a platform
331 independent manner.
332  
333 =cut
334  
335 3719 ▶sub _do_open {
Subroutine _do_open: called CC=11 SCAR=24.0
336 3719 my $self = shift;
337 3719 my ( $child_debug_fd, $parent_handle ) = @_;
338  
339 3719 ▶ if ( $self->dir eq "<" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dir eq '<') { } 1201 2518 3719
340 1201 ▶ if ( $self->{TYPE} eq '<blocking_pipe' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'TYPE'} eq '<blocking_pipe') { } 63 1138 1201
341 63 ( $self->{TFD}, $self->{FD} ) = IPC::Run::_pipe;
342 }
343 else {
344 1138 ( $self->{TFD}, $self->{FD} ) = IPC::Run::_pipe_nb;
345 }
346 1201 ▶ if ($parent_handle) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent_handle) 98 1103 1201
347 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
348 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") 98 0 98
349 or croak "$! duping write end of pipe for caller";
350 98 ▶ $$parent_handle = $fh if ref $parent_handle eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $parent_handle eq "SCALAR" 2 96 98
351 }
352 }
353 else {
354 2518 ( $self->{FD}, $self->{TFD} ) = IPC::Run::_pipe;
355 2518 ▶ if ($parent_handle) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent_handle) 61 2457 2518
356 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
357 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") 61 0 61
358 or croak "$! duping read end of pipe for caller";
359 61 ▶ $$parent_handle = $fh if ref $parent_handle eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $parent_handle eq "SCALAR" 3 58 61
360 }
361 }
362 }
363  
364 3719 ▶sub open_pipe {
Subroutine open_pipe: called CC=3 SCAR=11.0
Pod documented
365 3719 my IPC::Run::IO $self = shift;
366  
367 ## Hmmm, Maybe allow named pipes one day. But until then...
368 croak "IPC::Run::IO: Can't pipe() when a file name has been set"
369 3719 ▶ if defined $self->{FILENAME};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"FILENAME"} 0 3719 3719
370  
371 3719 $self->_do_open(@_);
372  
373 ## return ( child_fd, parent_fd )
374 return $self->dir eq "<"
375 ? ( $self->{TFD}, $self->{FD} )
376 3719 ▶ : ( $self->{FD}, $self->{TFD} );
Branch: 1 branch
BranchTrueFalseTotal
$self->dir eq '<' ? : 1201 2518 3719
377 }
378  
379 3881 ▶sub _cleanup { ## Called from Run.pm's _cleanup
Subroutine _cleanup: called CC=1 SCAR=0.0
380 3881 my $self = shift;
381 3881 undef $self->{FAKE_PIPE};
382 }
383  
384 =item close
385  
386 Closes the handle. Throws an exception on failure.
387  
388  
389 =cut
390  
391 3233 ▶sub close {
Subroutine close: called CC=4 SCAR=14.5
Pod documented
392 3233 my IPC::Run::IO $self = shift;
393  
394 3233 ▶ if ( defined $self->{HANDLE} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'HANDLE'}) { } 2 3231 3233
395 close $self->{HANDLE}
396 or croak(
397 "IPC::Run::IO: $! closing "
398 . (
399 defined $self->{FILENAME}
400 2 ▶ ? "'$self->{FILENAME}'"
Branch: 2 branches
BranchTrueFalseTotal
CORE::close $self->{"HANDLE"} or croak("IPC::Run::IO: $! closing " . (defined $self->{"FILENAME"} ? qq['$self->{"FILENAME"}'] : "handle")) 2 0 2
defined $self->{'FILENAME'} ? : 0 0 0
401 : "handle"
402 )
403 );
404 }
405 else {
406 3231 IPC::Run::_close( $self->{FD} );
407 }
408  
409 3233 $self->{FD} = undef;
410  
411 3233 return undef;
412 }
413  
414 =item fileno
415  
416 Returns the fileno of the handle. Throws an exception on failure.
417  
418  
419 =cut
420  
421 4 ▶sub fileno {
Subroutine fileno: called CC=3 SCAR=12.5
Pod documented
422 4 my IPC::Run::IO $self = shift;
423  
424 4 my $fd = fileno $self->{HANDLE};
425 croak(
426 "IPC::Run::IO: $! "
427 . (
428 defined $self->{FILENAME}
429 4 ▶ ? "'$self->{FILENAME}'"
Branch: 2 branches
BranchTrueFalseTotal
unless defined $fd 0 4 4
defined $self->{'FILENAME'} ? : 0 0 0
430 : "handle"
431 )
432 ) unless defined $fd;
433  
434 4 return $fd;
435 }
436  
437 =item mode
438  
439 Returns the operator in terms of 'r', 'w', and 'a'. There is a state
440 'ra', unlike Perl's open(), which indicates that data read from the
441 handle or file will be appended to the output if the output is a scalar.
442 This is only meaningful if the output is a scalar, it has no effect if
443 the output is a subroutine.
444  
445 The redirection operators can be a little confusing, so here's a reference
446 table:
447  
448 > r Read from handle in to process
449 < w Write from process out to handle
450 >> ra Read from handle in to process, appending it to existing
451 data if the destination is a scalar.
452 << wa Write from process out to handle, appending to existing
453 data if IPC::Run::IO opened a named file.
454  
455 =cut
456  
457 10 ▶sub mode {
Subroutine mode: called CC=4 SCAR=13.9
Pod documented
458 10 my IPC::Run::IO $self = shift;
459  
460 10 ▶ croak "IPC::Run::IO: unexpected arguments for mode(): @_" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 10 10
461  
462 ## TODO: Optimize this
463 10 ▶ return ( $self->{TYPE} =~ /</ ? 'w' : 'r' ) . ( $self->{TYPE} =~ /<<|>>/ ? 'a' : '' );
Branch: 2 branches
BranchTrueFalseTotal
$self->{'TYPE'} =~ /</ ? : 5 5 10
$self->{'TYPE'} =~ /<<|>>/ ? : 2 8 10
464 }
465  
466 =item op
467  
468 Returns the operation: '<', '>', '<<', '>>'. See L</mode> if you want
469 to spell these 'r', 'w', etc.
470  
471 =cut
472  
473 4336 ▶sub op {
Subroutine op: called CC=2 SCAR=7.1
Pod documented
474 4336 my IPC::Run::IO $self = shift;
475  
476 4336 ▶ croak "IPC::Run::IO: unexpected arguments for op(): @_" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 4336 4336
477  
478 4336 return $self->{TYPE};
479 }
480  
481 =item binmode
482  
483 Sets/gets whether this pipe is in binmode or not. No effect off of Win32
484 OSs, of course, and on Win32, no effect after the harness is start()ed.
485  
486 =cut
487  
488 4334 ▶sub binmode {
Subroutine binmode: called CC=2 SCAR=7.1
Pod documented
489 4334 my IPC::Run::IO $self = shift;
490  
491 4334 ▶ $self->{BINMODE} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 4334 0 4334
492  
493 4334 return $self->{BINMODE};
494 }
495  
496 =item dir
497  
498 Returns the first character of $self->op. This is either "<" or ">".
499  
500 =cut
501  
502 23435 ▶sub dir {
Subroutine dir: called CC=2 SCAR=7.1
Pod documented
503 23435 my IPC::Run::IO $self = shift;
504  
505 23435 ▶ croak "IPC::Run::IO: unexpected arguments for dir(): @_" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 23435 23435
506  
507 23435 return substr $self->{TYPE}, 0, 1;
508 }
509  
510 ##
511 ## Filter Scaffolding
512 ##
513 #my $filter_op ; ## The op running a filter chain right now
514 #my $filter_num; ## Which filter is being run right now.
515  
516 231 ▶use vars (
Subroutine BEGIN: called CC=1 SCAR=0.0
517 231 '$filter_op', ## The op running a filter chain right now
518 '$filter_num' ## Which filter is being run right now.
519 231 );
520  
521 4475 ▶sub _init_filters {
Subroutine _init_filters: called CC=6 SCAR=17.9
522 4475 my IPC::Run::IO $self = shift;
523  
524 4475 ▶ 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 4475 4475
525 4475 $self->{FBUFS} = [];
526  
527 $self->{FBUFS}->[0] = $self->{DEST}
528 4475 ▶ if $self->{DEST} && ref $self->{DEST} eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEST"} and ref $self->{"DEST"} eq "SCALAR" 2368 2107 4475
Condition100%3/3
$self->{"DEST"} and ref $self->{"DEST"} eq "SCALAR"
!ll&&!rl&&r
16794282368
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
529  
530 4475 ▶ return unless $self->{FILTERS} && @{ $self->{FILTERS} };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"FILTERS"} and @{$self->{"FILTERS"};} 2887 1588 4475
Condition66%2/3
$self->{"FILTERS"} and @{$self->{"FILTERS"};}
!ll&&!rl&&r
028871588
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
531  
532 1588 push @{ $self->{FBUFS} }, map {
533 3879 my $s = "";
534 3879 \$s;
535 1588 } ( @{ $self->{FILTERS} }, '' );
536  
537 1588 push @{ $self->{FBUFS} }, $self->{SOURCE};
538 }
539  
540 =item poll
541  
542 TODO: Needs confirmation that this is correct. Was previously undocumented.
543  
544 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.
545  
546 =cut
547  
548 16043 ▶sub poll {
Subroutine poll: called CC=8 SCAR=20.9
Pod documented
549 16043 my IPC::Run::IO $self = shift;
550 16043 my ($harness) = @_;
551  
552 16043 ▶ if ( defined $self->{FD} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"FD"}) 15997 46 16043
553 15997 my $d = $self->dir;
554 15997 ▶ if ( $d eq "<" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($d eq '<') { } 2166 13831 15997
elsif ($d eq '>') { } 13831 0 13831
555 2166 ▶ if ( vec $harness->{WOUT}, $self->{FD}, 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (vec $harness->{"WOUT"}, $self->{"FD"}, 1) 1534 632 2166
556 1534 ▶ _debug_desc_fd( "filtering data to", $self )
Branch: 1 branch
BranchTrueFalseTotal
if &_debugging_details() 0 1534 1534
557 if _debugging_details;
558 1534 return $self->_do_filters($harness);
559 }
560 }
561 elsif ( $d eq ">" ) {
562 13831 ▶ if ( vec $harness->{ROUT}, $self->{FD}, 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (vec $harness->{"ROUT"}, $self->{"FD"}, 1) 4213 9618 13831
563 4213 ▶ _debug_desc_fd( "filtering data from", $self )
Branch: 1 branch
BranchTrueFalseTotal
if &_debugging_details() 0 4213 4213
564 if _debugging_details;
565 4213 return $self->_do_filters($harness);
566 }
567 }
568 }
569 10296 return 0;
570 }
571  
572 8684 ▶sub _do_filters {
Subroutine _do_filters: called CC=5 SCAR=16.9
573 8684 my IPC::Run::IO $self = shift;
574  
575 8684 ( $self->{HARNESS} ) = @_;
576  
577 8684 my ( $saved_op, $saved_num ) = ( $IPC::Run::filter_op, $IPC::Run::filter_num );
578 8684 $IPC::Run::filter_op = $self;
579 8684 $IPC::Run::filter_num = -1;
580 8684 my $redos = 0;
581 8684 my $r;
582 {
583 8684 $@ = '';
584 8684 $r = eval { IPC::Run::get_more_input(); };
585  
586 # Detect Resource temporarily unavailable and re-try 200 times (2 seconds), assuming select behaves (which it doesn't always? need ref)
587 8684 ▶ if ( ( $@ || '' ) =~ $IPC::Run::_EAGAIN && $redos++ < 200 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($@ || "") =~ /$IPC::Run::_EAGAIN/ and $redos++ < 200) 0 8684 8684
Condition40%2/5
($@ || "") =~ /$IPC::Run::_EAGAIN/ and $redos++ < 200
!ll&&!rl&&r
868400
$@ || ''
l!l
08684
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
588 0 select( undef, undef, undef, 0.01 );
589 0 redo;
590 }
591 }
592 8684 ( $IPC::Run::filter_op, $IPC::Run::filter_num ) = ( $saved_op, $saved_num );
593 8684 $self->{HARNESS} = undef;
594 8684 ▶ die "ack ", $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 8684 8684
595 8684 return $r;
596 }
597  
598 =back
599  
600 =head1 AUTHOR
601  
602 Barrie Slaymaker <barries@slaysys.com>
603  
604 =head1 TODO
605  
606 Implement bidirectionality.
607  
608 =cut
609  
610 231 1;
« blib/lib/IPC/Run/Debug.pm ↑ Summary blib/lib/IPC/Run/Timer.pm »