perlfunc
gods
<P>
seek - reposition file pointer for random-access
<FONT SIZE=-1>I/O</FONT>
<P>
<HR>
<P>
seek
<FONT SIZE=-1>FILEHANDLE,POSITION,WHENCE</FONT>
<P>
<HR>
<P>
Sets FILEHANDLE's position, just like the <CODE>fseek()</CODE> call of <CODE>stdio()</CODE>.
<FONT SIZE=-1>FILEHANDLE</FONT> may be an expression whose value gives the name of the filehandle. The values for
<FONT SIZE=-1>WHENCE</FONT> are
<CODE>0</CODE> to set the new position to
<FONT SIZE=-1>POSITION,</FONT> <CODE>1</CODE> to set it to the current position plus
<FONT SIZE=-1>POSITION,</FONT> and <CODE>2</CODE> to set it to
<FONT SIZE=-1>EOF</FONT> plus
<FONT SIZE=-1>POSITION</FONT> (typically negative). For
<FONT SIZE=-1>WHENCE</FONT> you may use the constants
<CODE>SEEK_SET</CODE>, <CODE>SEEK_CUR</CODE>, and <CODE>SEEK_END</CODE> from either the
<CODE>IO::Seekable</CODE> or the
<FONT SIZE=-1>POSIX</FONT> module. Returns <CODE>1</CODE> upon success, <CODE>0</CODE> otherwise.
<P>
If you want to position file for [perlfunc:sysread|sysread()] or [perlfunc:syswrite|syswrite()], don't use
[perlfunc:seek|seek()] -- buffering makes its effect on the file's system position unpredictable
and non-portable. Use <CODE>sysseek()</CODE> instead.
<P>
On some systems you have to do a seek whenever you switch between reading and writing. Amongst other things, this may have the effect of calling stdio's
<CODE>clearerr(3).</CODE>
<FONT SIZE=-1>A</FONT>
<FONT SIZE=-1>WHENCE</FONT> of
<CODE>1</CODE> (<CODE>SEEK_CUR</CODE>) is useful for not moving the file position:
<P>
<PRE> seek(TEST,0,1);
</PRE>
<P>
This is also useful for applications emulating <CODE>tail -f</CODE>. Once you hit
<FONT SIZE=-1>EOF</FONT> on your read, and then sleep for a while, you might have to stick in a
<CODE>seek()</CODE> to reset things. The
[perlfunc:seek|seek()] doesn't change the current position, but it <EM>does</EM> clear the end-of-file condition on the handle, so that the next <CODE><FILE></CODE> makes Perl try again to read something. We hope.
<P>
If that doesn't work (some stdios are particularly cantankerous), then you
may need something more like this:
<P>
<PRE> for (;;) {
for ($curpos = tell(FILE); $_ = <FILE>;
$curpos = tell(FILE)) {
# search for some stuff and put it into files
}
sleep($for_a_while);
seek(FILE, $curpos, 0);
}
</PRE>
<HR>