I don't know why you're using select for this problem. If you were to use select, you'd have

use strict; use warnings; use IO::Handle qw( ); # For autoflush. use IO::Select qw( ); use IPC::Open3 qw( open3 ); use Symbol qw( gensym ); use constant BLK_SIZE => 64*1024; my $pid = open3( my $stdin_fh = gensym(), my $stdout_fh = gensym(), my $stderr_fh = gensym(), 'server.pl', ); $stdin_fh->autoflush(1); my $stdin_buf = ''; my $stdout_buf = ''; my $stderr_buf = ''; my $writers = IO::Select->new(); my $readers = IO::Select->new($stdout_fh, $stderr_fh); my %answers = ( "What is your name?" => 'ikegami', "What is your mission?" => 'To become the King of Halloween' +, "What is your favorite color?" => 'orange', ); while ($readers->count()) { my ($ready_readers, $ready_writers) = IO::Select::select($readers, +$writers, undef); foreach my $fh (@$ready_readers) { if ($fh == $stdout_fh) { my $rv = sysread($fh, $stdout_buf, BLK_SIZE, length($stdout_b +uf)); if (!defined($rv)) { $readers->remove($fh); # ...[ Do something in response to error from sysread. ].. +. } elsif (!$rv) { $readers->remove($fh); # ...[ Do something in response to EOF from sysread. ]... } else { while ($stdout_buf =~ s/(.*)\n//) { my $query = $1; if ($query =~ /Have a nice day!\z/) { $readers->remove($fh); print("$query\n"); } elsif ($answers{$query}) { $stdin_buf .= "$answers{$query}\n"; $writers->add($stdin_fh); } else { # ...[ Do something with unrecognised output from ST +DIN. ]... } } } } if ($fh == $stderr_fh) { my $rv = sysread($fh, $stderr_buf, BLK_SIZE, length($stderr_b +uf)); if (!defined($rv)) { $readers->remove($fh); # ...[ Do something in response to error from sysread. ].. +. } elsif (!$rv) { $readers->remove($fh); # ...[ Do something in response to EOF from sysread. ]... } else { # ...[ Do something in response to output to STDERR. ]... } } } foreach my $fh (@$ready_writers) { if (length($stdin_buf)) { # It's only safe to send one bytes unless you # somehow query the system to find otherwise. print($stdin_fh substr($stdin_buf, 0, 1, '')); # ...[ Do something if print returned an error. ]... } if (!length($stdin_buf)) { $writers->remove($fh); } } } waitpid($pid, 0); # ...[ Do something if waitpid returned an error. ]...

In reply to Re: Weirdness with IO::Select and IPC::Open3 by ikegami
in thread Weirdness with IO::Select and IPC::Open3 by rastoboy

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.