Can select($rout,....) be made to block on EOF

marlius
marlius used Ask the Experts™
on
Hi experts,

My intention is to monitor multiple files for new data to read. For this I use select($rout,....) with a timeout of undef, which blocks until at least one file is ready for reading. Unfortunately, select($rout,....) also seems to return when a file has reached EOF. The following code, in which I was expecting select($rout,....) to block until new data is appended to the end of the file, returns immediately (with no data):

#####################
#!/usr/bin/perl -w

use strict;
use IO::File;
use IO::Select;

my $select = IO::Select->new();

my $fh = IO::File->new($ARGV[0], O_RDONLY | O_NONBLOCK);
sysseek($fh, 0, SEEK_END);         # See? I'm at end of file
$select->add($fh);

my @ready = $select->can_read(undef);
my $char;
$ready[0]->sysread($char, 1);     # non-blocking read
print "The next char is ", $char, "\n";

#####################

My question is, can select($rout,....) be made to block at EOF until new data appears? Or will I have to resort to polling?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
select is supposed to do that IMO.  So you might have to use other methods.

Author

Commented:
Hi inq123,

Thanks for your comment. For now, I've decided to forget about select($rout,....) and go with polling. However I am still interested in any comments other experts might want to add, so I won't be closing this question just yet.
There's something not quite right here.

If the file is seekable, you can't sensibly use select on it. As you've seen, the select ALWAYS returns immediately. There is no completely general way to set things up so your program can receive a notification when a file is extended. The most common way to deal with this is polling.

I believe there are some file systems and some operating systems (Windows?) that permit one to set up such a notification.

Author

Commented:
Hi inq123 and jmcg,

Thank you both for your comments.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial