Greetings Brothers, I've been banging my head against the wall trying to nail down good IPC technique for a couple months now, and just when I think it all makes sense I can't quite test my understandings successfully. In particular, my research indicates that it's essential basically to use IO::Select to find out if read and write handles are ready for reading and writing before using them. In particular I've studied some nice code here:

http://www.perlmonks.org/?node_id=151886

But what's happening when I try my own implementation (including *writing* to the child process) is IO::Select seems to let the write file handle sneak through as ready for reading! Here is my "server" code:

#!/usr/bin/perl print "What is your name?\n"; my $name = prompt_for_input(); print "What is your mission?\n"; my $mission = prompt_for_input(); print "What is your favorite color?\n"; my $color = prompt_for_input(); print "Your name is $name, your mission is $mission, and your favorite + color is $color. Have a nice day!\n"; sub prompt_for_input { local ($|) = (1); my $resp = <STDIN>; chomp $resp; return $resp; }
Simple enough, right? Here is my "client":
#!/usr/bin/perl use strict; use warnings; use diagnostics; use IPC::Open3; use IO::Select; my @answers = qw/galahad seekthegrail bluenored!/; my( $wtr, $rdr, $err ); use Symbol 'gensym'; $err = gensym; my $pid = open3( $wtr, $rdr, $err, 'server.pl' ); print "IN: $wtr OUT: $rdr ERR: $err\n"; print "PID was $pid\n"; my $sel = new IO::Select; $sel->add( $wtr, $rdr ); my $line; foreach my $readhandle ( $sel->can_read(1) ) { print "readhandle is $readhandle \n"; next if $readhandle != $rdr; print "past next\n"; my $len = sysread $readhandle, $line, 4096; if( not defined $len ) { die "Error from child: $!\n"; } print $line; write_answers(); } waitpid( $pid, 0 ); sub write_answers { foreach my $writehandle ( $sel->can_write ) { print "write handle is $writehandle\n"; return if $writehandle != $wtr; my $answer = shift @answers; print "writing $answer\n"; print $writehandle $answer . "\n"; } }
So what happens is very strange:
rasto@frodo:~/work$ ./client.pl IN: GLOB(0x880fe78) OUT: GLOB(0x880fe88) ERR: GLOB(0x880fe28) PID was 6834 readhandle is GLOB(0x880fe78) readhandle is GLOB(0x880fe88) past next write handle is GLOB(0x880fe78) writing galahad rasto@frodo:~/work$
So there are two things happening here that I don't understand. 1. Why did IO::Select brings GLOB(0x880fe78) up as ready for reading at all, when it was opened as the write (input) filehandle? To paraphrase Basil Fawlty, "What's the point of the bl**dy thing if it doesn't work when you really need it?" lol.

And 2. I don't understand why the program terminates after writing the answer to the first question. I suspect the two things are interrelated, and I feel like I'm *so close* to really understanding how this process is supposed to work--any input on understanding this would be greatly appreciated!


In reply to 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.