seek - Perldoc Browser (original) (raw)
Sets FILEHANDLE's position, just like the fseek(3) call of C stdio
. FILEHANDLE may be an expression whose value gives the name of the filehandle. The values for WHENCE are 0
to set the new position in bytes to POSITION; 1
to set it to the current position plus POSITION; and 2
to set it to EOF plus POSITION, typically negative. For WHENCE you may use the constants SEEK_SET
, SEEK_CUR
, and SEEK_END
(start of the file, current position, end of the file) from the Fcntl module. Returns 1
on success, false otherwise.
Note the emphasis on bytes: even if the filehandle has been set to operate on characters (for example using the :encoding(UTF-8)
I/O layer), the seek, tell, and sysseek family of functions use byte offsets, not character offsets, because seeking to a character offset would be very slow in a UTF-8 file.
If you want to position the file for sysread or syswrite, don't use seek, because buffering makes its effect on the file's read-write position unpredictable and non-portable. Use sysseek instead.
Due to the rules and rigors of ANSI C, 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 clearerr(3). A WHENCE of 1
(SEEK_CUR
) is useful for not moving the file position:
seek($fh, 0, 1);
This is also useful for applications emulating tail -f
. Once you hit EOF on your read and then sleep for a while, you (probably) have to stick in a dummy seek to reset things. The seek doesn't change the position, but it does clear the end-of-file condition on the handle, so that the next readline FILE
makes Perl try again to read something. (We hope.)
If that doesn't work (some I/O implementations are particularly cantankerous), you might need something like this:
for (;;) {
for ($curpos = tell($fh); <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mrow></mrow><mo>=</mo></msub><mi>r</mi><mi>e</mi><mi>a</mi><mi>d</mi><mi>l</mi><mi>i</mi><mi>n</mi><mi>e</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">_ = readline(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord"><span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1068em;"><span style="top:-2.55em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mrel mtight">=</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal">re</span><span class="mord mathnormal">a</span><span class="mord mathnormal">d</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">in</span><span class="mord mathnormal">e</span><span class="mopen">(</span></span></span></span>fh);
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>c</mi><mi>u</mi><mi>r</mi><mi>p</mi><mi>o</mi><mi>s</mi><mo>=</mo><mi>t</mi><mi>e</mi><mi>l</mi><mi>l</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">curpos = tell(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">c</span><span class="mord mathnormal">u</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">p</span><span class="mord mathnormal">os</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">t</span><span class="mord mathnormal">e</span><span class="mord mathnormal" style="margin-right:0.01968em;">ll</span><span class="mopen">(</span></span></span></span>fh)) {
# search for some stuff and put it into files
}
sleep($for_a_while);
seek($fh, $curpos, 0);
}