Flame has asked for the wisdom of the Perl Monks concerning the following question:

I've been trying to figure out what the IO::Handle->blocking() method does for the past hour or so, and for lack of any real leads. I've about given up. Can someone elighten me as to what it does and why/when to use it?



My code doesn't have bugs, it just develops random features.

Flame ~ Lead Programmer: GMS-SF | GMS-UOE

Replies are listed 'Best First'.
Re: IO::File->blocking
by graff (Chancellor) on Aug 09, 2002 at 05:07 UTC
    Well, based on looking at the man page for IO::File, I'd say that what it does is to control whether the file handle involved will perform "blocking i/o" or "non-blocking i/o". Are you looking for an explanation about what this means? If so, you could start by looking at Tk::fileevent...
    ... Event-driven I/O works best for filehandles that have been placed into nonblocking mode. In blocking mode, a "print" command may block if you give it more data than the under­ lying file or device can accept, and a "<>", "sysread" or "read" command will block if you attempt to read more data than is ready; no events will be processed while the com­ mands block. In nonblocking mode "print", "<>", "sysread" and "read" never block. See the documentation for the individual commands for information on how they handle blocking and nonblocking filehandles.
    Non-blocking i/o is totally cool for those situations when it really matters, like "I don't know when that %^&#!! user is gonna hit a key, but as soon as he does, BOY am I gonna whack him!"

    update: (A less vindictive application would be a case like a server that may be accepting connections and input data from any number of clients. Once it has one or more clients connected, it merely wants to try to read from each one, and if there's something to read, handle that; otherwise, don't just hang, waiting on a lagging client -- move on! See if there's another connection request, look for input from other clients, keep busy!)

      Ok, thanks, here's another question, I can't really experement since I'm forced (for now) to use a Win32-(W98) system with ActiveState. Can an IO-File object be flock'ed?



      My code doesn't have bugs, it just develops random features.

      Flame ~ Lead Programmer: GMS | GMS

        In case you haven't done a super-search for "flock", you can check this thread, which has relevant info in some of the later replies, as well as a link to a useful article from the Feb.2002 Perl Journal (Issue #23).

        In short, flock is not available on Windows 9x systems, but is available on NT, XP, 2000. However, the TPJ article contains some code that you can use to create semaphore files, which can serve to accomplish the same thing as flock.