# Multiplatform pipe open?

**URL:** <https://www.ruby-forum.com/t/multiplatform-pipe-open/154216>\
**Category:** Ruby\
**Created:** [November 26, 2008, 2:21pm UTC](https://www.ruby-forum.com/t/multiplatform-pipe-open/154216 "2008-11-26T14:21:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![syepes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bc8723/32.png) [@syepes](https://www.ruby-forum.com/u/syepes)\
**Post date:** [November 26, 2008, 2:21pm UTC](https://www.ruby-forum.com/t/multiplatform-pipe-open/154216/1 "2008-11-26T14:21:39Z")

</div>

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 “\n”;  
foreach my $service ( @SERVICES ){  
print CONS\_WRITE “\n$service\n\n”;  
}  
print CONS\_WRITE “\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… } }

---

<div class="post-metadata">

**Author:** ![syepes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bc8723/32.png) [@syepes](https://www.ruby-forum.com/u/syepes)\
**Post date:** [November 26, 2008, 3:18pm UTC](https://www.ruby-forum.com/t/multiplatform-pipe-open/154216/2 "2008-11-26T15:18:54Z")

</div>

Sebastian (syepes) wrote:

> 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.
