Several of the perl IPC::Open2 examples seem incomplete, including perldoc (which waitpid's on a process
with open file descriptors) and a gnuplot example (which assumes gnuplot exit'd).
Below is a complete perl IPC::Open2 example that interfaces with mysql (yes, it's contrived; you should be using DBI ;).
sub mysql($) {
my ($argv) = @_;
my $cmd = sprintf "mysql --batch --raw -A -u %s --password=%s -h %s %s",
$argv->{'user'}, $argv->{'password'}, $argv->{'host'}, $argv->{'db'};
my ($child_stdin, $child_stdout);
my $pid = IPC::Open2::open2($child_stdout, $child_stdin, $cmd);
return sub {
my ($query) = @_;
print $child_stdin $query;
close $child_stdin;
my $discard_header = <$child_stdout>;
my @ret = map { s/\r?\n$//; $_ } <$child_stdout>;
close $child_stdout;
kill 9, $pid;
waitpid $pid, 0;
return \@ret;
};
}
my $two = mysql( { user=> 'root', ... } )->('select 1+1')->[0];