tail -f in perl?
seek(GWFILE, 0, 1);
The statement seek doesn't change the current position, but it does clear the end-of-file condition on the handle, so that the next <GWFILE> makes Perl try again to read something.
If that doesn't work (it relies on features of your stdio implementation), then you need something more like this:
for (;;) {
for ($curpos = tell(GWFILE); <GWFILE>; $curpos = tell(GWFILE)) {
# search for some stuff and put it into files
}
# sleep for a while
seek(GWFILE, $curpos, 0); # seek to where we had been
}
If this still doesn't work, look into the
POSIX module.
POSIX defines the clearerr method, which can remove the end of file condition on a filehandle. The method: read until end of file, clearerr, read some more. Lather, rinse, repeat.
head2 How do
I dup a filehandle in Perl?
If you check open, you'll see that several of the ways to call open should do
the trick. For example:
open(LOG, ">>/tmp/logfile");
open(STDERR, ">&LOG");
Or even with a literal numeric descriptor:
$fd = $ENV{MHCONTEXTFD};
open(MHCONTEXT, "<&=$fd"); # like fdopen(3S)
Error checking has been left as an exercise for the reader.
head2 How do I close a file descriptor by number?
This should rarely be necessary, as the Perl close function is to be used for things that Perl opened itself, even if it was a dup of a numeric descriptor, as with MHCONTEXT above. But if you really have to, you may be able to do this:
require 'sys/syscall.ph';
$rc = syscall(&SYS_close, $fd + 0); # must force numeric
die "can't sysclose $fd: $!" unless $rc == -1;