You can use open
for this (to run the program /bin/some/program
with two command-line arguments):
open my $fh, "-|", "/bin/some/program", "cmdline_argument_1", "cmdline_argument_2";
while (my $line = readline($fh)) {
print "Program said: $line";
}
Reading from $fh
will give you the stdout of the program you're running.
The other way around works as well:
open my $fh, "|-", "/bin/some/program";
say $fh "Hello!";
This pipes everything you write on the filehandle to the stdin of the spawned process.
If you want to read and write to and from the same process, have a look at the IPC::Open3
and IPC::Cmd
modules.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…