perl-5.38.0
seek FILEHANDLE,POSITION,WHENCE

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.

C の stdio ライブラリの fseek(3) 関数のように、FILEHANDLE の ファイルポインタを任意の位置に設定します。 FILEHANDLE は、実際のファイルハンドル名を与える式でもかまいません。 WHENCE の値が、0 ならば、新しい位置を バイト単位で POSITION の位置へ 設定します; 1 ならば、現在位置から POSITION 加えた位置へ 設定します; 2 ならば、EOF からPOSITION だけ加えた位置へ、新しい位置を 設定します。 この値には、Fcntl モジュールで使われている SEEK_SETSEEK_CURSEEK_END (ファイルの先頭、現在位置、ファイルの最後)という定数を 使うこともできます。 成功時には、1 を、失敗時にはそれ以外を返します。

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.

バイト単位に対する注意: 例え(例えば :encoding(UTF-8) I/O 層を使うなどして) ファイルハンドルが文字単位で処理するように設定されていたとしても、 seek, tell, sysseek シリーズの関数は 文字オフセットではなくバイトオフセットを使います; 文字オフセットでシークするのは UTF-8 ファイルではとても遅いからです。

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.

sysreadsyswrite のためにファイルの 位置を指定したい場合は、seek は 使えません; なぜならバッファリングのためにファイルの読み込み位置は 動作は予測不能で移植性のないものになってしまいます。 代わりに sysseek を使ってください。

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:

ANSI C の規則と困難により、システムによっては読み込みと書き込みを 切り替える度にシークしなければならない場合があります。 その他のことの中で、これは stdio の clearerr(3) を呼び出す効果があります。 WHENCE の 1 (SEEK_CUR) が、ファイル位置を変えないので有用です:

    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.)

これはアプリケーションで tail -f をエミュレートするのにも有用です。 一度読み込み時に EOF に到達すると、しばらくスリープし、 (おそらく) ダミーの seek をすることで リセットする必要があります。 seek は現在の位置を変更しませんが、 ハンドルの EOF 状態をクリアします ので、次の readline FILE で Perl は 再び何かを読み込もうとします。(そのはずです。)

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

これが動かない場合(特に意地の悪い I/O 実装もあります)、 以下のようなことをする必要があります:

    for (;;) {
        for ($curpos = tell($fh); $_ = readline($fh);
             $curpos = tell($fh)) {
            # search for some stuff and put it into files
        }
        sleep($for_a_while);
        seek($fh, $curpos, 0);
    }