<?xml version="1.0" encoding="windows-1252"?>
<node id="300" title="perlfunc:seek" created="1999-08-24 18:43:03" updated="2005-08-15 15:44:50">
<type id="119">
perlfunc</type>
<author id="114">
gods</author>
<data>
<field name="doctext">
</field>
<field name="name">

&lt;P&gt;
seek - reposition file pointer for random-access 
&lt;FONT SIZE=-1&gt;I/O&lt;/FONT&gt;

&lt;P&gt;
&lt;HR&gt;
</field>
<field name="synopsis">

&lt;P&gt;
seek 
&lt;FONT SIZE=-1&gt;FILEHANDLE,POSITION,WHENCE&lt;/FONT&gt;

&lt;P&gt;
&lt;HR&gt;
</field>
<field name="description">

&lt;P&gt;
Sets FILEHANDLE's position, just like the &lt;CODE&gt;fseek()&lt;/CODE&gt; call of &lt;CODE&gt;stdio()&lt;/CODE&gt;. 
&lt;FONT SIZE=-1&gt;FILEHANDLE&lt;/FONT&gt; may be an expression whose value gives the name of the filehandle. The values for 
&lt;FONT SIZE=-1&gt;WHENCE&lt;/FONT&gt; are
 &lt;CODE&gt;0&lt;/CODE&gt; to set the new position to 
&lt;FONT SIZE=-1&gt;POSITION,&lt;/FONT&gt; &lt;CODE&gt;1&lt;/CODE&gt; to set it to the current position plus 
&lt;FONT SIZE=-1&gt;POSITION,&lt;/FONT&gt; and &lt;CODE&gt;2&lt;/CODE&gt; to set it to 
&lt;FONT SIZE=-1&gt;EOF&lt;/FONT&gt; plus 
&lt;FONT SIZE=-1&gt;POSITION&lt;/FONT&gt; (typically negative). For 
&lt;FONT SIZE=-1&gt;WHENCE&lt;/FONT&gt; you may use the constants
 &lt;CODE&gt;SEEK_SET&lt;/CODE&gt;, &lt;CODE&gt;SEEK_CUR&lt;/CODE&gt;, and &lt;CODE&gt;SEEK_END&lt;/CODE&gt; from either the
&lt;CODE&gt;IO::Seekable&lt;/CODE&gt; or the 
&lt;FONT SIZE=-1&gt;POSIX&lt;/FONT&gt; module. Returns &lt;CODE&gt;1&lt;/CODE&gt; upon success, &lt;CODE&gt;0&lt;/CODE&gt; otherwise.

&lt;P&gt;
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 &lt;CODE&gt;sysseek()&lt;/CODE&gt; instead.

&lt;P&gt;
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 
&lt;CODE&gt;clearerr(3).&lt;/CODE&gt; 
&lt;FONT SIZE=-1&gt;A&lt;/FONT&gt; 
&lt;FONT SIZE=-1&gt;WHENCE&lt;/FONT&gt; of
 &lt;CODE&gt;1&lt;/CODE&gt; (&lt;CODE&gt;SEEK_CUR&lt;/CODE&gt;) is useful for not moving the file position:

&lt;P&gt;
&lt;PRE&gt;    seek(TEST,0,1);
&lt;/PRE&gt;
&lt;P&gt;
This is also useful for applications emulating &lt;CODE&gt;tail -f&lt;/CODE&gt;. Once you hit 
&lt;FONT SIZE=-1&gt;EOF&lt;/FONT&gt; on your read, and then sleep for a while, you might have to stick in a 
&lt;CODE&gt;seek()&lt;/CODE&gt; to reset things. The
 [perlfunc:seek|seek()] doesn't change the current position, but it &lt;EM&gt;does&lt;/EM&gt; clear the end-of-file condition on the handle, so that the next &lt;CODE&gt;&amp;lt;FILE&amp;gt;&lt;/CODE&gt; makes Perl try again to read something. We hope.

&lt;P&gt;
If that doesn't work (some stdios are particularly cantankerous), then you
may need something more like this:

&lt;P&gt;
&lt;PRE&gt;    for (;;) {
        for ($curpos = tell(FILE); $_ = &amp;lt;FILE&amp;gt;;
             $curpos = tell(FILE)) {
            # search for some stuff and put it into files
        }
        sleep($for_a_while);
        seek(FILE, $curpos, 0);
    }
&lt;/PRE&gt;
&lt;HR&gt;
</field>
</data>
</node>
