|Think about Loose Coupling|
RE: Socket.pm nonblockingby ahunter (Monk)
|on Jun 10, 2000 at 20:23 UTC||Need Help??|
I presume you mean forking rather than folking <g>
I've written this sort of program quite a lot in the past - the call you want is select(), which will wait for data to arrive on a filehandle. You can also use IO::Select, but there is a major caveat - whatever you do, do *not* ever use the buffered IO functions, because madness lies that way. All the IO functions except sysread() and syswrite() are buffered. I've always been a bit dubious about what IO::Socket does, too, so I've always stuck with the standard Socket library and done things the hard way. I've actually written a module to deal with all this in a transparent way, and there are others available on CPAN.
The vital bit from that code is:
Hopefully you can decipher that... When you get data on a file descriptor, read one byte using sysread() to avoid blocking. If sysread() returns undef, you have an error, and if it returns 0, you have EOF.
In a chat room environment, it may be useful to have timeouts and things - the Time::HiRes library is useful for this. The last argument to select() is a timeout value, in decimal seconds. Use a function like this (UNTESTED!) to add timeouts:
And when you come to do the select, you call callbacks and work out the last value for select like so:
If a timer expires, $rout will be empty, and the callback will be called when the select() loop is next entered.
UPDATE: Tidied up the code, as I noticed it was
a bit ugly, and not very good perl in places.
Added some more helpful comments