> Interestingly, it gets stuck in the while loop, but never
> actually prints anything out from the pipe.  I had initially
> thought this was because I should be calling IO::Handle->getln()
> to read from the pipe, but for some reason that refuses to work
> under any circumstances with the POE pipe (which AFAIK is a
> IO::Socket::INET, derived from IO::Handle where getline() lives)
>
> To add insult to injury, if I replace the while loop with this
> code, I actually do end up with the first line of output being printed:
>
> #-------snip-----------
>       #this one actually seems to work (of course, only for the
> first line)
>       if ($sel->can_read()){
>               print <$a_read>;
>       }
> #-------snip-----------

This is an excerpt from "Network Programming with Perl" by Lincoln Stein
(the author of CGI.pm), reprinted w/o permission:

---snip---

    When using C<select()> to multiplex I/O, you must be extremely careful
I<not> to mix it with functions that use stdio buffering.  The reason is
that the stdio library maintains its own I/O buffers independent of the
buffrs used by the operating system.  C<select()> doesn't know about there
buffers; as a result it may indicate there's no more data to be read from a
filehandle, when in fact there is data remaining in the buffer.  This will
lead to confusion and frustration.
    In multiplexed programs you should avoid the <> operator, C<print()>,
and C<read()>, as well as the built-in C<getline()> function and the
C<IO::Handle->getline()> method.  Instead, you should use the low-level
C<sysread()> and C<syswrite()> calls exclusively.  This makes it difficult
to do line-oriented I/O.  However, Chapter 13 (Nonblocking I/O) develops
some modules that overcome this limitation.

---snip---

I *strongly* recommend this book.  It clearly and carefully explains the
pitfalls and workarounds to all sorts of I/O, socket, multiplexing, and
daemon-izing issues, and paid for itself in the first couple of days, for
me.

But in the meantime, it's clear from the above excerpt that your listed code
walks right into several of these potential problems.

If you're interested, the source for the modules developed in the book now
live on CPAN, as well as at http://www.modperl.com/perl_networking.
Specifically, look at IO::SessionData/IO::SessionSet as possible
replacements to your IO::Select/POE::Pipe objects.

HTH!

L8r,
Rob

Reply via email to