| line | stmt | bran | cond | sub | pod | time | code | 
| 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 | 3 |  |  | 3 |  | 1514184 | use strict; | 
|  | 3 |  |  |  |  | 6 |  | 
|  | 3 |  |  |  |  | 113 |  | 
| 6 | 3 |  |  | 3 |  | 18 | use warnings; | 
|  | 3 |  |  |  |  | 7 |  | 
|  | 3 |  |  |  |  | 86 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 3 |  |  | 3 |  | 17 | use Carp; | 
|  | 3 |  |  |  |  | 8 |  | 
|  | 3 |  |  |  |  | 198 |  | 
| 9 | 3 |  |  | 3 |  | 3111 | use IPC::Run3 (); | 
|  | 3 |  |  |  |  | 145115 |  | 
|  | 3 |  |  |  |  | 83 |  | 
| 10 | 3 |  |  | 3 |  | 34 | use Exporter 'import'; | 
|  | 3 |  |  |  |  | 7 |  | 
|  | 3 |  |  |  |  | 1118 |  | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | our $VERSION = '0.011'; # VERSION | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | our @EXPORT = qw( run3 ); | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | 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 |  |  |  |  |  |  | our %EXPORT_TAGS = ( 'all' => [ @EXPORT, @EXPORT_OK ] ); | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | our $CHOMP_ERR      = 1; | 
| 26 |  |  |  |  |  |  | our $CHOMP_OUT      = 1; | 
| 27 |  |  |  |  |  |  | our $CROAK_ON_ERR   = 0; | 
| 28 |  |  |  |  |  |  | our $DEFAULT_STDIN  = undef; | 
| 29 |  |  |  |  |  |  | our $DEFAULT_STDOUT = \my $out; | 
| 30 |  |  |  |  |  |  | our $DEFAULT_STDERR = \my $err; | 
| 31 |  |  |  |  |  |  | our $TEE_SYSTEMCALL = 0; | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | BEGIN { | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | # Is Capture::Tiny available? | 
| 36 |  |  |  |  |  |  |  | 
| 37 | 3 | 50 |  | 3 |  | 5 | if ( eval { require Capture::Tiny } ) { | 
|  | 3 |  |  |  |  | 3109 |  | 
| 38 |  |  |  |  |  |  |  | 
| 39 | 3 |  |  |  |  | 19887 | Capture::Tiny->import( 'tee' ); | 
| 40 | 3 |  |  | 2 |  | 14 | *tee_systemcall = sub { $TEE_SYSTEMCALL = ! ! +shift }; | 
|  | 2 |  |  |  |  | 100 |  | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | } else { | 
| 43 |  |  |  |  |  |  |  | 
| 44 | 0 |  |  |  |  | 0 | *tee_systemcall = sub { $TEE_SYSTEMCALL = 0 }; | 
|  | 0 |  |  |  |  | 0 |  | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | } | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | # Is Time::HiRes available? | 
| 49 |  |  |  |  |  |  |  | 
| 50 | 3 | 50 |  |  |  | 11 | if ( eval { require Time::HiRes } ) { | 
|  | 3 |  |  |  |  | 27 |  | 
| 51 |  |  |  |  |  |  |  | 
| 52 | 3 |  |  |  |  | 14 | Time::HiRes->import( qw( gettimeofday tv_interval ) ); | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | } else { | 
| 55 |  |  |  |  |  |  |  | 
| 56 | 0 |  |  |  |  | 0 | *gettimeofday = sub { time, 0 }; | 
|  | 0 |  |  |  |  | 0 |  | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | *tv_interval = sub { | 
| 59 | 0 |  |  |  |  | 0 | my ( $t0, $t1 ) = @_; | 
| 60 | 0 | 0 |  |  |  | 0 | $t1 = [ gettimeofday() ] unless defined $t1; | 
| 61 | 0 |  |  |  |  | 0 | $t1->[ 0 ] - $t0->[ 0 ]; | 
| 62 | 0 |  |  |  |  | 0 | }; | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | } | 
| 65 |  |  |  |  |  |  | } | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | # '!! +shift' forces the value to be either undef or 1; | 
| 69 |  |  |  |  |  |  |  | 
| 70 | 2 |  |  | 2 | 1 | 24818 | sub chomp_err      { $CHOMP_ERR      = ! ! +shift } | 
| 71 | 2 |  |  | 2 | 1 | 106 | sub chomp_out      { $CHOMP_OUT      = ! ! +shift } | 
| 72 | 2 |  |  | 2 | 1 | 106 | sub croak_on_err   { $CROAK_ON_ERR   = ! ! +shift } | 
| 73 | 2 |  |  | 2 | 1 | 101 | sub default_stderr { $DEFAULT_STDERR = shift } | 
| 74 | 2 |  |  | 2 | 1 | 100 | sub default_stdin  { $DEFAULT_STDIN  = shift } | 
| 75 | 2 |  |  | 2 | 1 | 99 | sub default_stdout { $DEFAULT_STDOUT = shift } | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | sub run3 { | 
| 79 |  |  |  |  |  |  |  | 
| 80 | 4 |  |  | 4 | 1 | 9169 | my $arg = shift; | 
| 81 | 4 |  |  |  |  | 20 | my $ref = ref $arg; | 
| 82 |  |  |  |  |  |  |  | 
| 83 | 4 |  |  |  |  | 8 | my $return_array = 0; | 
| 84 |  |  |  |  |  |  |  | 
| 85 | 4 |  |  |  |  | 8 | my ( $cmd, $stdin, $stdout, $stderr, $options ); | 
| 86 |  |  |  |  |  |  |  | 
| 87 | 4 | 100 |  |  |  | 15 | if ( $ref eq 'ARRAY' ) { | 
|  |  | 50 |  |  |  |  |  | 
| 88 |  |  |  |  |  |  |  | 
| 89 | 3 |  |  |  |  | 7 | $return_array++; | 
| 90 | 3 |  |  |  |  | 8 | $cmd     = $arg; | 
| 91 | 3 |  |  |  |  | 7 | $stdin   = $DEFAULT_STDIN; | 
| 92 | 3 |  |  |  |  | 5 | $stdout  = $DEFAULT_STDOUT; | 
| 93 | 3 |  |  |  |  | 6 | $stderr  = $DEFAULT_STDERR; | 
| 94 | 3 |  |  |  |  | 12 | $options = {}; | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | } elsif ( $ref eq 'HASH' ) { | 
| 97 |  |  |  |  |  |  |  | 
| 98 | 0 | 0 | 0 |  |  | 0 | croak "'cmd' required and must be a reference to an array" | 
| 99 |  |  |  |  |  |  | unless exists $arg->{ 'cmd' } && ref $arg->{ 'cmd' } eq 'ARRAY'; | 
| 100 |  |  |  |  |  |  |  | 
| 101 | 0 |  |  |  |  | 0 | $cmd     = $arg->{ 'cmd' }; | 
| 102 | 0 |  | 0 |  |  | 0 | $stdin   = $arg->{ 'stdin' } || $DEFAULT_STDIN; | 
| 103 | 0 |  | 0 |  |  | 0 | $stdout  = $arg->{ 'stdout' } || $DEFAULT_STDOUT; | 
| 104 | 0 |  | 0 |  |  | 0 | $stderr  = $arg->{ 'stderr' } || $DEFAULT_STDERR; | 
| 105 | 0 |  | 0 |  |  | 0 | $options = $arg->{ 'options' } || {}; | 
| 106 |  |  |  |  |  |  |  | 
| 107 | 0 | 0 |  |  |  | 0 | chomp_err( $arg->{ 'CHOMP_ERR' } ) | 
| 108 |  |  |  |  |  |  | if exists $arg->{ 'CHOMP_ERR' }; | 
| 109 |  |  |  |  |  |  |  | 
| 110 | 0 | 0 |  |  |  | 0 | chomp_out( $arg->{ 'CHOMP_OUT' } ) | 
| 111 |  |  |  |  |  |  | if exists $arg->{ 'CHOMP_OUT' }; | 
| 112 |  |  |  |  |  |  |  | 
| 113 | 0 | 0 |  |  |  | 0 | croak_on_err( $arg->{ 'CROAK_ON_ERR' } ) | 
| 114 |  |  |  |  |  |  | if exists $arg->{ 'CROAK_ON_ERR' }; | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | } else { | 
| 117 |  |  |  |  |  |  |  | 
| 118 | 1 |  |  |  |  | 62 | croak "Expecting either an array ref or a hash ref"; | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | } | 
| 121 |  |  |  |  |  |  |  | 
| 122 | 3 |  |  |  |  | 24 | my $t0 = [ gettimeofday() ]; | 
| 123 |  |  |  |  |  |  |  | 
| 124 | 3 | 100 |  |  |  | 19 | if ( exists $ENV{ DEBUG_IPCR3S_CALL } ) { | 
|  |  | 50 |  |  |  |  |  | 
| 125 |  |  |  |  |  |  |  | 
| 126 | 1 |  |  |  |  | 7 | $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 |  |  | 0 |  | 0 | ( $stdout, $stderr ) = tee { IPC::Run3::run3( $cmd, $stdin, undef, undef, $options ) }; | 
|  | 0 |  |  |  |  | 0 |  | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | } else { | 
| 137 |  |  |  |  |  |  |  | 
| 138 | 2 |  |  |  |  | 13 | IPC::Run3::run3( $cmd, $stdin, $stdout, $stderr, $options ); | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  | } | 
| 141 |  |  |  |  |  |  |  | 
| 142 | 3 |  |  |  |  | 35258 | my $time = tv_interval( $t0 ); | 
| 143 |  |  |  |  |  |  |  | 
| 144 | 3 |  |  |  |  | 93 | my $syserr = $?; | 
| 145 |  |  |  |  |  |  |  | 
| 146 | 3 | 50 | 33 |  |  | 123 | croak $$stderr | 
| 147 |  |  |  |  |  |  | if $CROAK_ON_ERR && $$stderr ne ''; | 
| 148 |  |  |  |  |  |  |  | 
| 149 | 3 | 100 | 33 |  |  | 49 | if ( ref $stdout eq 'SCALAR' ) { | 
|  |  | 50 |  |  |  |  |  | 
| 150 |  |  |  |  |  |  |  | 
| 151 | 2 |  |  |  |  | 17 | $stdout = $$stdout; | 
| 152 | 2 | 50 |  |  |  | 30 | chomp $stdout if $CHOMP_OUT; | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | } elsif ( ref $stdout eq 'ARRAY' && $CHOMP_OUT ) { | 
| 155 |  |  |  |  |  |  |  | 
| 156 | 1 |  |  |  |  | 6 | chomp @$stdout; | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  | } | 
| 159 |  |  |  |  |  |  |  | 
| 160 | 3 | 50 | 0 |  |  | 19 | if ( ref $stderr eq 'SCALAR' ) { | 
|  |  | 0 |  |  |  |  |  | 
| 161 |  |  |  |  |  |  |  | 
| 162 | 3 |  |  |  |  | 13 | $stderr = $$stderr; | 
| 163 | 3 | 50 |  |  |  | 26 | chomp $stderr if $CHOMP_OUT; | 
| 164 |  |  |  |  |  |  |  | 
| 165 |  |  |  |  |  |  | } elsif ( ref $stderr eq 'ARRAY' && $CHOMP_OUT ) { | 
| 166 |  |  |  |  |  |  |  | 
| 167 | 0 |  |  |  |  | 0 | chomp @$stderr; | 
| 168 |  |  |  |  |  |  |  | 
| 169 |  |  |  |  |  |  | } | 
| 170 |  |  |  |  |  |  |  | 
| 171 | 3 | 50 |  |  |  | 76 | return ( $stdout, $stderr, $syserr, $time ) | 
| 172 |  |  |  |  |  |  | if $return_array; | 
| 173 |  |  |  |  |  |  |  | 
| 174 |  |  |  |  |  |  | } ## end sub run3 | 
| 175 |  |  |  |  |  |  |  | 
| 176 |  |  |  |  |  |  | 1; | 
| 177 |  |  |  |  |  |  |  | 
| 178 |  |  |  |  |  |  |  | 
| 179 |  |  |  |  |  |  | __END__ |