Markdown.pl is a Perl text-processing filter which takes input from stdin and
outputs to stdout. I'd like to call it as a child process from within my
script -- pass it a string and get a string back.
My first attempt is to use open2(), but after failing and reading perldoc
perlipc carefully I realized my problem is that, like most programs,
Markdown.pl does not support "unbuffered" I/O. Advice in perldoc perlipc
suggests using 'Expect' module.
So, I try this code:
#!/usr/bin/perl
use Expect ;
my $stdin = "*Hello* **World**\n" ;
my $cmd = '/Users/jk/Downloads/Markdown_1.0.1/Markdown.pl';
my @parameters = () ; # No arguments to Markdown.pl
my $exp = new Expect;
$exp->raw_pty(1);
$exp->spawn($cmd, @parameters)
or die "Cannot spawn $command: $!\n";
$exp->send($stdin);
print "Waiting for output.\n" ;
my $stdout = $exp->expect(undef);
print "stdout: $stdout\n" ;
The result is "Waiting for output" -- forever. If I give the expect()
function a timeout parameter, then it times out after the given time, but
returns no data.
Does anyone know how to use the Expect module in the simple case of spawning
a command with stdin to get stdout?
Maybe this is not appropriate. Although perldoc peripc recommends Expect
for this problem, Expect documentation poo-poos such a usage:
Question: "I just want to read the output of a process without
expect()ing anything. How can I do this?"
Answer: "[ Are you sure you need Expect for this? How about qx() or
open("prog|")? ]
That's what we call "the runaround". Should I give up and write my stdin to
a temporary file? I know I can make that work but at this point curiosity
and the need for closure has taken over ;)
--
View this message in context: http://www.nabble.com/Can-use-Expect-to-spawn-a-process-with-stdin%2C-stdout--tp25346856p25346856.html
Sent from the Perl - Expectperl-Discuss mailing list archive at Nabble.com.
|