=head3 safe_pipe safe_pipe [ options, ] command, input my $results = safe_pipe [ 'command', 'arg' ], @input; my @results = safe_pipe [ 'command', 'arg' ], @input; my $results = safe_pipe { capture_err => 1 }, [ 'command', 'arg' ], @input; Pipe data to a shell command safely (without touching a command line) and retrieve the results. Options: =over 4 =item capture_err If true, STDERR will also be captured and included in returned results. =back =cut sub safe_pipe { my $opt = {}; $opt = shift if 'HASH' eq ref($_[0]); my $command = shift; my ($RESULT, @x); $command = [$command] unless ref $command; if (open $RESULT, "-|") { # original process: receiver (reads $RESULT) @x = <$RESULT>; } else { if (open STDIN, "-|") { # child: processor (reads STDIN, writes STDOUT) open STDERR, ">&STDOUT" or die "Can't dup STDOUT: $!" if $$opt{capture_err}; exec { $$command[0] } @$command; die "Cannot exec @$command: $!"; } else { # grandchild: sender (writes STDOUT) print @_; exit 0; } } wantarray ? @x : join('', @x); }