rastoboy has asked for the wisdom of the Perl Monks concerning the following question:
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:
Simple enough, right? Here is my "client":#!/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; }
So what happens is very strange:#!/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 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.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$
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!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Weirdness with IO::Select and IPC::Open3
by ikegami (Patriarch) on Mar 21, 2011 at 02:00 UTC | |
by rastoboy (Monk) on Mar 22, 2011 at 01:41 UTC | |
by ikegami (Patriarch) on Mar 22, 2011 at 02:19 UTC | |
by rastoboy (Monk) on Mar 23, 2011 at 00:22 UTC | |
|
Re: Weirdness with IO::Select and IPC::Open3
by ikegami (Patriarch) on Mar 21, 2011 at 02:48 UTC | |
by rastoboy (Monk) on Mar 21, 2011 at 04:35 UTC | |
by ikegami (Patriarch) on Mar 21, 2011 at 04:40 UTC | |
|
Re: Weirdness with IO::Select and IPC::Open3
by zentara (Cardinal) on Mar 21, 2011 at 15:07 UTC |