perl-5.38.0
sysread FILEHANDLE,SCALAR,LENGTH,OFFSET
sysread FILEHANDLE,SCALAR,LENGTH

Attempts to read LENGTH bytes of data into variable SCALAR from the specified FILEHANDLE, using read(2). It bypasses any PerlIO layers including buffered IO (but is affected by the presence of the :utf8 layer as described later), so mixing this with other kinds of reads, print, write, seek, tell, or eof can cause confusion because the :perlio or :crlf layers usually buffer data. Returns the number of bytes actually read, 0 at end of file, or undef if there was an error (in the latter case $! is also set). SCALAR will be grown or shrunk so that the last byte actually read is the last byte of the scalar after the read.

read(2) を用いて、指定した FILEHANDLE から、変数 SCALAR へ、LENGTH バイトの データの読み込みを試みます。 これは、バッファ付き IO ルーチンを含むどの PerlIO も通らないので (しかし、後述するように :utf8 の存在の影響を受けます)、他の入力関数、 print, write, seek, tell, eof と混ぜて使うと、入力がおかしくなるかも しれません; :perlio 層や :crlf 層は普通データをバッファリングするからです。 ファイルの最後では 0が、エラー時には undef が、それ以外では実際に 読み込まれたデータの長さが返されます (後者の場合は $! も セットされます)。 実際に読み込んだ最後のバイトが read した後の最後のバイトになるので、 SCALAR は伸び縮みします。

An OFFSET may be specified to place the read data at some place in the string other than the beginning. A negative OFFSET specifies placement at that many characters counting backwards from the end of the string. A positive OFFSET greater than the length of SCALAR results in the string being padded to the required size with "\0" bytes before the result of the read is appended.

OFFSET を指定すると、文字列の先頭以外の場所から読み込みを行なえます。 OFFSET に負の値を指定すると、文字列の最後から逆向きに何文字目かで 位置を指定します。 OFFSET が正の値で、SCALAR の長さよりも大きかった場合、文字列は読み込みの結果が 追加される前に、必要なサイズまで "\0" のバイトでパッディングされます。

There is no syseof() function, which is ok, since eof doesn't work well on device files (like ttys) anyway. Use sysread and check for a return value of 0 to decide whether you're done.

syseof() 関数はありませんが、問題ありません; どちらにしろ eof は (tty のような)デバイスファイルに対してはうまく動作しないからです。 sysread を使って、 返り値が 0 かどうかで最後まで読んだかを判断してください。

Note that if the filehandle has been marked as :utf8, sysread will throw an exception. The :encoding(...) layer implicitly introduces the :utf8 layer. See binmode, open, and the open pragma.

ファイルハンドルが :utf8 であるとマークが付けられていると、 sysread は例外を投げます。 :encoding(...) 層は暗黙に :utf8 層を導入します。 binmode, open, open プラグマを参照してください。