| 1 | package IPC::Run3::Simple; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Simple utility module to make the easy to use IPC::Run3 even more easy to use. | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use IPC::Run3 (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 4 | our $VERSION = '0.011'; # VERSION | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 4 | our @EXPORT = qw( run3 ); | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 4 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | chomp_err chomp_out croak_on_err default_stderr default_stdin default_stdout | ||||||||||||||||||||||||||||
| 19 | tee_systemcall | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 4 | our %EXPORT_TAGS = ( 'all' => [ @EXPORT, @EXPORT_OK ] ); | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 4 | our $CHOMP_ERR = 1; | |||||||||||||||||||||||||||
| 26 | 4 | our $CHOMP_OUT = 1; | |||||||||||||||||||||||||||
| 27 | 4 | our $CROAK_ON_ERR = 0; | |||||||||||||||||||||||||||
| 28 | 4 | our $DEFAULT_STDIN = undef; | |||||||||||||||||||||||||||
| 29 | 4 | our $DEFAULT_STDOUT = \my $out; | |||||||||||||||||||||||||||
| 30 | 4 | our $DEFAULT_STDERR = \my $err; | |||||||||||||||||||||||||||
| 31 | 4 | our $TEE_SYSTEMCALL = 0; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 4 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=3 SCAR=15.0
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # Is Capture::Tiny available? | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 4 | ▶ | if ( eval { require Capture::Tiny } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 4 | Capture::Tiny->import( 'tee' ); | |||||||||||||||||||||||||||
| 40 | 4 | ▶ | *tee_systemcall = sub { $TEE_SYSTEMCALL = ! ! +shift }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | } else { | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | ▶ | *tee_systemcall = sub { $TEE_SYSTEMCALL = 0 }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # Is Time::HiRes available? | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 4 | ▶ | if ( eval { require Time::HiRes } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 4 | Time::HiRes->import( qw( gettimeofday tv_interval ) ); | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | } else { | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | *gettimeofday = sub { time, 0 }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | *tv_interval = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 59 | 0 | my ( $t0, $t1 ) = @_; | |||||||||||||||||||||||||||
| 60 | 0 | ▶ | $t1 = [ gettimeofday() ] unless defined $t1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 0 | $t1->[ 0 ] - $t0->[ 0 ]; | |||||||||||||||||||||||||||
| 62 | 0 | }; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # '!! +shift' forces the value to be either undef or 1; | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 2 | ▶ | sub chomp_err { $CHOMP_ERR = ! ! +shift } | ||||||||||||||||||||||||||
|
Subroutine
chomp_err: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | 2 | ▶ | sub chomp_out { $CHOMP_OUT = ! ! +shift } | ||||||||||||||||||||||||||
|
Subroutine
chomp_out: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 72 | 2 | ▶ | sub croak_on_err { $CROAK_ON_ERR = ! ! +shift } | ||||||||||||||||||||||||||
|
Subroutine
croak_on_err: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | 2 | ▶ | sub default_stderr { $DEFAULT_STDERR = shift } | ||||||||||||||||||||||||||
|
Subroutine
default_stderr: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 74 | 2 | ▶ | sub default_stdin { $DEFAULT_STDIN = shift } | ||||||||||||||||||||||||||
|
Subroutine
default_stdin: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | 2 | ▶ | sub default_stdout { $DEFAULT_STDOUT = shift } | ||||||||||||||||||||||||||
|
Subroutine
default_stdout: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 4 | ▶ | sub run3 { | ||||||||||||||||||||||||||
|
Subroutine
run3: called CC=25 SCAR=48.3
Pod
documented
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 4 | my $arg = shift; | |||||||||||||||||||||||||||
| 81 | 4 | my $ref = ref $arg; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 4 | my $return_array = 0; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 4 | my ( $cmd, $stdin, $stdout, $stderr, $options ); | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 4 | ▶ | if ( $ref eq 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 3 | $return_array++; | |||||||||||||||||||||||||||
| 90 | 3 | $cmd = $arg; | |||||||||||||||||||||||||||
| 91 | 3 | $stdin = $DEFAULT_STDIN; | |||||||||||||||||||||||||||
| 92 | 3 | $stdout = $DEFAULT_STDOUT; | |||||||||||||||||||||||||||
| 93 | 3 | $stderr = $DEFAULT_STDERR; | |||||||||||||||||||||||||||
| 94 | 3 | $options = {}; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | } elsif ( $ref eq 'HASH' ) { | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | croak "'cmd' required and must be a reference to an array" | ||||||||||||||||||||||||||||
| 99 | 0 | ▶ | unless exists $arg->{ 'cmd' } && ref $arg->{ 'cmd' } eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $arg->{"cmd"} and ref $arg->{"cmd"} eq "ARRAY"
MC/DC0%0/2
exists $arg->{"cmd"} and ref $arg->{"cmd"} eq "ARRAY"
exists $arg->{"cmd"}
ref $arg->{"cmd"} eq "ARRAY"
Truth table0%0/3
A and B
A= exists $arg->{"cmd"}B= ref $arg->{"cmd"} eq "ARRAY"
| |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | $cmd = $arg->{ 'cmd' }; | |||||||||||||||||||||||||||
| 102 | 0 | ▶ | $stdin = $arg->{ 'stdin' } || $DEFAULT_STDIN; | ||||||||||||||||||||||||||
|
Condition0%0/3
$arg->{'stdin'} || $DEFAULT_STDIN
MC/DC0%0/2
$arg->{'stdin'} || $DEFAULT_STDIN
$arg->{'stdin'}
$DEFAULT_STDIN
Truth table0%0/3
A || B
A= $arg->{'stdin'}B= $DEFAULT_STDIN
| |||||||||||||||||||||||||||||
| 103 | 0 | ▶ | $stdout = $arg->{ 'stdout' } || $DEFAULT_STDOUT; | ||||||||||||||||||||||||||
|
Condition0%0/3
$arg->{'stdout'} || $DEFAULT_STDOUT
MC/DC0%0/2
$arg->{'stdout'} || $DEFAULT_STDOUT
$arg->{'stdout'}
$DEFAULT_STDOUT
Truth table0%0/3
A || B
A= $arg->{'stdout'}B= $DEFAULT_STDOUT
| |||||||||||||||||||||||||||||
| 104 | 0 | ▶ | $stderr = $arg->{ 'stderr' } || $DEFAULT_STDERR; | ||||||||||||||||||||||||||
|
Condition0%0/3
$arg->{'stderr'} || $DEFAULT_STDERR
MC/DC0%0/2
$arg->{'stderr'} || $DEFAULT_STDERR
$arg->{'stderr'}
$DEFAULT_STDERR
Truth table0%0/3
A || B
A= $arg->{'stderr'}B= $DEFAULT_STDERR
| |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | $options = $arg->{ 'options' } || {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$arg->{'options'} || {}
MC/DC0%0/1
$arg->{'options'} || {}
$arg->{'options'}
Truth table0%0/2
A
A= $arg->{'options'}
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | chomp_err( $arg->{ 'CHOMP_ERR' } ) | ||||||||||||||||||||||||||||
| 108 | 0 | ▶ | if exists $arg->{ 'CHOMP_ERR' }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | chomp_out( $arg->{ 'CHOMP_OUT' } ) | ||||||||||||||||||||||||||||
| 111 | 0 | ▶ | if exists $arg->{ 'CHOMP_OUT' }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | croak_on_err( $arg->{ 'CROAK_ON_ERR' } ) | ||||||||||||||||||||||||||||
| 114 | 0 | ▶ | if exists $arg->{ 'CROAK_ON_ERR' }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | } else { | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 1 | croak "Expecting either an array ref or a hash ref"; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 3 | my $t0 = [ gettimeofday() ]; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 3 | ▶ | if ( exists $ENV{ DEBUG_IPCR3S_CALL } ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 1 | $stdout = $cmd; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | } elsif ( $TEE_SYSTEMCALL ) { | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # If you run 'perl -M-indirect -c thispackage' you will see a warning | ||||||||||||||||||||||||||||
| 131 | # about this line. This shouldn't be a problem because, hopefully, | ||||||||||||||||||||||||||||
| 132 | # execution will never get here if Capture::Tiny isn't available. | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 0 | ▶ | ( $stdout, $stderr ) = tee { IPC::Run3::run3( $cmd, $stdin, undef, undef, $options ) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | } else { | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 2 | IPC::Run3::run3( $cmd, $stdin, $stdout, $stderr, $options ); | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 3 | my $time = tv_interval( $t0 ); | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 3 | my $syserr = $?; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 3 | ▶ | croak $$stderr | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$CROAK_ON_ERR and $$stderr ne ""
MC/DC0%0/2
$CROAK_ON_ERR and $$stderr ne ""
$CROAK_ON_ERR
$$stderr ne ""
Truth table33%1/3
A and B
A= $CROAK_ON_ERRB= $$stderr ne ""
| |||||||||||||||||||||||||||||
| 147 | if $CROAK_ON_ERR && $$stderr ne ''; | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 3 | ▶ | if ( ref $stdout eq 'SCALAR' ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 2 | $stdout = $$stdout; | |||||||||||||||||||||||||||
| 152 | 2 | ▶ | chomp $stdout if $CHOMP_OUT; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | ▶ | } elsif ( ref $stdout eq 'ARRAY' && $CHOMP_OUT ) { | |||||||||||||||||||||||||||
|
Condition33%1/3
ref $stdout eq "ARRAY" and $CHOMP_OUT
MC/DC0%0/2
ref $stdout eq "ARRAY" and $CHOMP_OUT
ref $stdout eq "ARRAY"
$CHOMP_OUT
Truth table33%1/3
A and B
A= ref $stdout eq "ARRAY"B= $CHOMP_OUT
| |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 1 | chomp @$stdout; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 3 | ▶ | if ( ref $stderr eq 'SCALAR' ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 3 | $stderr = $$stderr; | |||||||||||||||||||||||||||
| 163 | 3 | ▶ | chomp $stderr if $CHOMP_OUT; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | ▶ | } elsif ( ref $stderr eq 'ARRAY' && $CHOMP_OUT ) { | |||||||||||||||||||||||||||
|
Condition0%0/3
ref $stderr eq "ARRAY" and $CHOMP_OUT
MC/DC0%0/2
ref $stderr eq "ARRAY" and $CHOMP_OUT
ref $stderr eq "ARRAY"
$CHOMP_OUT
Truth table0%0/3
A and B
A= ref $stderr eq "ARRAY"B= $CHOMP_OUT
| |||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 0 | chomp @$stderr; | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 3 | ▶ | return ( $stdout, $stderr, $syserr, $time ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | if $return_array; | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | } ## end sub run3 | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 4 | 1; | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | __END__ | ||||||||||||||||||||||||||||