perl-5.38.0
do BLOCK

Not really a function. Returns the value of the last command in the sequence of commands indicated by BLOCK. When modified by the while or until loop modifier, executes the BLOCK once before testing the loop condition. (On other statements the loop modifiers test the conditional first.)

実際は関数ではありません。 BLOCK で示されるコマンド列の最後の値を返します。 whileuntil ループ修飾子で修飾すると、 ループ条件を調べる前に 1 度、BLOCK を実行します。 (これ以外の実行文は、ループ修飾子により、条件が最初に 調べられます。)

do BLOCK does not count as a loop, so the loop control statements next, last, or redo cannot be used to leave or restart the block. See perlsyn for alternative strategies.

do BLOCK はループとしては 扱われません; 従って、next, last,redo といったループ制御文は ブロックから抜けたり再開することはできません。 その他の戦略については perlsyn を参照してください。

do EXPR

Uses the value of EXPR as a filename and executes the contents of the file as a Perl script:

EXPR の値をファイル名として用い、そのファイルの中身を Perl のスクリプトとして実行します:

    # load the exact specified file (./ and ../ special-cased)
    do '/foo/stat.pl';
    do './stat.pl';
    do '../foo/stat.pl';

    # search for the named file within @INC
    do 'stat.pl';
    do 'foo/stat.pl';

do './stat.pl' is largely like

do './stat.pl' はだいたい以下のものと同じようなものですが、

    eval `cat stat.pl`;

except that it's more concise, runs no external processes, and keeps track of the current filename for error messages. It also differs in that code evaluated with do FILE cannot see lexicals in the enclosing scope; eval STRING does. It's the same, however, in that it does reparse the file every time you call it, so you probably don't want to do this inside a loop.

より簡潔で、外部プログラムを起動せず、エラーメッセージでファイル名がわかる、 といったことがあります。 do FILE で評価されたコードは、入れ子のスコープにある レキシカル変数を見ることができないのに対し、eval STRINGではできる、 という違いがあります。 しかし、呼び出すたびにファイルを解析し直すという点では同じですから、 ループ内でこれを使おうなどとは、間違っても思ったりしないように。

Using do with a relative path (except for ./ and ../), like

次のように、do に (./../ 以外の) 相対パスを使うと:

    do 'foo/stat.pl';

will search the @INC directories, and update %INC if the file is found. See "@INC" in perlvar and "%INC" in perlvar for these variables. In particular, note that whilst historically @INC contained '.' (the current directory) making these two cases equivalent, that is no longer necessarily the case, as '.' is not included in @INC by default in perl versions 5.26.0 onwards. Instead, perl will now warn:

@INC ディレクトリを検索し、ファイルが見つかれば %INC を更新します。 これらの変数については "@INC" in perlvar"%INC" in perlvar を参照してください。 特に、歴史的には @INC に '.' (カレントディレクトリ) を 含んでいたのでこの二つの場合は等価でしたが、 perl バージョン 5.26.0 以降ではデフォルトでは @INC に '.' を 含んでいないので、もはやそうではないことに注意してください。 代わりに、perl は次のような警告を出します:

    do "stat.pl" failed, '.' is no longer in @INC;
    did you mean do "./stat.pl"?

If do can read the file but cannot compile it, it returns undef and sets an error message in $@. If do cannot read the file, it returns undef and sets $! to the error. Always check $@ first, as compilation could fail in a way that also sets $!. If the file is successfully compiled, do returns the value of the last expression evaluated.

do がファイルを読み込めたがコンパイルできなかった場合、 undef を返して $@ にエラーメッセージを 設定します。 doがファイルを読み込めなかった場合、undef を返して $! にエラーを設定します。 コンパイルに失敗したときにも $! が設定されるので、 常に $@ を先にチェックします。 ファイルのコンパイルに成功した場合、do は最後に評価した表現の 値を返します。

Inclusion of library modules is better done with the use and require operators, which also do automatic error checking and raise an exception if there's a problem.

ライブラリモジュールのインクルードには、 use 演算子や require 演算子を使った方がよいです; これらは自動的にエラーをチェックして、問題があれば例外を発生させます。

You might like to use do to read in a program configuration file. Manual error checking can be done this way:

do をプログラム設定ファイルを読み込むのに 使いたいかもしれません。 手動のエラーチェックは以下のようにして行えます:

    # Read in config files: system first, then user.
    # Beware of using relative pathnames here.
    for $file ("/share/prog/defaults.rc",
               "$ENV{HOME}/.someprogrc")
    {
        unless ($return = do $file) {
            warn "couldn't parse $file: $@" if $@;
            warn "couldn't do $file: $!"    unless defined $return;
            warn "couldn't run $file"       unless $return;
        }
    }