- 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.
callernever 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 サブルーチン呼び出しのコンテキストを返します。 スカラコンテキストでは、呼び元が ある 場合 (サブルーチン、eval、require の中に いるとき) には呼び出し元のパッケージ名を返し、 その他のときには未定義値を返します。
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_requireare set:$is_requireis true if the frame is created by a require or use statement, $evaltext contains the text of theeval EXPRstatement. In particular, for aneval BLOCKstatement, $subroutine is(eval), but $evaltext is undefined. (Note also that each use statement creates a require frame inside aneval EXPRframe.) $subroutine may also be(unknown)if this particular subroutine happens to have been deleted from the symbol table.$hasargsis true if a new instance of@_was set up for the frame.$hintsand$bitmaskcontain pragmatic hints that the caller was compiled with.$hintscorresponds to$^H, and$bitmaskcorresponds to${^WARNING_BITS}. The$hintsand$bitmaskvalues 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 は未定義値になります。 (それぞれの use はeval EXPRの中で require フレームを作ることに注意してください。) $subroutine は、そのサブルーチンがシンボルテーブルから削除された場合は(unknown)になります。$hasargsはこのフレーム用に@_の新しい実体が 設定された場合に真となります。$hintsと$bitmaskは caller がコンパイルされたときの 実際的なヒントを含みます。$hintsは$^Hに対応し、$bitmaskは${^WARNING_BITS}に 対応します。$hintsは$bitmaskは Perl のバージョンによって変更される 可能性があるので、外部での使用を想定していません。$hinthashis a reference to a hash containing the value of%^Hwhen the caller was compiled, or undef if%^Hwas 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 aswhileorforeachloops ortryblocks are not considered interesting tocaller, as they do not alter the behaviour of thereturnexpression.見ることができる呼び出しフレームの種類はサブルーチン呼び出しと
evalだけであることに注意してください。whileやforeachのループや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::argsto 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
callerhad a chance to get the information. That means thatcaller(N)might not return information about the call frame you expect it to, forN > 1. In particular,@DB::argsmight have information from the previous timecallerwas called.callerが情報を得る前にオプティマイザが呼び出しフレームを 最適化してしまうかもしれないことに注意してください。 これは、caller(N)がN > 1のとき、 あなたが予測した呼び出しフレームの情報を返さないかもしれないことを意味します。 特に、@DB::argsはcallerが前回呼び出された時の情報を 持っているかもしれません。Be aware that setting
@DB::argsis 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::argswill 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 ofshift @_can normally be undone (but notpop @_or other splicing, and not if a reference to@_has been taken, and subject to the caveat about reallocated elements), so@DB::argsis 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は実際には現在の 状態と@_の初期状態との合成物となります。 ご用心を。