multiplatform pipe open?

  • Thread starter Sebastian Yepes ESN
  • Start date
S

Sebastian Yepes ESN

Hi all,

I'm starting with Ruby and i am trying to migrate some Perl scripts to
Ruby, but cant get pipes with open to work like in perl.

What's the Right way to make open "pipes, stdin,stdout" work in all OS
platforms, with no external gems?

I have tried the 'fh = IO.popen("/bin/console_programe","w+")', but
don't understand why i need to use the method "close_write".



Thanks for any help.


Perl script:
---------
&cons_connect;
&cons_register;
&cons_listener;

sub cons_connect {
local (*CONS_READ, *CONS_WRITE);
open2(\*CONS_READ, \*CONS_WRITE, "/bin/console_programe");
my $line = <CONS_READ>;

if($line =~ /xml/){ print "CONNECTED CONNECT TO CONS"
}else{ print "CANNOT CONNECT TO CONS"; }
}

sub cons_register {
my(@SERVICES) = @_;

print CONS_WRITE "<Register>\n";
foreach my $service ( @SERVICES ){
print CONS_WRITE "<Cond>\n$service\n</Cond>\n";
}
print CONS_WRITE "</Register>\n";
my $line = <CONS_READ>;

if($line =~ /<OK\/>\n/){ print "REGISTRATION: OK";
}else{ print "REGISTRATION: NOK"; }
}

sub cons_listener {
while ($line = <CONS_READ>){
# Processing the...
}
}
 
B

Brian Candler

Sebastian said:
Hi all,

I'm starting with Ruby and i am trying to migrate some Perl scripts to
Ruby, but cant get pipes with open to work like in perl.

What's the Right way to make open "pipes, stdin,stdout" work in all OS
platforms, with no external gems?

I have tried the 'fh = IO.popen("/bin/console_programe","w+")', but
don't understand why i need to use the method "close_write".

You don't show your Ruby code, but maybe you just need fh.flush after
the last write, or set fh.sync = true after opening the pipe.

That is: the output data may be being buffered at the Ruby side. A
close_write would certainly flush it, whilst allowing you to read the
response.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,787
Messages
2,569,630
Members
45,335
Latest member
Tommiesal

Latest Threads

Top