perl-5.38.0
exec LIST
exec PROGRAM LIST

The exec function executes a system command and never returns; use system instead of exec if you want it to return. It fails and returns false only if the command does not exist and it is executed directly instead of via your system's command shell (see below).

exec 関数は、システムのコマンドを実行し、戻ってはきません; 戻って欲しい場合には、execではなく system 関数を使ってください。 コマンドが存在せず、しかも システムのコマンドシェル経由でなく 直接コマンドを実行しようとした場合にのみこの関数は失敗して偽を返します。

Since it's a common mistake to use exec instead of system, Perl warns you if exec is called in void context and if there is a following statement that isn't die, warn, or exit (if warnings are enabled--but you always do that, right?). If you really want to follow an exec with some other statement, you can use one of these styles to avoid the warning:

system の代わりに exec を使うという よくある間違いを防ぐために、exec が無効コンテキストで 呼び出されて、引き続く文が die, warn, exit 以外の場合、Perl は警告を出します(warnings が 有効の場合 -- でもいつもセットしてますよね?)。 もし 本当に exec の後に他の文を書きたい場合、以下の どちらかのスタイルを使うことで警告を回避できます:

    exec ('foo')   or print STDERR "couldn't exec foo: $!";
    { exec ('foo') }; print STDERR "couldn't exec foo: $!";

If there is more than one argument in LIST, this calls execvp(3) with the arguments in LIST. If there is only one element in LIST, the argument is checked for shell metacharacters, and if there are any, the entire argument is passed to the system's command shell for parsing (this is /bin/sh -c on Unix platforms, but varies on other platforms). If there are no shell metacharacters in the argument, it is split into words and passed directly to execvp, which is more efficient. Examples:

LIST に複数の引数がある場合は、LIST の引数を使って execvp(3) を 呼び出します。 LIST に要素が一つのみの場合には、その引数からシェルのメタ文字をチェックし、 もしメタ文字があれば、引数全体をシステムのコマンドシェル(これはUnix では /bin/sh -c ですが、システムによって異なります)に渡して解析させます。 シェルのメタ文字がなかった場合、引数は単語に分解されて直接 execvp に 渡されます; この方がより効率的です。 例:

    exec '/bin/echo', 'Your arguments are: ', @ARGV;
    exec "sort $outfile | uniq";

If you don't really want to execute the first argument, but want to lie to the program you are executing about its own name, you can specify the program you actually want to run as an "indirect object" (without a comma) in front of the LIST, as in exec PROGRAM LIST. (This always forces interpretation of the LIST as a multivalued list, even if there is only a single scalar in the list.) Example:

第一引数に指定するものを本当に実行したいが、実行するプログラムに対して別の 名前を教えたい場合には、exec PROGRAM LIST のように、LIST の前に 「間接オブジェクト」(コンマなし) として実際に実行したいプログラムを 指定することができます。 (これによって、LIST に単一のスカラしかなくても、複数値のリストであるように、 LIST の解釈を行ないます。) 例:

    my $shell = '/bin/csh';
    exec $shell '-sh';    # pretend it's a login shell

or, more directly,

あるいは、より直接的に、

    exec {'/bin/csh'} '-sh';  # pretend it's a login shell

When the arguments get executed via the system shell, results are subject to its quirks and capabilities. See "`STRING`" in perlop for details.

引数がシステムシェルで実行されるとき、結果はシェルの奇癖と能力によって 変わります。 詳細については "`STRING`" in perlop を参照してください。

Using an indirect object with exec or system is also more secure. This usage (which also works fine with system) forces interpretation of the arguments as a multivalued list, even if the list had just one argument. That way you're safe from the shell expanding wildcards or splitting up words with whitespace in them.

execsystem で間接オブジェクトを 使うのもより安全です。 この使い方(system でも同様にうまく動きます)は、たとえ 引数が一つだけの場合も、複数の値を持つリストとして引数を解釈することを 強制します。 この方法で、シェルによるワイルドカード展開や、空白による単語の分割から 守られます。

    my @args = ( "echo surprise" );

    exec @args;               # subject to shell escapes
                                # if @args == 1
    exec { $args[0] } @args;  # safe even with one-arg list

The first version, the one without the indirect object, ran the echo program, passing it "surprise" an argument. The second version didn't; it tried to run a program named "echo surprise", didn't find it, and set $? to a non-zero value indicating failure.

間接オブジェクトなしの一つ目のバージョンでは、echo プログラムが実行され、 "surprise" が引数として渡されます。 二つ目のバージョンでは違います; "echo surprise" という名前の プログラムを実行しようとして、見つからないので、失敗したことを示すために $? に非 0 がセットされます。

On Windows, only the exec PROGRAM LIST indirect object syntax will reliably avoid using the shell; exec LIST, even with more than one element, will fall back to the shell if the first spawn fails.

Windows では、exec PROGRAM LIST 間接オブジェクト構文のみが、シェルを 使うのを回避するための信頼できる方法です; exec LIST は、複数の要素が あっても、最初の spawn が失敗したときにシェルに フォールバックすることがあります。

Perl attempts to flush all files opened for output before the exec, but this may not be supported on some platforms (see perlport). To be safe, you may need to set $| ($AUTOFLUSH in English) or call the autoflush method of IO::Handle on any open handles to avoid lost output.

Perl は exec の前に出力用に開かれている全てのファイルを フラッシュしようとしますが、これに対応していないプラットフォームもあります (perlport を参照してください)。 安全のためには、出力が重複するのを避けるために、全てのオープンしている ハンドルに対して $| (English モジュールでは $AUTOFLUSH) を設定するか、 IO::Handle モジュールの autoflush メソッドを 呼ぶ必要があるかもしれません。

Note that exec will not call your END blocks, nor will it invoke DESTROY methods on your objects.

execEND ブロックや、オブジェクトの DESTROY メソッドを起動しないことに注意してください。

Portability issues: "exec" in perlport.

移植性の問題: "exec" in perlport