timed reads from files

jdavidb on 2002-05-06T20:11:04

This probably belongs on perlmonks or a newgroup, but this site is my primary interface into the Perl community. I'm debating submitting this as an ask use Perl;.

I need to read from arbitrary filehandles with a timeout, i.e., timed_read($fh, 2) reads from the filehandle in $fh but if nothing is received in 2 seconds the call is interrupted and you return with an undef. Now, you're thinking $SIG{ALRM}, but I can't do that. I've already got alarms going for something else, and I might need resolution other than integral seconds.

Expect (the TCL version) comes with a timed-read command-line script that accomplishes this very nicely. I won't have expect on the machine I'm using, though, and besides, I want to do this in pure Perl. The way it's accomplished there is with the select() system call. I'm working to do the same thing in Perl, and it seems to sort of work.

What's the right way to be doing this? Is there a module for timed reads? Is there a FAQ entry somewhere I've missed? My version with select() doesn't work reliably at the moment, and I'm hoping to find someone else has already invented this wheel.


Expect.pm

djberg96 on 2002-05-06T20:21:15

Isn't Roland Giersig's Expect.pm module pure-Perl? I haven't looked at the internals too closely (though I did look once), so I'm not entirely sure. If so, I think that's what you need. The README file says he modeled it after Tcl Expect, and yes, you can set a timeout.

As for timed reads, is Time::HiRes what you're looking for?*

* both on CPAN