Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Here's an idiom to spawn a subprocess and capture its output, without fear of
- # being attacked by shell metachars
- # (Perl's backtick operator spawns a shell)
- sub safeticks {
- my $retval = '';
- my $line;
- my $pid;
- die "Can't fork: $!" unless defined($pid = open(KID, "-|"));
- if ($pid) { # parent
- while (defined($line = <KID>)) {
- $retval .= $line;
- }
- close KID;
- }
- else {
- exec @_;
- }
- return $retval;
- }
- # Here's a remarkably similar technique from git 1.7.7 source. It is better
- # in some ways and in some ways worse. I should probably combine them.
- sub safe_pipe_capture {
- my @output;
- if (my $pid = open my $child, '-|') {
- @output = (<$child>);
- close $child or die join(' ',@_).": $! $?";
- } else {
- exec(@_) or die "$! $?"; # exec() can fail the executable can't be found
- }
- return wantarray ? @output : join('',@output);
- }
Add Comment
Please, Sign In to add comment