perl-5.38.0
caller EXPR
caller

Returns the context of the current pure perl subroutine call. In scalar context, returns the caller's package name if there is a caller (that is, if we're in a subroutine or eval or require) and the undefined value otherwise. caller never returns XS subs and they are skipped. The next pure perl sub will appear instead of the XS sub in caller's return values. In list context, caller returns

その時点のピュア perl サブルーチン呼び出しのコンテキストを返します。 スカラコンテキストでは、呼び元が ある 場合 (サブルーチン、evalrequire の中に いるとき) には呼び出し元のパッケージ名を返し、 その他のときには未定義値を返します。 caller は XS サブルーチンを返すことはなく、それらは飛ばされます。 XS サブルーチンの代わりに次のピュア perl サブルーチンが caller の返り値に なります。 リストコンテキストでは、caller は以下を返します:

       # 0         1          2
    my ($package, $filename, $line) = caller;

Like __FILE__ and __LINE__, the filename and line number returned here may be altered by the mechanism described at "Plain Old Comments (Not!)" in perlsyn.

__FILE____LINE__ 同様、 ここで返されるファイル名と行番号は "Plain Old Comments (Not!)" in perlsyn で記述されている機構によって 置き換えられます。

With EXPR, it returns some extra information that the debugger uses to print a stack trace. The value of EXPR indicates how many call frames to go back before the current one.

EXPR を付けると、デバッガがスタックトレースを表示するために使う情報を返します。 EXPR の値は、現状から数えて、 いくつ前のコールフレームまで戻るかを示します。

    #  0         1          2      3            4
 my ($package, $filename, $line, $subroutine, $hasargs,

    #  5          6          7            8       9         10
    $wantarray, $evaltext, $is_require, $hints, $bitmask, $hinthash)
  = caller($i);

Here, $subroutine is the function that the caller called (rather than the function containing the caller). Note that $subroutine may be (eval) if the frame is not a subroutine call, but an eval. In such a case additional elements $evaltext and $is_require are set: $is_require is true if the frame is created by a require or use statement, $evaltext contains the text of the eval EXPR statement. In particular, for an eval BLOCK statement, $subroutine is (eval), but $evaltext is undefined. (Note also that each use statement creates a require frame inside an eval EXPR frame.) $subroutine may also be (unknown) if this particular subroutine happens to have been deleted from the symbol table. $hasargs is true if a new instance of @_ was set up for the frame. $hints and $bitmask contain pragmatic hints that the caller was compiled with. $hints corresponds to $^H, and $bitmask corresponds to ${^WARNING_BITS}. The $hints and $bitmask values are subject to change between versions of Perl, and are not meant for external use.

ここで、$subroutine は、(caller を含む関数ではなく) caller が呼び出した 関数です。 フレームがサブルーチン呼び出しではなく eval だった場合、この $subroutine は (eval) になることに注意してください。 この場合、追加の要素である $evaltext と $is_require がセットされます: $is_require はフレームが require または use で作られた場合に真になり、 $evaltext は eval EXPR のテキストが入ります。 特に、eval BLOCK の場合、$subroutine は (eval) になりますが、 $evaltext は未定義値になります。 (それぞれの useeval EXPR の中で require フレームを作ることに注意してください。) $subroutine は、そのサブルーチンがシンボルテーブルから削除された場合は (unknown) になります。 $hasargs はこのフレーム用に @_ の新しい実体が 設定された場合に真となります。 $hints$bitmask は caller がコンパイルされたときの 実際的なヒントを含みます。 $hints$^H に対応し、$bitmask${^WARNING_BITS} に 対応します。 $hints$bitmask は Perl のバージョンによって変更される 可能性があるので、外部での使用を想定していません。

$hinthash is a reference to a hash containing the value of %^H when the caller was compiled, or undef if %^H was empty. Do not modify the values of this hash, as they are the actual values stored in the optree.

$hinthash は、caller がコンパイルされた時の %^H の値を 含むハッシュへのリファレンスか、あるいは %^H が空の場合は undef です。 このハッシュの値は構文木に保管されている実際の値なので、変更しないで下さい。

Note that the only types of call frames that are visible are subroutine calls and eval. Other forms of context, such as while or foreach loops or try blocks are not considered interesting to caller, as they do not alter the behaviour of the return expression.

見ることができる呼び出しフレームの種類はサブルーチン呼び出しと eval だけであることに注意してください。 whileforeach のループや try ブロックのようなその他の構造は、 caller の関心外です; これらは return 式の振る舞いを変えないからです。

Furthermore, when called from within the DB package in list context, and with an argument, caller returns more detailed information: it sets the list variable @DB::args to be the arguments with which the subroutine was invoked.

さらに、DB パッケージの中からリストコンテキストで引数付きで呼ばれた場合は、 caller はより詳細な情報を返します; サブルーチンが起動されたときの引数を 変数 @DB::args に設定します。

Be aware that the optimizer might have optimized call frames away before caller had a chance to get the information. That means that caller(N) might not return information about the call frame you expect it to, for N > 1. In particular, @DB::args might have information from the previous time caller was called.

caller が情報を得る前にオプティマイザが呼び出しフレームを 最適化してしまうかもしれないことに注意してください。 これは、caller(N)N > 1 のとき、 あなたが予測した呼び出しフレームの情報を返さないかもしれないことを意味します。 特に、@DB::argscaller が前回呼び出された時の情報を 持っているかもしれません。

Be aware that setting @DB::args is best effort, intended for debugging or generating backtraces, and should not be relied upon. In particular, as @_ contains aliases to the caller's arguments, Perl does not take a copy of @_, so @DB::args will contain modifications the subroutine makes to @_ or its contents, not the original values at call time. @DB::args, like @_, does not hold explicit references to its elements, so under certain cases its elements may have become freed and reallocated for other variables or temporary values. Finally, a side effect of the current implementation is that the effects of shift @_ can normally be undone (but not pop @_ or other splicing, and not if a reference to @_ has been taken, and subject to the caveat about reallocated elements), so @DB::args is actually a hybrid of the current state and initial state of @_. Buyer beware.

@DB::args の設定は ベストエフォート で、デバッグやバックトレースの 生成を目的としていて、これに依存するべきではないということにも 注意してください。 特に、@_ は呼び出し元の引数へのエイリアスを含んでいるので、 Perl は @_ のコピーを取らず、従って @DB::args は サブルーチンが @_ やその内容に行った変更を含んでいて、 呼び出し時の元の値ではありません。 @DB::args は、@_ と同様、その要素への明示的な リファレンスを保持しないので、ある種の状況では、解放されて他の変数や 一時的な値のために再割り当てされているかもしれません。 最後に、現在の実装の副作用は、shift @_ の効果は 普通は 行われない (しかし pop @_ やその他の splice は違い、そして もし @_ のリファレンスが取られると違い、そして 再割り当てされた 要素に関する問題になりやすいです)ことなので、@DB::args は実際には現在の 状態と @_ の初期状態との合成物となります。 ご用心を。