perl-5.38.0
IO::Handle->input_record_separator( EXPR )
$INPUT_RECORD_SEPARATOR
$RS
$/

The input record separator, newline by default. This influences Perl's idea of what a "line" is. Works like awk's RS variable, including treating empty lines as a terminator if set to the null string (an empty line cannot contain any spaces or tabs). You may set it to a multi-character string to match a multi-character terminator, or to undef to read through the end of file. Setting it to "\n\n" means something slightly different than setting to "", if the file contains consecutive empty lines. Setting to "" will treat two or more consecutive empty lines as a single empty line. Setting to "\n\n" will blindly assume that the next input character belongs to the next paragraph, even if it's a newline.

入力レコードセパレータで、デフォルトでは改行文字。 これは Perl での「行」とは何か、ということに影響を与えます。 空文字列に設定されると、空行をセパレータとして扱うことを含めて、awk の 変数 RS のように働きます(空行はスペースやタブを含んでいてはいけません)。 複数文字の区切文字を示すために、文字列を設定することもできます; また、 ファイルの最後まで読み込むために undef を指定することもできます。 この変数に "\n\n" を設定すると、空行が続く場合において、 "" を設定した場合とわずかに違う動作をするようになります。 "" を設定した場合には、複数の空行も 1 つの空行であるかのように扱います。 "\n\n" を設定した場合には、単純に次の文字が (たとえ改行文字であっても) 次の段落に含まれるものとして扱います。

    local $/;           # enable "slurp" mode
    local $_ = <FH>;    # whole file now here
    s/\n[ \t]+/ /g;
    local $/;           # 「吸い込み」モードを有効にする
    local $_ = <FH>;    # ファイル全体が入る
    s/\n[ \t]+/ /g;

Remember: the value of $/ is a string, not a regex. awk has to be better for something. :-)

注意: $/ は文字列であり、正規表現ではありません。 awk は何かもっとうまくやらなくてはいけません。:-)

Setting $/ to an empty string -- the so-called paragraph mode -- merits special attention. When $/ is set to "" and the entire file is read in with that setting, any sequence of one or more consecutive newlines at the beginning of the file is discarded. With the exception of the final record in the file, each sequence of characters ending in two or more newlines is treated as one record and is read in to end in exactly two newlines. If the last record in the file ends in zero or one consecutive newlines, that record is read in with that number of newlines. If the last record ends in two or more consecutive newlines, it is read in with two newlines like all preceding records.

$/ に空文字列を設定すること -- 段落モード と呼ばれます -- には、 特に注意する価値があります。 $/"" に設定されて、ファイル全体がこの設定で読み込まれた場合、 ファイル先頭の一つ以上の連続する改行の並びは捨てられます。 ファイルの最後のレコードを例外として、 二つ以上の改行で終わるそれぞれの文字並びは一つのレコードとして扱われ、 正確に二つの改行で終わるように読み込まれます。 ファイルの最後のレコードが 0 または 1 の連続する改行で終わっている場合、 そのレコードはその数の改行と共に読み込まれます。 最後のレコードが二つ以上の連続する改行で終わっている場合、 全ての先行するレコードと同様、二つの改行と共に読み込まれます。

Suppose we wrote the following string to a file:

次の文字列をファイルに書いたとします:

    my $string = "\n\n\n";
    $string .= "alpha beta\ngamma delta\n\n\n";
    $string .= "epsilon zeta eta\n\n";
    $string .= "theta\n";

    my $file = 'simple_file.txt';
    open my $OUT, '>', $file or die;
    print $OUT $string;
    close $OUT or die;

Now we read that file in paragraph mode:

このファイルを段落モードで読み込みます:

    local $/ = ""; # paragraph mode
    open my $IN, '<', $file or die;
    my @records = <$IN>;
    close $IN or die;

@records will consist of these 3 strings:

@records は三つの文字列からなります:

    (
      "alpha beta\ngamma delta\n\n",
      "epsilon zeta eta\n\n",
      "theta\n",
    )

Setting $/ to a reference to an integer, scalar containing an integer, or scalar that's convertible to an integer will attempt to read records instead of lines, with the maximum record size being the referenced integer number of characters. So this:

$/ に整数、整数を含むスカラ、整数に変換できるスカラのいずれかへの リファレンスをセットすると、行を読む代わりにレコードを読もうとします; この場合、最大レコードサイズはリファレンス先の整数値の文字数となります。 つまり:

    local $/ = \32768; # or \"32768", or \$var_containing_32768
    open my $fh, "<", $myfile or die $!;
    local $_ = <$fh>;

will read a record of no more than 32768 characters from $fh. If you're not reading from a record-oriented file (or your OS doesn't have record-oriented files), then you'll likely get a full chunk of data with every read. If a record is larger than the record size you've set, you'll get the record back in pieces. Trying to set the record size to zero or less is deprecated and will cause $/ to have the value of "undef", which will cause reading in the (rest of the) whole file.

これは $fh から 32768 文字を超えないようにレコードを読み込みます。 もしレコード指向のファイルを読み込まない場合(あるいは OS がレコード指向 ファイルを持たない場合)、読み込み毎にデータのチャンク全部を取り込みます。 もしレコードがセットしたレコードサイズより大きい場合、 レコードの部分を取り込みます。 レコードサイズを 0 以下にセットしようとするのは廃止予定で、 $/ に値 "undef" が設定され、(残りの)ファイル全体を読み込むことになります。

As of 5.19.9 setting $/ to any other form of reference will throw a fatal exception. This is in preparation for supporting new ways to set $/ in the future.

5.19.9 から、$/ にその他の形式のリファレンスを設定すると致命的エラーが 投げられます。 これは将来 $/ を設定する新しい方法に対応する準備です。

On VMS only, record reads bypass PerlIO layers and any associated buffering, so you must not mix record and non-record reads on the same filehandle. Record mode mixes with line mode only when the same buffering layer is in use for both modes.

VMS だけでは、レコードは PerlIO 層とそれに関連するバッファリングを迂回して 読み込まれるので、同じファイルハンドルでレコード読み込みと 非レコード読み込みを混ぜてはいけません。 レコードモードは、同じバッファリング層を両方のモードで使う場合にのみ ラインモードと混ざります。

You cannot call input_record_separator() on a handle, only as a static method. See IO::Handle.

ハンドルに対して input_record_separator() を呼び出すことはできません; 静的メソッドしてのみです。 IO::Handle を参照してください。

See also "Newlines" in perlport. Also see "$.".

"Newlines" in perlport を参照してください。 "$." も参照してください。

Mnemonic: / delimits line boundaries when quoting poetry.

記憶法: /は、詩を引用するときに、行の区切りを示します。