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

Attempts to read LENGTH characters of data into variable SCALAR from the specified FILEHANDLE. Returns the number of characters 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 character actually read is the last character of the scalar after the read.

指定した FILEHANDLE から、変数 SCALAR に LENGTH 文字 のデータを 読み込みます。 実際に読み込まれた文字数、ファイル終端の場合は 0、エラーの場合は undef の いずれかを返します (後者の場合、$! もセットされます)。 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" のバイトでパッディングされます。

The call is implemented in terms of either Perl's or your system's native fread(3) library function, via the PerlIO layers applied to the handle. To get a true read(2) system call, see sysread.

この関数は、Perl か システムの fread(3) ライブラリ関数を使って、 ハンドルに適用されている PerlIO 層経由で実装されています。 本当の read(2) システムコールを利用するには、 sysread を参照してください。

Note the characters: depending on the status of the filehandle, either (8-bit) bytes or characters are read. By default, all filehandles operate on bytes, but for example if the filehandle has been opened with the :utf8 I/O layer (see open, and the open pragma), the I/O will operate on UTF8-encoded Unicode characters, not bytes. Similarly for the :encoding layer: in that case pretty much any characters can be read.

文字 に関する注意: ファイルハンドルの状態によって、(8 ビットの) バイトか 文字が読み込まれます。 デフォルトでは全てのファイルハンドルはバイトを処理しますが、 例えばファイルハンドルが :utf8 I/O 層 (open, open プラグマを参照してください) で 開かれた場合、I/O はバイトではなく、 UTF8 エンコードされた Unicode 文字を操作します。 :encoding 層も同様です: この場合、ほとんど大体全ての文字が読み込めます。