Seems fine to me, given that I cant see the all important read_line sub

Here's my version, with some important revisions

#!/usr/bin/perl -w use strict; use IO::Select; use IO::Pipe; use IO::Handle; my $debug = 1; my @connect = ("3","2","1"); my %msgs; @msgs{@connect} = qw/hooray hip hip/; my $select_ch = IO::Select->new(); foreach my $msg (@connect) { # for each write, fork a child if ($msg) { # fork and open child for reading my $pipe = IO::Pipe->new(); if (my $pid = fork() ) { #parent my $fh = $pipe->reader(); $debug && print "parent $$ forked $pid\n"; $fh->blocking(0); # set non-blocking I/O $debug && print "parent adding readable filehandle ($fh)\n"; $select_ch->add($fh); } else { #child my $childhandle = $pipe->writer(); my $mt = $msgs{$msg}; sleep $msg; print STDERR "$$ child $msg writing to pipe $msg, response $mt +\n"; print $childhandle "child $msg, response $mt"; exit 0; #kill the child process } } } # done with processing this read # here we read for responses from the filehandles my @readyfiles; #array for select to populate while ( @readyfiles = $select_ch->can_read() ) { foreach my $rh (@readyfiles) { # read from filehandle, send back string print "$$ calling IO::Handle::getline\n"; my $child_resp_string = $rh->getline(); $debug && print "$$ read |$child_resp_string|\n"; # only one read per handle, so close $debug && print "$$ removing @{[ref $rh]}\n"; $select_ch->remove($rh); $rh->close; $debug && print "$$ @{[$select_ch->count()]} handles left to read\ +n"; } } print "exiting...\n"; exit 0;

Platforms
Linux itdevtst 2.4.20-8 #1 Thu Mar 13 17:54:28 EST 2003 i686 i686 i386 GNU/Linux
SunOS robin 5.8 Generic sun4u sparc SUNW,Ultra-60

Perl versions
This is perl, v5.8.2 built for i686-linux
This is perl, v5.8.1 built for sun4-solaris

Output

le99007@robin:~/src/perl> ./pipe.pl parent 7146 forked 7147 parent adding readable filehandle (IO::Pipe::End=GLOB(0x195708)) parent 7146 forked 7148 parent adding readable filehandle (IO::Pipe::End=GLOB(0x1957a4)) parent 7146 forked 7149 parent adding readable filehandle (IO::Pipe::End=GLOB(0x151eb0)) 7149 child 1 writing to pipe 1, response hip 7146 calling IO::Handle::getline 7146 read |child 1, response hip| 7146 removing IO::Pipe::End 7146 2 handles left to read 7148 child 2 writing to pipe 2, response hip 7146 calling IO::Handle::getline 7146 read |child 2, response hip| 7146 removing IO::Pipe::End 7146 1 handles left to read 7147 child 3 writing to pipe 3, response hooray 7146 calling IO::Handle::getline 7146 read |child 3, response hooray| 7146 removing IO::Pipe::End 7146 0 handles left to read exiting...

Same result on the Linux box

+++++++++++++++++
#!/usr/bin/perl
use warnings;use strict;use brain;


In reply to Re: IO::Pipe and IO::Select by leriksen
in thread IO::Pipe and IO::Select by brinogordon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.