You're suffering from multiple diseases.

First of all, try to give us something that should work cut-and-paste if there was no error. This helps us concentrate on the problem you have instead of working out a new example from the scratch, and helps you have more answers.

Then, IO::Select says:

can_read ( [ TIMEOUT ] ) Return an array of handles that are ready for reading. "TIMEOUT" is the maximum amount of time to wait before returning an empty list, in seconds, possibly frac- tional. If "TIMEOUT" is not given and any handles are registered then the call will block.
So it doesn't return the lines which wait to be read, but the handles that have data and that you can read from.

Last, but not least, you're maybe suffering from buffering. You don't provide much information about SOCKET/SOC, but note that if it buffers you could wait a long time before something is output. Unless it's really some socket, in which case you'd not.

Here's a working example, you'll be able to adapt it to your case easily. Note that the child sleeps 5 seconds, then checks for data in handles and reads from them.

#!/usr/bin/perl use warnings; use strict; use IO::Select; my $pid = open(PIP, "|-"); if ($pid < 0) { die "open(): $!"; } elsif ($pid > 0) { print scalar localtime time, " - Parent: sending something to chil +d\n"; print PIP "Hi, son!\n"; print scalar localtime time, " - Parent: waiting for child to fini +sh\n"; close PIP; print scalar localtime time, " - Parent: exiting\n"; } else { my $selector = IO::Select->new(\*STDIN); print scalar localtime time, " - Child: just born!\n"; sleep(60); my @handles = $selector->can_read(0); if (@handles) { print scalar localtime time, " - Child: there are " . scalar(@ +handles) . " handles ready for reading\n"; # It can only be STDIN... my $line = <STDIN>; chomp($line); print scalar localtime time, " - Child: parent says: [$line]\n +"; } print scalar localtime time, " - Child: exiting\n"; exit(0); } __END__ Sun May 22 15:43:48 2005 - Parent: sending something to child Sun May 22 15:43:48 2005 - Child: just born! Sun May 22 15:43:48 2005 - Parent: waiting for child to finish Sun May 22 15:44:48 2005 - Child: there are 1 handles ready for readin +g Sun May 22 15:44:48 2005 - Child: exiting Sun May 22 15:44:48 2005 - Parent: exiting
As a final note, always check the return status of open, it can fail!

Flavio (perl -e 'print(scalar(reverse("\nti.xittelop\@oivalf")))')

Don't fool yourself.

In reply to Re: forked kid can't read from IO::Select->can_read by polettix
in thread forked kid can't read from IO::Select->can_read by Anonymous Monk

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.