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

Hi Monks,

Can I make the sysread function timeout, or is there an alternate function for that?

$ret = sysread($sock, $buf, 100);

Replies are listed 'Best First'.
Re: How to make sysread timeout
by duff (Parson) on Nov 01, 2007 at 18:33 UTC

    Typically you would use select (if you follow the link, you want the 4-arg version of select) or IO::Select to handle the timeout.

Re: How to make sysread timeout
by kyle (Abbot) on Nov 01, 2007 at 18:58 UTC

    The alarm documentation contains an example of how to use it to timeout sysread specifically.

      That alarm documentation gives a clear example, but it isn't working. Shouldn't the below code timeout every second?
      eval { $timeout = 1; local $SIG{ALRM} = sub { die "alarm\n" }; alarm $timeout; $ret = sysread($sock, $buf, 100); alarm 0; };

        What platform are you on (you haven't specified)? Windows in particular is not so good with signals. In that case, you might need to look at Re: Backticks and SIGALRM.