blib/lib/IPC/PrettyPipe/Stream.pm

filter: Statementstmt 70.6% 77 / 109 Branchbran 38.2% 13 / 34 Conditioncond 28.5% 4 / 14 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 65.3% 17 / 26 Podpod 50.0% 2 / 4 totaltotal 58.1% 114 / 196 CC 24 SCAR 16.8
CC
24
Cov
60%
__ANON__
12.0
_dup
12.0
BUILD
8.2
CRAP 5.4
« blib/lib/IPC/PrettyPipe.pm ↑ Summary blib/lib/IPC/PrettyPipe/Stream/Utils.pm »
1 package IPC::PrettyPipe::Stream;
2  
3 # ABSTRACT: An I/O stream for an B<IPC::PrettyPipe> pipeline or command
4  
5 22 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 22 ▶use Types::Standard qw[ Bool Str ];
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 22 ▶use IPC::PrettyPipe::Stream::Utils qw[ parse_spec ];
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 22 ▶use String::ShellQuote 'shell_quote';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 22 ▶use IO::ReStoreFH;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 22 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 22 ▶use Fcntl qw[ O_RDONLY O_WRONLY O_CREAT O_TRUNC O_APPEND ];
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 22 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 22 our $VERSION = '0.13';
20  
21  
22 22 with 'IPC::PrettyPipe::Queue::Element';
23  
24 22 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26  
27 22 my %fh_map = (
28 0 => *STDIN,
29 1 => *STDOUT,
30 2 => *STDERR
31 );
32  
33 22 my %op_map = (
34  
35 '<' => O_RDONLY,
36 '>' => O_WRONLY | O_CREAT | O_TRUNC,
37 '>>' => O_WRONLY | O_CREAT | O_APPEND
38  
39 );
40  
41 22 has N => (
42 is => 'rwp',
43 predicate => 1,
44 init_arg => undef,
45 );
46 22 has M => (
47 is => 'rwp',
48 predicate => 1,
49 init_arg => undef,
50 );
51 22 has Op => (
52 is => 'rwp',
53 init_arg => undef,
54 );
55  
56 22 has spec => (
57 is => 'rwp',
58 isa => Str,
59 required => 1,
60 );
61  
62 22 has file => (
63 is => 'rw',
64 isa => Str,
65 predicate => 1,
66 );
67  
68 22 has _type => (
69 is => 'rwp',
70 isa => Str,
71 init_arg => undef,
72 );
73  
74 22 has requires_file => (
75 is => 'rwp',
76 init_arg => undef,
77 );
78  
79 has strict => (
80 is => 'ro',
81 isa => Bool,
82 5 ▶ default => sub { 1 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
83  
84  
85  
86  
87  
88 205 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=5 SCAR=17.5
Pod undocumented
89  
90 205 my $class = shift;
91  
92 205 ▶ if ( @_ == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 3 202 205
93  
94 3 ▶ return $_[0] if 'HASH' eq ref( $_[0] );
Branch: 1 branch
BranchTrueFalseTotal
if "HASH" eq ref $_[0] 0 3 3
95  
96 return { spec => $_[0][0], file => $_[0][1] }
97 0 ▶ if 'ARRAY' eq ref( $_[0] ) && @{ $_[0] } == 2;
Branch: 1 branch
BranchTrueFalseTotal
if "ARRAY" eq ref $_[0] and @{$_[0];} == 2 0 3 3
Condition33%1/3
"ARRAY" eq ref $_[0] and @{$_[0];} == 2
!ll&&!rl&&r
300
MC/DC0%0/2
"ARRAY" eq ref $_[0] and @{$_[0];} == 2
"ARRAY" eq ref $_[0] @{$_[0];} == 2
Truth table33%1/3
A and B
A= "ARRAY" eq ref $_[0]B= @{$_[0];} == 2
ABresult
0X0
100
111
98  
99 3 return { spec => $_[0] };
100  
101 }
102  
103 202 return {@_};
104  
105 }
106  
107 205 ▶sub BUILD {
Subroutine BUILD: called CC=8 SCAR=21.1
Pod undocumented
108  
109 205 my $self = shift;
110  
111 ## no critic (ProhibitAccessOfPrivateData)
112  
113 205 my $opc = parse_spec( $self->spec );
114  
115 croak( __PACKAGE__, ': ', "cannot parse stream specification: ",
116 $self->spec )
117 205 ▶ unless defined $opc->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opc->{"type"} 133 72 205
118  
119 72 $self->_set_requires_file( $opc->{param} );
120 72 $self->_set__type( $opc->{type} );
121  
122 146 $self->${ \"_set_$_" }( $opc->{$_} )
123 72 for grep { exists $opc->{$_} } qw[ N M Op ];
124  
125  
126 72 ▶ if ( $self->strict ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->strict) 4 68 72
127  
128 4 ▶ croak( __PACKAGE__, ': ', "stream specification ",
Branch: 1 branch
BranchTrueFalseTotal
if $self->requires_file and not $self->has_file 2 2 4
Condition66%2/3
$self->requires_file and not $self->has_file
!ll&&!rl&&r
022
MC/DC50%1/2
$self->requires_file and not $self->has_file
$self->requires_file not $self->has_file
Truth table66%2/3
A and not(B)
A= $self->requires_fileB= not $self->has_file
ABresult
0X0
100
111
129 $self->spec, "requires a file\n" )
130 if $self->requires_file && !$self->has_file;
131  
132 2 ▶ croak( __PACKAGE__, ': ', "stream specification ",
Branch: 1 branch
BranchTrueFalseTotal
if not $self->requires_file and $self->has_file 0 2 2
Condition33%1/3
not $self->requires_file and $self->has_file
!ll&&!rl&&r
200
MC/DC0%0/2
not $self->requires_file and $self->has_file
not $self->requires_file $self->has_file
Truth table33%1/3
not(A) and B
A= not $self->requires_fileB= $self->has_file
ABresult
0X0
100
111
133 $self->spec, "should not have a file\n" )
134 if !$self->requires_file && $self->has_file;
135  
136 }
137  
138 70 return;
139 }
140  
141 25 ▶sub quoted_file { shell_quote( $_[0]->file ) }
Subroutine quoted_file: called CC=1 SCAR=0.0
Pod documented
142  
143 4 ▶sub _redirect {
Subroutine _redirect: called CC=3 SCAR=15.7
144  
145 4 my ( $self, $N ) = @_;
146  
147 4 my $file = $self->file;
148  
149 4 my $sub;
150  
151 4 ▶ if ( defined $N ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $N) { } 4 0 4
152  
153 4 my $op = $self->Op;
154  
155 4 ▶ $sub = sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
156 4 ▶ open( $N, $op, $file ) or die( "unable to open ", $file, ": $!\n" );
Branch: 1 branch
BranchTrueFalseTotal
open $N, $op, $file or die "unable to open ", $file, ": $!\n" 4 0 4
157 4 };
158 }
159  
160 else {
161  
162 0 $N = $self->N;
163 0 ▶ my $op = $op_map{ $self->Op }
Condition0%0/2
$op_map{$self->Op} // croak("error: unrecognized operator: ", $self->Op, "\n")
l!l
00
MC/DC0%0/1
$op_map{$self->Op} // croak("error: unrecognized operator: ", $self->Op, "\n")
$op_map{$self->Op}
Truth table0%0/2
A
A= $op_map{$self->Op}
Aresult
11
00
164 // croak( "error: unrecognized operator: ", $self->Op, "\n" );
165  
166 0 ▶ $sub = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
167 0 ▶ my $nfd = POSIX::open( $file, $op, oct( 644 ) )
Branch: 1 branch
BranchTrueFalseTotal
my $nfd = POSIX::open($file, $op, 420) or croak("error opening", $file, ": $!\n") 0 0 0
168 or croak( 'error opening', $file, ": $!\n" );
169 0 ▶ POSIX::dup2( $nfd, $N )
Branch: 1 branch
BranchTrueFalseTotal
POSIX::dup2($nfd, $N) or croak("error in dup2( $nfd, $N ): $!\n") 0 0 0
170 or croak( "error in dup2( $nfd, $N ): $!\n" );
171 0 POSIX::close( $nfd );
172 0 };
173  
174 }
175  
176 4 return $sub, $N;
177 }
178  
179 0 ▶sub _dup {
Subroutine _dup: not called CC=3 SCAR=24.8
180  
181 0 my ( $self, $N, $M ) = @_;
182  
183 0 ▶ $M //= $self->M;
Condition0%0/3
$M //= $self->M
l!l&&r!l&&!r
000
MC/DC0%0/2
$M //= $self->M
$M $self->M
Truth table0%0/3
A //= B
A= $MB= $self->M
ABresult
1X1
011
000
184  
185 0 my $sub;
186  
187 # if $N is a known filehandle, we're in luck
188 0 ▶ if ( defined $N ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $N) { } 0 0 0
189  
190 0 ▶ $sub = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
191 0 ▶ open( $N, '>&', $M )
Branch: 1 branch
BranchTrueFalseTotal
open $N, ">&", $M or die "error in open($N >& $M): $!\n" 0 0 0
192 or die( "error in open($N >& $M): $!\n" );
193 0 };
194  
195 }
196  
197 else {
198  
199 0 $N = $self->N;
200 0 $M = $self->M;
201  
202 0 ▶ $sub = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
203 0 ▶ POSIX::dup2( $N, $M )
Branch: 1 branch
BranchTrueFalseTotal
POSIX::dup2($N, $M) or die "error in dup2( $N, $M ): $!\n" 0 0 0
204 or die( "error in dup2( $N, $M ): $!\n" );
205 0 };
206  
207 }
208  
209 0 return $sub, $N;
210 }
211  
212 0 ▶sub _redirect_stdout_stderr {
Subroutine _redirect_stdout_stderr: not called CC=1 SCAR=6.9
213  
214 0 my $self = shift;
215  
216 0 ( undef, my $sub_redir ) = $self->_redirect( *STDOUT );
217 0 ( undef, my $sub_dup ) = $self->_dup( *STDERR, *STDOUT );
218 0 ▶ return sub { $sub_redir->(), $sub_dup->() }, *STDOUT, *STDERR;
Subroutine __ANON__: not called CC=1 SCAR=6.9
219  
220 }
221  
222 0 ▶sub _close {
Subroutine _close: not called CC=2 SCAR=17.9
223  
224 0 my ( $self, $N ) = @_;
225  
226 0 my $sub;
227  
228 0 ▶ if ( defined $N ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $N) { } 0 0 0
229  
230 0 ▶ $sub = sub { close( $N ) or die( "error in closing $N: $!\n" ); };
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
close $N or die "error in closing ${N}: $!\n" 0 0 0
231  
232 }
233  
234 else {
235  
236 0 $N = $self->N;
237  
238 0 ▶ $sub = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
239 0 ▶ POSIX::close( $N )
Branch: 1 branch
BranchTrueFalseTotal
POSIX::close($N) or die "error in closing ${N}: $!\n" 0 0 0
240 or die( "error in closing $N: $!\n" );
241 0 };
242  
243 }
244  
245 0 return $sub, $N;
246  
247 }
248  
249 4 ▶sub apply {
Subroutine apply: called CC=1 SCAR=0.0
Pod documented
250  
251 4 my $self = shift;
252  
253 4 my ( $N, $M ) = do {
254  
255 22 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
256  
257 4 map { $fh_map{$_} } $self->N, $self->M;
258  
259 };
260  
261 4 my $mth = '_' . $self->_type;
262 4 return $self->$mth( $N, $M );
263 }
264  
265  
266  
267 22 1;
268  
269 #
270 # This file is part of IPC-PrettyPipe
271 #
272 # This software is Copyright (c) 2018 by Smithsonian Astrophysical Observatory.
273 #
274 # This is free software, licensed under:
275 #
276 # The GNU General Public License, Version 3, June 2007
277 #
278  
279 __END__
« blib/lib/IPC/PrettyPipe.pm ↑ Summary blib/lib/IPC/PrettyPipe/Stream/Utils.pm »