Popen, open3 problems

Hi,

I think this problem is surely solved but hven’t found anything on the
net:

I get two programs:

  1. a ruby (master)
  2. a perl (slave)

I want the master to set commands to the slave and wait for the answer
(repeatedly). So a conversation could look like this:

m: set_var
m: v=8
s: set
m: get_var
m: v
s: 8

The problem is that ruby refuses to write to stdin of the slave until I
close the pipe. But I want to use the pipe repeatedly. Did I miss
something about IPC?

Thanks

My curent progs look like this:

require ‘fcntl’
require ‘open3’

pin, pout, perr = Open3.popen3(‘perl test.pl’)
pin.fcntl(Fcntl::F_SETFL, Fcntl::O_NONBLOCK)
pout.fcntl(Fcntl::F_SETFL, Fcntl::O_NONBLOCK)
perr.fcntl(Fcntl::F_SETFL, Fcntl::O_NONBLOCK)

pin.syswrite “set_var\n”
pin.syswrite “v=8\n”
#pin.close_write #<----------don’t want to do this but have to!
result=pout.readline
puts result
#pin.close
pout.close
perr.close


perl slave:

while ($line = ) { #first line is the command
chomp($line);
if ($line eq “set_var”) { &set_var;}
elsif ($line eq “quit”) { exit 0;}
}

sub set_var {
$var=; #second…lines are the params; get the var
chomp($var);
print “set.”; #return a confirmation
}

On Fri, 17 Nov 2006, Peter S. wrote:

something about IPC?
man stdio
/buffer

My curent progs look like this:

require ‘fcntl’
require ‘open3’

 harp:~ > cat a.rb
 require 'open3'

 Open3.popen3 "perl a.pl" do |i,o,e|
   i.puts 'set_var'
   i.puts 'v=8'
   i.flush                              #   <<---

   puts o.gets
 end


 harp:~ > cat a.pl

 $| = 1;                                #   <<---

 while ($line = <STDIN>) {
   chomp($line);
   if ($line eq "set_var") { &set_var;}
   elsif ($line eq "quit") { exit 0;}
 }

 sub set_var { $var=<STDIN>; chomp($var); print "set.$var\n"; }


 harp:~ > ruby a.rb
 set.v=8

-a

Peter S. wrote:

/ …

pin.syswrite “set_var\n”
pin.syswrite “v=8\n”
#pin.close_write #<----------don’t want to do this but have to!

Have you tried “pin.flush” ?

[email protected] wrote:

  i.puts 'v=8'
  i.flush                              #   <<---

ok. i don’t need this if I use syswrite (flush flushes only ruby
buffers)

  puts o.gets
end


harp:~ > cat a.pl

$| = 1;                                #   <<---

yes! that’s it. I knew there was some perl-guru-thing about it.

set.v=8

thanks very much.

Peter S. [email protected] wrote:

The problem is that ruby refuses to write to stdin of the slave until I
close the pipe. But I want to use the pipe repeatedly. Did I miss
something about IPC?

Are you sure it’s that Ruby didn’t send the data, and not that perl
didn’t answer yet?

I.e., have you tried setting $| on the perl side?
http://perl.plover.com/FAQs/Buffering.html

I love perl, it’s an awesome language, but unfortunately there are many
magic nooks and crannies.

lg, Bernd