blib/lib/IPC/Pipeline.pm

filter: Statementstmt 96.6% 57 / 59 Branchbran 75.0% 30 / 40 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 2 totaltotal 87.1% 95 / 109 CC 23 SCAR 26.8
CC
23
Cov
88%
pipeline
20.6
exec_filter
4.8
BEGIN
1.0
CRAP 14.6
↑ Summary
1 package IPC::Pipeline;
2  
3 # Copyright (c) 2012, cPanel, Inc.
4 # All rights reserved.
5 # http://cpanel.net/
6 #
7 # This is free software; you can redistribute it and/or modify it under the same
8 # terms as Perl itself. See the LICENSE file for further details.
9  
10 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 17 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 BEGIN {
16 17 ▶ use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 17 ▶ use vars qw( $VERSION @ISA @EXPORT );
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 17 ▶ $VERSION = '0.9';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 17 @ISA = ('Exporter');
21 17 @EXPORT = ('pipeline');
22 }
23  
24 sub exec_filter {
25 13 ▶ my ($filter) = @_;
Subroutine exec_filter: called CC=4 SCAR=15.6
Pod undocumented
26  
27 13 ▶ if ( ref($filter) eq 'CODE' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $filter eq 'CODE') { } 5 8 13
elsif (ref $filter eq 'ARRAY') { } 8 0 8
28 5 exit $filter->();
29 }
30 elsif ( ref($filter) eq 'ARRAY' ) {
31 8 ▶ exec(@$filter) or die("Cannot exec(): $!");
Branch: 1 branch
BranchTrueFalseTotal
exec @$filter or die "Cannot exec(): $!" 0 0 0
32 }
33  
34 0 die('Invalid filter');
35 }
36  
37 sub pipeline {
38 31 ▶ my @filters = @_[ 3 .. $#_ ];
Subroutine pipeline: called CC=20 SCAR=30.3
Pod undocumented
39  
40 31 ▶ die('Not enough arguments') unless @_ >= 4;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ >= 4 4 27 31
41  
42 #
43 # Validate the filters and die early.
44 #
45 27 foreach my $filter (@filters) {
46 48 ▶ next if ref($filter) =~ /^CODE|ARRAY$/;
Branch: 1 branch
BranchTrueFalseTotal
if ref($filter) =~ /^CODE|ARRAY$/ 47 1 48
47  
48 1 die('Filter passed is not a CODE reference or ARRAY containing command and arguments');
49 }
50  
51 #
52 # Create the initial pipe for passing data into standard input to the first
53 # filter passed. Share a single pipe for standard error use for each
54 # process.
55 #
56 26 ▶ pipe my ( $child_out, $in ) or die("Cannot create a file handle pair for standard input piping: $!");
Branch: 1 branch
BranchTrueFalseTotal
pipe my $child_out, my $in or die "Cannot create a file handle pair for standard input piping: $!" 0 26 26
57 26 ▶ pipe my ( $error_out, $error_in ) or die("Cannot create a file handle pair for standard error piping: $!");
Branch: 1 branch
BranchTrueFalseTotal
pipe my $error_out, my $error_in or die "Cannot create a file handle pair for standard error piping: $!" 0 26 26
58  
59 26 my @pids;
60  
61 26 foreach my $filter (@filters) {
62 42 ▶ pipe my ( $out, $child_in ) or die("Cannot create a file handle pair for standard output piping: $!");
Branch: 1 branch
BranchTrueFalseTotal
pipe my $out, my $child_in or die "Cannot create a file handle pair for standard output piping: $!" 0 42 42
63  
64 42 my $pid = fork();
65  
66 42 ▶ if ( !defined($pid) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $pid) { } 0 42 42
elsif ($pid == 0) { } 13 29 42
67 0 die("Cannot fork(): $!");
68 }
69 elsif ( $pid == 0 ) {
70 13 ▶ open( STDIN, '<&=' . fileno($child_out) ) or die("Cannot dup2() last output fd to current child stdin: $!");
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, '<&=' . fileno($child_out) or die "Cannot dup2() last output fd to current child stdin: $!" 0 13 13
71 13 ▶ open( STDOUT, '>&=' . fileno($child_in) ) or die("Cannot dup2() last input fd to current child stdout: $!");
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, '>&=' . fileno($child_in) or die "Cannot dup2() last input fd to current child stdout: $!" 0 13 13
72 13 ▶ open( STDERR, '>&=' . fileno($error_in) ) or die("Cannot dup2() error pipe input to current child stderr: $!");
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, '>&=' . fileno($error_in) or die "Cannot dup2() error pipe input to current child stderr: $!" 0 13 13
73  
74 13 exec_filter($filter);
75 }
76  
77 #
78 # This last child STDOUT file handle should be duplicated onto the next
79 # process' standard input reader, or will be passed as the last child
80 # output file descriptor if no other subsequent commands are left
81 # to be run.
82 #
83 29 $child_out = $out;
84  
85 29 push @pids, $pid;
86 }
87  
88 #
89 # Substitute the first three arguments passed by the user with the file
90 # handle on the parent's writing end of the initial pipe created for
91 # writing to the first command, the last output file handle for the
92 # last command, and the standard error handle. If typeglobs or numeric
93 # file descriptors for existing file handles are passed, an attempt will
94 # be made to dup2() them as appropriate.
95 #
96 # The evals around the assignments are present so that we don't die when the
97 # argument passed in is undef. Perl 5.19 and newer don't like that.
98 # [perl #7508, #109726]
99 13 ▶ if ( !defined $_[0] ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $_[0]) { } 9 4 13
elsif (ref $_[0] eq 'GLOB') { } 3 1 4
100 9 eval { $_[0] = $in };
101 }
102 elsif ( ref( $_[0] ) eq 'GLOB' ) {
103 3 open( $_[0], '>&=' . fileno($in) );
104 }
105 else {
106 1 POSIX::dup2( fileno($in), $_[0] );
107 }
108  
109 13 ▶ if ( !defined $_[1] ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $_[1]) { } 9 4 13
elsif (ref $_[1] eq 'GLOB') { } 3 1 4
110 9 eval { $_[1] = $child_out };
111 }
112 elsif ( ref( $_[1] ) eq 'GLOB' ) {
113 3 open( $_[1], '<&=' . fileno($child_out) );
114 }
115 else {
116 1 POSIX::dup2( fileno($child_out), $_[1] );
117 }
118  
119 13 ▶ if ( !defined $_[2] ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $_[2]) { } 9 4 13
elsif (ref $_[2] eq 'GLOB') { } 3 1 4
120 9 eval { $_[2] = $error_out };
121 }
122 elsif ( ref( $_[2] ) eq 'GLOB' ) {
123 3 open( $_[2], '<&=' . fileno($error_out) );
124 }
125 else {
126 1 POSIX::dup2( fileno($error_out), $_[2] );
127 }
128  
129 13 ▶ return wantarray ? @pids : \@pids;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 8 5 13
130 }
131  
132 17 1;
133  
134 __END__
↑ Summary