Cari di Perl 
    Perl User Manual
Daftar Isi
(Sebelumnya) Reset directory handleReposition directory pointer (Berikutnya)
Input and output functions

Reposition file pointer for random-access I/O

Daftar Isi

  • seek FILEHANDLE,POSITION,WHENCE

    Sets FILEHANDLE's position, just like the fseek call of stdio.FILEHANDLE may be an expression whose value gives the name of thefilehandle. The values for WHENCE are 0 to set the new positionin bytes to POSITION; 1 to set it to the current position plusPOSITION; and 2 to set it to EOF plus POSITION, typicallynegative. For WHENCE you may use the constants SEEK_SET,SEEK_CUR, and SEEK_END (start of the file, current position, endof the file) from the Fcntl module. Returns 1 on success, falseotherwise.

    Note the in bytes: even if the filehandle has been set tooperate on characters (for example by using the :encoding(utf8) openlayer), tell() will return byte offsets, not character offsets(because implementing that would render seek() and tell() rather slow).

    If you want to position the file for sysread or syswrite, don't useseek, because buffering makes its effect on the file's read-write positionunpredictable and non-portable. Use sysseek instead.

    Due to the rules and rigors of ANSI C, on some systems you have to do aseek whenever you switch between reading and writing. Amongst otherthings, 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:

    1. seek(TEST,0,1);

    This is also useful for applications emulating tail -f. Once you hitEOF on your read and then sleep for a while, you (probably) have to stick in adummy 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 thenext <FILE> makes Perl try again to read something. (We hope.)

    If that doesn't work (some I/O implementations are particularlycantankerous), you might need something like this:

    1. for (;;) {
    2. for ($curpos = tell(FILE); $_ = <FILE>;
    3. $curpos = tell(FILE)) {
    4. # search for some stuff and put it into files
    5. }
    6. sleep($for_a_while);
    7. seek(FILE, $curpos, 0);
    8. }
 
Source : perldoc.perl.org - Official documentation for the Perl programming language
Site maintained by Jon Allen (JJ)     See the project page for more details
Documentation maintained by the Perl 5 Porters
(Sebelumnya) Reset directory handleReposition directory pointer (Berikutnya)