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

Astonishingly enough, I have a module which needs to distinguish between sysread() and read() for a tied filehandle. This arises because the semantics for sysread() (block until a packet of data arrives on a socket) and read() (block until the desired length of data arrives) are slightly different, which complicates life immeasurably for those expecting the wrong behavior.

To make a long story short, printing a stack trace from the READ() method in the tied filehandle class does not allow one to tell whether the programmer did read(FOO...) or sysread(FOO...). I also can't use any hackish solutions like inserting a wrapper for sysread() in the programmer's namespace...this has to be a respectable module. Are there any clever solutions to this problem, or should I just request that a SYSREAD() method be added for tied filehandles in Perl 5.10?
  • Comment on Distinguishing sysread() and read() in tied handles

Replies are listed 'Best First'.
Re: Distinguishing sysread() and read() in tied handles
by salva (Canon) on Feb 26, 2006 at 09:12 UTC
    I can't find that difference in behaviour between read and sysread described on any perl docs so it's probably an implementation detail that shouldn't be taken for granted.
      The difference described in the perl docs is that Perl sysread() uses the read() system call, whereas Perl read() uses the fread() call or Perl's equivalent. In the majority of common UNIX systems, it is not an error for the read() system call to return fewer than the requested number of bytes (see the read(2) manpage) if not that many bytes have arrived yet. Unfortunately, several modules that will depend on the one I'm working on take advantage of this quirky behavior, and it's not in my power to change them.