[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

popen, open3 problems

Peter Schrammel

11/16/2006 11:28:00 PM

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 = <STDIN>) { #first line is the command
chomp($line);
if ($line eq "set_var") { &set_var;}
elsif ($line eq "quit") { exit 0;}
}

sub set_var {
$var=<STDIN>; #second...lines are the params; get the var
chomp($var);
print "set."; #return a confirmation
}
4 Answers

Bnerd[TM]

11/16/2006 11:42:00 PM

0

Peter Schrammel <peter.schrammel@gmx.de> 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/Buff...

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

lg, Bernd

Paul Lutus

11/16/2006 11:51:00 PM

0

Peter Schrammel 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" ?

--
Paul Lutus
http://www.ara...

Ara.T.Howard

11/16/2006 11:53:00 PM

0

Peter Schrammel

11/17/2006 7:57:00 AM

0

ara.t.howard@noaa.gov 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.


> 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

thanks very much.