Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

jdavidb (1361)

jdavidb
  (email not shown publicly)
http://voiceofjohn.blogspot.com/

J. David Blackstone has a Bachelor of Science in Computer Science and Engineering and nine years of experience at a wireless telecommunications company, where he learned Perl and never looked back. J. David has an advantage in that he works really hard, he has a passion for writing good software, and he knows many of the world's best Perl programmers.

Journal of jdavidb (1361)

Monday May 06, 2002
03:11 PM

timed reads from files

[ #4701 ]

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.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • 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