seek

seekFILEHANDLE,OFFSET,WHENCE
This function positions the file pointer for
FILEHANDLE, just like the
fseek(3) call of standard I/O. The first position
in a file is at offset 0, not offset 1. Also, offsets refer to byte
positions, not character positions or line numbers. In general, since
line lengths vary, it’s not possible to access a particular line number
without examining the whole file up to that point, unless all your lines
are known to be of a particular length, or you’ve built an index that
translates line numbers into byte offsets. (The same restrictions apply
to character positions in files with variable-length character encodings
like UTF-8 and UTF-16: the operating system doesn’t know what characters
are, only bytes.)
FILEHANDLE can be an expression whose
value gives either the name of the actual filehandle, a typeglob, or a
reference to anything resembling a filehandle object. The function
returns true on success, and false otherwise. For handiness, the
function can calculate offsets from various file positions for you. The
value of WHENCE specifies which file position
your OFFSET uses for its starting point:
0, the beginning of the file;
1, the current position in the file;
or 2, the end of the file. The
OFFSET can be negative for a
WHENCE of 1 or 2. If you’d like ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access