http://qs1969.pair.com?node_id=1177816

StoneLeopard has asked for the wisdom of the Perl Monks concerning the following question:

Hello PerlMonks,

I'm trying to spawn a Perl process via an Expect loop. The idea is that the main program would be sending Perl code to the slave, and have it return stuff on STDOUT, which would be received, parsed and acted upon by the main program. I tried to implement this via Expect, but it seems to be deadlocking. Can someone point out what I'm doing wrong?

I'm using Perl v5.14.1 if that makes a difference.

Thanks much. Pankaj

use Expect; # create a pipe to slave Perl process my $exp = new Expect; $exp->raw_pty(1); $exp->exp_internal(1); $exp->spawn("/usr/bin/perl") or die "Couldn't start Perl: $!\n"; $exp->log_user(0); $exp->log_stdout(0); # attempting to unbuffer the slave Perl's stdout test_ipc ("select STDOUT; \$| = 1;\n"); test_ipc ("print \"Got this\\n\";\n"); # close the pipe $exp->hard_close(); sub test_ipc { my $line = shift @_; # send command to Perl $exp->send ($line); # may receive any number of lines in return (or none) my $done = 1; while ($done) { $exp->expect (1, [qr"\r\n" => sub { print STDOUT $exp->before(). "\n"; exp_ +continue; }] , [timeout => sub { $done = 0; print STDOUT "TIMED OUT\n"; +}] , [eof => sub { $done = 0; print STDOUT "DONE\n"; }] ); } }