perl-5.22.1
study SCALAR
study

May take extra time to study SCALAR ($_ if unspecified) in anticipation of doing many pattern matches on the string before it is next modified. This may or may not save time, depending on the nature and number of patterns you are searching and the distribution of character frequencies in the string to be searched; you probably want to compare run times with and without it to see which is faster. Those loops that scan for many short constant strings (including the constant parts of more complex patterns) will benefit most.

May take extra time to study SCALAR ($_ if unspecified) in anticipation of doing many pattern matches on the string before it is next modified. This may or may not save time, depending on the nature and number of patterns you are searching and the distribution of character frequencies in the string to be searched; you probably want to compare run times with and without it to see which is faster. Those loops that scan for many short constant strings (including the constant parts of more complex patterns) will benefit most. (TBT)

Note that since Perl version 5.16 this function has been a no-op, but this might change in a future release.

Note that since Perl version 5.16 this function has been a no-op, but this might change in a future release. (TBT)

(The way study works is this: a linked list of every character in the string to be searched is made, so we know, for example, where all the 'k' characters are. From each search string, the rarest character is selected, based on some static frequency tables constructed from some C programs and English text. Only those places that contain this "rarest" character are examined.)

(The way study works is this: a linked list of every character in the string to be searched is made, so we know, for example, where all the 'k' characters are. From each search string, the rarest character is selected, based on some static frequency tables constructed from some C programs and English text. Only those places that contain this "rarest" character are examined.) (TBT)

For example, here is a loop that inserts index producing entries before any line containing a certain pattern:

たとえば、特定のパターンを含む行の前にインデックスを 付けるエントリを入れる例を示します。

    while (<>) {
        study;
        print ".IX foo\n"    if /\bfoo\b/;
        print ".IX bar\n"    if /\bbar\b/;
        print ".IX blurfl\n" if /\bblurfl\b/;
        # ...
        print;
    }

In searching for /\bfoo\b/, only locations in $_ that contain f will be looked at, because f is rarer than o. In general, this is a big win except in pathological cases. The only question is whether it saves you more time than it took to build the linked list in the first place.

fo よりも珍しいので、/\bfoo\b/ を探すとき、$_f を 含む場所だけが探されます。 一般に、病的な場合を除いて、かなりの結果が得られます。 唯一の問題は、節約できる時間が、最初にリンクリストを作る 時間よりも多いかどうかです、

Note that if you have to look for strings that you don't know till runtime, you can build an entire loop as a string and eval that to avoid recompiling all your patterns all the time. Together with undefining $/ to input entire files as one record, this can be quite fast, often faster than specialized programs like fgrep(1). The following scans a list of files (@files) for a list of words (@words), and prints out the names of those files that contain a match:

実行時まで、探そうとする文字列がわからないときには、 ループ全体を文字列として組み立てて、eval すれば、 いつも、すべてのパターンを再コンパイルするという事態は避けられます。 ファイル全体を一つのレコードとして入力するために、 $/ を未定義にすれば、かなり速くなり、 多くの場合 fgrep(1) のような専用のプログラムより速くなります。 以下の例は、ファイルのリスト (@files) から単語のリスト (@words) を 探して、マッチするものがあったファイル名を出力します。

    $search = 'while (<>) { study;';
    foreach $word (@words) {
        $search .= "++\$seen{\$ARGV} if /\\b$word\\b/;\n";
    }
    $search .= "}";
    @ARGV = @files;
    undef $/;
    eval $search;        # this screams
    $/ = "\n";        # put back to normal input delimiter
    foreach $file (sort keys(%seen)) {
        print $file, "\n";
    }