目次 TABLE OF CONTENTS
B::Concise - Perl構文木を辿ってopの簡潔な情報を表示 B::Concise - Walk Perl syntax tree, printing concise info about ops
perl -MO=Concise[,OPTIONS] foo.pl
use B::Concise qw(set_style add_callback);
このコンパイラバックエンドはPerlプログラムの構文木の内部OPを表示します. 表示形式はperlの内部構造や他のコンパイラバックエンドのデバッグに便利な スペースを効果的に使ったいくつかのテキスト形式があります. ここでは, OPツリーに現れた順, 実行される順, ツリー構造に近似させた テキスト等を表示できます. また, 表示される情報はカスタマイズ可能です. この機能は Perl の -Dx デバッグフラグや B::Terse モジュールと よく似ています. しかしこれはよりより洗練されていて, また, 柔軟です. This compiler backend prints the internal OPs of a Perl program's syntax tree in one of several space-efficient text formats suitable for debugging the inner workings of perl or other compiler backends. It can print OPs in the order they appear in the OP tree, in the order they will execute, or in a text approximation to their tree structure, and the format of the information displyed is customizable. Its function is similar to that of perl's -Dx debugging flag or the B::Terse module, but it is more sophisticated and flexible.
ここに2つの出力(レンダリングとも呼ばれる)例を示します. 1つは -exec を使った物で, もう1つは -basic (デフォルト)を使った物です. 処理対象のコードは同じ物です. Here's an example of 2 outputs (aka 'renderings'), using the -exec and -basic (i.e. default) formatting conventions on the same code snippet.
% perl -MO=Concise,-exec -e '$a = $b + 42'
1 <0> enter
2 <;> nextstate(main 1 -e:1) v
3 <#> gvsv[*b] s
4 <$> const[IV 42] s
* 5 <2> add[t3] sK/2
6 <#> gvsv[*a] s
7 <2> sassign vKS/2
8 <@> leave[1 ref] vKP/REFC
各行が1つのopcodeに対応します. '*' がマークされているopcodeは 以下の例でも少しでてきます. Each line corresponds to an opcode. The opcode marked with '*' is used in a few examples below.
最初のカラムはopの連番です. デフォルトでは36進数です(訳注:0..9, a..z, 10..19, 1a..1z ていうかんじ). このレンダリングは-exec(つまり実行)順のものです. The 1st column is the op's sequence number, starting at 1, and is displayed in base 36 by default. This rendering is in -exec (i.e. execution) order.
山形の括弧に囲まれた記号はopの種類を示しています. <2>であればBINOP, <@>であればLISTOP, <#>であればPADOP, これはスレッドperlで使われます ("OP class abbreviations" [CPAN]を参照). The symbol between angle brackets indicates the op's type, for example; <2> is a BINOP, <@> a LISTOP, and <#> is a PADOP, which is used in threaded perls. (see "OP class abbreviations" [CPAN]).
opcode名は, 'add[t1]'の様に操作に関連する情報が括弧や大括弧で 追加されています. The opname, as in 'add[t1]', which may be followed by op-specific information in parentheses or brackets (ex '[t1]').
その後ろにはop-flags('sK/2'等)が続きます. この詳細は "OP class abbreviations" [CPAN]を参照してください. The op-flags (ex 'sK/2') follow, and are described in ("OP flags abbreviations" [CPAN]).
% perl -MO=Concise -e '$a = $b + 42'
8 <@> leave[1 ref] vKP/REFC ->(end)
1 <0> enter ->2
2 <;> nextstate(main 1 -e:1) v ->3
7 <2> sassign vKS/2 ->8
* 5 <2> add[t1] sK/2 ->6
- <1> ex-rv2sv sK/1 ->4
3 <$> gvsv(*b) s ->4
4 <$> const(IV 42) s ->5
- <1> ex-rv2sv sKRM*/1 ->7
6 <$> gvsv(*a) s ->7
デフォルトのレンダリングはトップダウンです. つまり実行順ではありません. この形式はパース及び式の評価に使われたスタックの道筋を反映しています. add はツリー上でその下にある2つの項に対して演算されます. The default rendering is top-down, so they're not in execution order. This form reflects the way the stack is used to parse and evaluate expressions; the add operates on the two terms below it in the tree.
Nullop は ex-opname として perl の最適化によって opname が
省かれた場所に現れます. これらは連番に '-' が表示されます.
なぜならこれらは評価されないからです(1つ前の例(訳注:-execの出力例)では
表示されていません). ここ(今回の例)ではパースの結果を反映しているので
表示されています.
Nullops appear as ex-opname, where opname is an op that has been
optimized away by perl. They're displayed with a sequence-number of
'-', because they are not executed (they don't appear in previous
example), they're printed here because they reflect the parse.
矢印が指している数字は次のopの連番です. これは -exec モードでは 明らかなことなので表示されません. The arrow points to the sequence number of the next op; they're not displayed in -exec mode, for obvious reasons.
このレンダリングはスレッドでないperlで行ったため, 前の例であった PADOP は ここでは SVOP になっています. またいくつかの大括弧(全てではないですが)は 括弧に置き換えられています. これはスレッドperlとスレッドでないperl間で 見た目の区別を提供するための巧妙な機能です. Note that because this rendering was done on a non-threaded perl, the PADOPs in the previous examples are now SVOPs, and some (but not all) of the square brackets have been replaced by round ones. This is a subtle feature to provide some visual distinction between renderings on threaded and un-threaded perls.
引数のうちハイフンで始まる物以外は OP を出力する関数の名前として
取ります. 関数を指定しなかった場合はプログラムのメイン(全ての関数の外側で
use や require されたファイルも含まない)がレンダリングされます.
BEGIN, 若しくは CHECK, INIT, END を渡すと対応する特殊ブロックが
出力されます.
Arguments that don't start with a hyphen are taken to be the names of
subroutines to print the OPs of; if no such functions are specified,
the main body of the program (outside any subroutines, and not
including use'd or require'd files) is rendered. Passing BEGIN,
CHECK, INIT, or END will cause all of the corresponding
special blocks to be printed.
オプションはどの様にレンダリング(つまり出力)されるのかに作用します. ここでは見た目毎に説明していきます. 最初にある方が優先されます. どのように関連するかに応じてグループ分けされています. それぞれの グループ内では指定できるのは1つだけです(記述がある物は除いて). Options affect how things are rendered (ie printed). They're presented here by their visual effect, 1st being strongest. They're grouped according to how they interrelate; within each group the options are mutually exclusive (unless otherwise stated).
これらのオプションは opcode の '縦方向の表示' を制御します. 表示される '順番' はこのドキュメント以外で 'モード' と 呼ばれることもあります. These options control the 'vertical display' of opcodes. The display 'order' is also called 'mode' elsewhere in this document.
Print OPs in the order they appear in the OP tree (a preorder traversal, starting at the root). The indentation of each OP shows its level in the tree, and the '->' at the end of the line indicates the next opcode in execution order. This mode is the default, so the flag is included simply for completeness.
Print OPs in the order they would normally execute (for the majority of constructs this is a postorder traversal of the tree, ending at the root). In most cases the OP that usually follows a given OP will appear directly below it; alternate paths are shown by indentation. In cases like loops when control jumps out of a linear path, a 'goto' line is generated.
Print OPs in a text approximation of a tree, with the root of the tree at the left and 'left-to-right' order of children transformed into 'top-to-bottom'. Because this mode grows both to the right and down, it isn't suitable for large programs (unless you have a very wide terminal).
これらのオプションは各opcodeのレンダリングに使用するラインスタイル( 若しくは単にスタイルとも呼ばれる)を選択します. そして各行に実際どんな情報が 出力されるかを決定します. These options select the line-style (or just style) used to render each opcode, and dictates what info is actually printed into each line.
ユーザの好きな書式セットを使います. もちろんこれがデフォルトです. Use the author's favorite set of formatting conventions. This is the default, of course.
B::Terse の出力をエミュレートする書式を使います. basicモードでは実際の B::Terseと見分けがつかないほどです. exec モードでもとてもよく似ています が, より論理的な順序を持ち, 少ない波括弧で構成されます. B::Terse は tree モードを持ちません. このためツリーモードはB::Terseの曖昧な連想に すぎません. Use formatting conventions that emulate the output of B::Terse. The basic mode is almost indistinguishable from the real B::Terse, and the exec mode looks very similar, but is in a more logical order and lacks curly brackets. B::Terse doesn't have a tree mode, so the tree mode is only vaguely reminiscent of B::Terse.
各OPの名前に1文字が2文字の省略で表現される書式を使います. ほぼお遊びです. Use formatting conventions in which the name of each OP, rather than being written out in full, is represented by a one- or two-character abbreviation. This is mainly a joke.
B::Debug を連想させる書式を使います. 全然明瞭(concise)ではありません. Use formatting conventions reminiscent of B::Debug; these aren't very concise at all.
環境変数 B_CONCISE_FORMAT, B_CONCISE_GOTO_FORMAT,
B_CONCISE_TREE_FORMAT の値を書式として使います.
Use formatting conventions read from the environment variables
B_CONCISE_FORMAT, B_CONCISE_GOTO_FORMAT, and B_CONCISE_TREE_FORMAT.
ノードをつなぐ線に最小量のスペース(多くの場合1文字)を割り当てる ツリー書式を使います. これは貴重な端末の表示幅からスペースを絞り 出します. Use a tree format in which the minimum amount of space is used for the lines connecting nodes (one character in most cases). This squeezes out a few precious columns of screen real estate.
区切られたOPノードに幾分長いエッジを使うツリー書式を使います. この書式は特にASCIIにおいてはcompactより見やすいでしょう. そして これがデフォルトです. Use a tree format that uses longer edges to separate OP nodes. This format tends to look better than the compact one, especially in ASCII, and is the default.
VT100行描画集合で描くツリーを使います. もし端末がサポートしているのなら これがよりよいでしょう. Use tree connecting characters drawn from the VT100 line-drawing set. This looks better if your terminal supports it.
+ や | 等の標準ASCII文字でツリーを描画します. これはVT100ほど
きれいではありませんが, ほとんどの端末(そしてless(1)の縦スクロール
モード等)でも機能します. また, テキストのドキュメントやemailにも
向いています. これはデフォルトです.
Draw the tree with standard ASCII characters like + and |. These don't
look as clean as the VT100 characters, but they'll work with almost any
terminal (or the horizontal scrolling mode of less(1)) and are suitable
for text documentation or email. This is the default.
これらは対になるもの, つまり compactとloose, vtとasciiで 排他的です. These are pairwise exclusive, i.e. compact or loose, vt or ascii.
Print OP sequence numbers in base n. If n is greater than 10, the digit for 11 will be 'a', and so on. If n is greater than 36, the digit for 37 will be 'A', and so on until 62. Values greater than 62 are not currently supported. The default is 36.
Print sequence numbers with the most significant digit first. This is the usual convention for Arabic numerals, and the default.
Print seqence numbers with the least significant digit first. This is obviously mutually exclusive with bigendian.
これらは対になるもので排他的です. These are pairwise exclusive.
関数が指定されている場合でもメインプログラムも出力に含めます. このレンダリングは通常は関数名若しくはリファレンスが与えられると抑制され ます. Include the main program in the output, even if subroutines were also specified. This rendering is normally suppressed when a subroutine name or reference is given.
これは '-main' で設定を変更した後にデフォルトの振る舞いを復元します (通常は必要ありません). もし関数の名前/リファレンスが与えられなかった 場合にはこのフラグによらずメインプログラムはレンダリングされます. This restores the default behavior after you've changed it with '-main' (it's not normally needed). If no subroutine name/ref is given, main is rendered, regardless of this flag.
レンダリングは通常関数名や関数リファレンスを文字列化したものを 識別用にバナー行として出力します. これはバナーの出力を抑制します. Renderings usually include a banner line identifying the function name or stringified subref. This suppresses the printing of the banner.
TBC: Remove the stringified coderef; while it provides a 'cookie' for each function rendered, the cookies used should be 1,2,3.. not a random hex-address. It also complicates string comparison of two different trees.
バナーのデフォルトの振る舞いを復元します. restores default banner behavior.
TBC: a hookpoint (and an option to set it) for a user-supplied function to produce a banner appropriate for users needs. It's not ideal, because the rendering-state variables, which are a natural candidate for use in concise.t, are unavailable to the user.
Concise を複数回プログラムから呼び出すのなら, オプションが 'sticky' な ことを知っておくべきでしょう. つまり最初の呼び出しで設定したオプションは 2度目の呼び出しでは指定していなくても記憶されていると言うことです. If you invoke Concise more than once in a program, you should know that the options are 'sticky'. This means that the options you provide in the first call will be remembered for the 2nd call, unless you re-specify or change them.
conciseのスタイルは最小のデータで最大の情報を伝達するための記号を 使います. まだ慣れていないのであればツリー構造の中で枝ではなく 花を見るとよいでしょう. The concise style uses symbols to convey maximum info with minimal clutter (like hex addresses). With just a little practice, you can start to see the flowers, not just the branches, in the trees.
これらの記号は op名の前に使われて, Perl コードの op に対応する B:: 名前空間を示します. These symbols appear before the op-name, and indicate the B:: namespace that represents the ops in your Perl code.
0 OP (aka BASEOP) An OP with no children
1 UNOP An OP with one child
2 BINOP An OP with two children
| LOGOP A control branch OP
@ LISTOP An OP that could have lots of children
/ PMOP An OP with a regular expression
$ SVOP An OP with an SV
" PVOP An OP with a string
{ LOOP An OP that holds pointers for a loop
; COP An OP that marks the start of a statement
# PADOP An OP with a GV on the pad
0 OP (aka BASEOP) 子を持たない OP
1 UNOP 1つだけ子を持つ OP
2 BINOP 2つの子を持つ OP
| LOGOP 制御分岐 OP
@ LISTOP 複数の子を持つ OP
/ PMOP 正規表現 OP
$ SVOP SV の OP
" PVOP 文字列の OP
{ LOOP ループ地点を保持する OP
; COP 文の開始を記録する OP
# PADOP パディングで GV を持つ OP
OP フラグにはパブリックなものとプライベートなものとがあります. パブリックなフラグは一貫した方法で各opcodeの振る舞いを変化させ, 0文字若しくは1文字以上で表現されます. OP flags are either public or private. The public flags alter the behavior of each opcode in consistent ways, and are represented by 0 or more single characters.
v OPf_WANT_VOID Want nothing (void context)
s OPf_WANT_SCALAR Want single value (scalar context)
l OPf_WANT_LIST Want list of any length (list context)
Want is unknown
K OPf_KIDS There is a firstborn child.
P OPf_PARENS This operator was parenthesized.
(Or block needs explicit scope entry.)
R OPf_REF Certified reference.
(Return container, not containee).
M OPf_MOD Will modify (lvalue).
S OPf_STACKED Some arg is arriving on the stack.
* OPf_SPECIAL Do something weird for this op (see op.h)
v OPf_WANT_VOID 何も必要なし (voidコンテキスト)
s OPf_WANT_SCALAR 1つの値を要求 (スカラーコンテキスト)
l OPf_WANT_LIST 任意長のリストを要求 (リストコンテキスト)
要求は不明
K OPf_KIDS 最初の子供
P OPf_PARENS この演算子は括弧の中 (若しくはブロックが
明示的にスコープへの突入が必要.)
R OPf_REF 保証されたリファレンス.
(格納されているものではなくコンテナを返す.)
M OPf_MOD 変更可能(lvalue).
S OPf_STACKED いくつかの引数はスタック上にある.
* OPf_SPECIAL なにか変わったことをするop (op.hを参照)
プライベートフラグはあるopcode用の集合で, '/' の後に表示されます. Private flags, if any are set for an opcode, are displayed after a '/'
8 <@> leave[1 ref] vKP/REFC ->(end)
7 <2> sassign vKS/2 ->8
これらはopcodeの仕様であり, パブリックなものより稀に発生します. そのため1文字ではなく短いニーモニックで表現されます. 驚愕の詳細は op.h [CPAN] を見るか, 以下の2行を試してみてください: They're opcode specific, and occur less often than the public ones, so they're represented by short mnemonics instead of single-chars; see op.h [CPAN] for gory details, or try this quick 2-liner:
$> perl -MB::Concise -de 1
DB<1> |x \%B::Concise::priv
それぞれのラインスタイル('concise', 'terse', 'linenoise' 等)とも, OPのレンダリング方法に3つの書式を持っています. For each line-style ('concise', 'terse', 'linenoise', etc.) there are 3 format-specs which control how OPs are rendered.
1つは 'default' で, これは basic と exec モードでのすべてのopcodeの 出力に使われます. 2つめは goto 書式で, exec モードで分岐の出力に 使われます. これらは実際のopcodeではなくて, 波括弧が閉じている様に 見える様に入れられるものです. そしてtree書式は木構造のためのです. The first is the 'default' format, which is used in both basic and exec modes to print all opcodes. The 2nd, goto-format, is used in exec mode when branches are encountered. They're not real opcodes, and are inserted to look like a closing curly brace. The tree-format is tree specific.
When a line is rendered, the correct format-spec is copied and scanned for the following items; data is substituted in, and other manipulations like basic indenting are done, for each opcode rendered.
There are 3 kinds of items that may be populated; special patterns, #vars, and literal text, which is copied verbatim. (Yes, it's a set of s///g steps.)
These items are the primitives used to perform indenting, and to select text from amongst alternatives.
Generates exec_text in exec mode, or basic_text in basic mode.
Generates one copy of text for each indentation level.
Generates one fewer copies of text1 than the indentation level, followed by one copy of text2 if the indentation level is more than 0.
If the value of var is true (not empty or zero), generates the value of var surrounded by text1 and Text2, otherwise nothing.
Any number of tildes and surrounding whitespace will be collapsed to a single space.
これらの #var はレンダリングの一部として必要になるopcodeのプロパティを 表現します. '#' はプライベートなsigilを意図しています. #var の値は "read $this" の様にスタイル行に書き込まれます. (訳注:sigil=印. $とか@とかを指すのにも使われる.) These #vars represent opcode properties that you may want as part of your rendering. The '#' is intended as a private sigil; a #var's value is interpolated into the style-line, much like "read $this".
これらには3つの形式があります: These vars take 3 forms:
opcodeの為に'var' という名前のプロパティが存在すると考えれます. そしてこれはレンダリングに書き込まれます. A property named 'var' is assumed to exist for the opcodes, and is interpolated into the rendering.
Nこの空間に左詰してvar の値を生成します. これは 'foo' と 'foo2' というプロパティをもっていたとしても, 'foo2' をレンダリング することは出来ないことを意味します. 'foo2a' であれば可能です. この振る舞いを当てにはしないでしょうが;-) Generates the value of var, left justified to fill N spaces. Note that this means while you can have properties 'foo' and 'foo2', you cannot render 'foo2', but you could with 'foo2a'. You would be wise not to rely on this behavior going forward ;-)
この #var の1文字目が大文字になっている形式は名前-値形式を 生成します. '#Var' は 'Var => #var' を生成し, これはこれまでに説明した ように扱われます. (実装メモ: #Var は条件付き埋め込みには使えません. => #var 変換は #Var の値のチェックの後に行われるからです. ) This ucfirst form of #var generates a tag-value form of itself for display; it converts '#Var' into a 'Var => #var' style, which is then handled as described above. (Imp-note: #Vars cannot be used for conditional-fills, because the => #var transform is done after the check for #Var's value).
The following variables are 'defined' by B::Concise; when they are used in a style, their respective values are plugged into the rendering of each opcode.
Only some of these are used by the standard styles, the others are provided for you to delve into optree mechanics, should you wish to add a new style (see "add_style" [CPAN] below) that uses them. You can also add new ones using "add_callback" [CPAN].
The address of the OP, in hexadecimal.
The OP-specific information of the OP (such as the SV for an SVOP, the non-local exit pointers for a LOOP, etc.) enclosed in parentheses.
The B-determined class of the OP, in all caps.
A single symbol abbreviating the class of the OP.
The label of the statement or block the OP is the start of, if any.
The name of the OP, or 'ex-foo' if the OP is a null that used to be a foo.
The target of the OP, or nothing for a nulled OP.
The address of the OP's first child, in hexadecimal.
The OP's flags, abbreviated as a series of symbols.
The numeric value of the OP's flags.
The sequence number of the OP, or a hyphen if it doesn't have one.
'NEXT', 'LAST', or 'REDO' if the OP is a target of one of those in exec mode, or empty otherwise.
The address of the OP's last child, in hexadecimal.
The OP's name.
The OP's name, in all caps.
The sequence number of the OP's next OP.
The address of the OP's next OP, in hexadecimal.
A one- or two-character abbreviation for the OP's name.
The OP's private flags, rendered with abbreviated names if possible.
The numeric value of the OP's private flags.
The sequence number of the OP. Note that this is a sequence number generated by B::Concise.
5.8.x and earlier only. 5.9 and later do not provide this.
The real sequence number of the OP, as a regular number and not adjusted to be relative to the start of the real program. (This will generally be a fairly large number because all of B::Concise is compiled before your program is).
Whether or not the op has been optimised by the peephole optimiser.
Only available in 5.9 and later.
Whether or not the op is statically defined. This flag is used by the B::C compiler backend and indicates that the op should not be freed.
Only available in 5.9 and later.
The address of the OP's next youngest sibling, in hexadecimal.
The address of the OP's SV, if it has an SV, in hexadecimal.
The class of the OP's SV, if it has one, in all caps (e.g., 'IV').
The value of the OP's SV, if it has one, in a short human-readable format.
The numeric value of the OP's targ.
The name of the variable the OP's targ refers to, if any, otherwise the letter t followed by the OP's targ in decimal.
Same as #targarg, but followed by the COP sequence numbers that delimit the variable's lifetime (or 'end' for a variable in an open scope) for a variable.
The numeric value of the OP's type, in decimal.
一般的な(そして本来の)B::Conciseの使用法はEXAMPLEで行っているように コマンドライン上での簡単なコードのレンダリングです. ですがB::Concise をコード上から呼び出し, compile() を直接, そして繰り返し呼び出すことが できます. これを行うためには O.pm のコンパイル時のみの操作を無効化する 必要があります. そしてデバッガを使ってB::Concise::compile自身を ステップ実行する必要があるかもしれません. (訳注:次の段落とうまく繋がらないから訳せてないかんじ) The common (and original) usage of B::Concise was for command-line renderings of simple code, as given in EXAMPLE. But you can also use B::Concise from your code, and call compile() directly, and repeatedly. By doing so, you can avoid the compile-time only operation of O.pm, and even use the debugger to step through B::Concise::compile() itself.
一度これを行えば, Conciseの出力に新しいレンダリングスタイルを加えたり, 新しい変数に張り付くコールバックルーティンを加えることで(追加した)スタイル から参照することも出来ます. Once you're doing this, you may alter Concise output by adding new rendering styles, and by optionally adding callback routines which populate new variables, if such were referenced from those (just added) styles.
use B::Concise qw(set_style add_callback);
add_style($yourStyleName => $defaultfmt, $gotofmt, $treefmt);
add_callback
( sub {
my ($h, $op, $format, $level, $stylename) = @_;
$h->{variable} = some_func($op);
});
$walker = B::Concise::compile(@options,@subnames,@subrefs);
$walker->();
set_style は3つの引数を受け取ります. そして行の形式を形成する 3つの書式(basic-exec, goto, tree)を更新します. ここには1つ重要ではない欠点があります. 新しい名前でスタイルを登録 することはできません. これは複数回のレンダリングやスタイルの切り替えを 行う時に問題になります. このため add_style() や set_style_standard() を代わりに使う方が好ましいでしょう. set_style accepts 3 arguments, and updates the three format-specs comprising a line-style (basic-exec, goto, tree). It has one minor drawback though; it doesn't register the style under a new name. This can become an issue if you render more than once and switch styles. Thus you may prefer to use add_style() and/or set_style_standard() instead.
標準ラインスタイル: terse, concise, linenoise, debug, env
のうち1つをアクティブにします(リストアします). これら以外にも add_style()
で定義したスタイル名も可能です.
This restores one of the standard line-styles: terse, concise,
linenoise, debug, env, into effect. It also accepts style
names previously defined with add_style().
この関数は新しいスタイル名と3つのスタイル引数を上にあるように受け取って, 作成, 登録, そして選択します. スタイルの再追加はエラーになります. スタイルの切り替えには set_style_standard() を使ってください. This subroutine accepts a new style name and three style arguments as above, and creates, registers, and selects the newly named style. It is an error to re-add a style; call set_style_standard() to switch between several styles.
新しく作ったスタイルが何か新しい #variable を参照するのなら, それらの変数を住まわせる(もしくは修正する)ためのコールバック関数を 定義する必要があります. それらは選択したスタイルで使うために 有効になります. If your newly minted styles refer to any new #variables, you'll need to define a callback subroutine that will populate (or modify) those variables. They are then available for use in the style you've chosen.
コールバックは Concise が各opcodeを辿る際に, 追加されたのと同じ 順番に呼び出されます. 各関数には5つのパラメータが渡されます. The callbacks are called for each opcode visited by Concise, in the same order as they are added. Each subroutine is passed five parameters.
1. A hashref, containing the variable names and values which are
populated into the report-line for the op
2. the op, as a B<B::OP> object
3. a reference to the format string
4. the formatting (indent) level
5. the selected stylename
1. ハッシュリファレンス, opに対してレポート行にある変数の名前と値.
2. op, B<B::OP> のオブジェクト.
3. 書式文字列へのリファレンス.
4. インデントレベル(字下げ幅).
5. 選択されているスタイル名.
独自の変数を定義するためには, 単にそれらをハッシュに追加, もしくは 必要であれば既存の値の変更と行うだけです. レベルと書式はスカラーへの リファレンスとして渡されますが, これを変更したり使ったりする必要は ないでしょう. To define your own variables, simply add them to the hash, or change existing values if you need to. The level and format are passed in as references to scalars, but it is unlikely that they will need to be changed or even used.
compile は前に "OPTIONS" [CPAN] で説明したオプションと, 関数リファレンスや関数名の引数を受け取ります. compile accepts options as described above in "OPTIONS" [CPAN], and arguments, which are either coderefs, or subroutine names.
これはオブジェクトを構成して $treewalker コードリファレンスを返します. これを呼び出すことでツリーを辿り歩いて引数に与えられたoptreeをSTDOUT にレンダリングします. これは再利用することも出来, 毎回レンダリングスタイル を変えることで新しいスタイルでレンダリングされていきます. It constructs and returns a $treewalker coderef, which when invoked, traverses, or walks, and renders the optrees of the given arguments to STDOUT. You can reuse this, and can change the rendering style used each time; thereafter the coderef renders in the new style.
walk_output は出力先をSTDOUTから他のファイルハンドルや渡された 文字列リファレンス(perlを-Uuseperlioでコンパイルした場合を除く)に 切り替えるものです. walk_output lets you change the print destination from STDOUT to another open filehandle, or into a string passed as a ref (unless you've built perl with -Uuseperlio).
my $walker = B::Concise::compile('-terse','aFuncName', \&aSubRef); # 1
walk_output(\my $buf);
$walker->(); # 1 renders -terse
set_style_standard('concise'); # 2
$walker->(); # 2 renders -concise
$walker->(@new); # 3 renders whatever
print "3 different renderings: terse, concise, and @new: $buf\n";
$walker を呼び出すと作成時に渡した関数を辿り, 現在のスタイルで レンダリングします. スタイルの変更はいくつかの方法があります: When $walker is called, it traverses the subroutines supplied when it was created, and renders them using the current style. You can change the style afterwards in several different ways:
1. call C<compile>, altering style or mode/order
2. call C<set_style_standard>
3. call $walker, passing @new options
1. スタイルやモード/順序を変えて C<compile> を呼び出す.
2. C<set_style_standard> を呼び出す.
3. $walker に @new オプションを渡す.
指定済みのスタイルを変更する一番簡単な方法は $walker に 新しいオプションを渡すことでしょう. そしてそれは compile を再度呼ぶ 以外で唯一レンダリング順序を変更する方法です. しかしレンダリング状態は 複数の $walker オブジェクト間で共有されているためマナーを守って 扱う必要があります. Passing new options to the $walker is the easiest way to change amongst any pre-defined styles (the ones you add are automatically recognized as options), and is the only way to alter rendering order without calling compile again. Note however that rendering state is still shared amongst multiple $walker objects, so they must still be used in a coordinated manner.
この関数(エクスポートはされません)は連番をリセットします. (これは 可読性のためのものが気まぐれになります.) この目的はテスト, つまり2つの無名関数(しかし異なるインスタンス)からの conciseの出力を比べるためでしょう. B::Concise はそれらを別々のoptree としてみるため, リセットしないことにはその出力の連番は異なってしまい ます. This function (not exported) lets you reset the sequence numbers (note that they're numbered arbitrarily, their goal being to be human readable). Its purpose is mostly to support testing, i.e. to compare the concise output from two identical anonymous subroutines (but different instances). Without the reset, B::Concise, seeing that they're separate optrees, generates different sequence numbers in the output.
検出されたエラー(不正な引数, 内部エラー等)は die($message) とされます. これらのエラーを検出して処理を継続する場合には, eval を使ってください. All detected errors, (invalid arguments, internal errors, etc.) are resolved with a die($message). Use an eval if you wish to catch these errors and continue processing.
特に, compile は存在しない関数名, 存在しない関数リファレンス, 関数以外のリファレンスを渡すとこれらは全て die となります. In particular, compile will die if you've asked for a non-existent function-name, a non-existent coderef, or a non-CODE reference.
Stephen McCamant, <smcc@CSUA.Berkeley.EDU>.