目次
B - Perl コンパイラ
use B;
B モジュールは Perl プログラムに自分自身の体内を探るための
クラス群を提供します. Perl コンパイラの"バックエンド"の実装にも
このモジュールが使われています. 単にコンパイラの使い方であれば
このモジュールの知識を必要としません: ユーザから見える部分に
関しては O モジュールを参照してください. B モジュールは
新しいコンパイラバックエンドを書く人のためのモジュールです.
このドキュメントでは読み手がperlの内部構造, SV, OP, 内部
シンボルテーブル, プログラムの構文ツリーなどに相当詳しいことを
前提としています.
B モジュールはPerlインタプリタの現在の状態を問い合わせる
ためのユーティリティ関数群を含んでいます. これらの関数の
多くは B::SV 及び B::OP クラス及びこれらの派生クラスのオブ
ジェクトを返します. これらのクラスはそれらの中でそれらの
内部状態に関して問い合わせるためのメソッドを定義しています.
B モジュールは様々な関数をエクスポートします. いくつかは
簡単なユーティリティ関数ですが, それ以外は内部オブジェクトの
入り口となる"ハンドル" (initial "handle")を取得する方法を
提供します.
B::SV 若しくは B::AV, B::HV, B::CV オブジェクトを返す関数
これらのクラスの階層図及び呼び出せるメソッドに関しては, 後述の "OVERVIEW OF CLASSES" 及び "SV-RELATED CLASSES" を参照してください.
Cでの変数sv_undefに対応するSVオブジェクトを返します.
Cでの変数sv_yesに対応するSVオブジェクトを返します.
Cでの変数sv_noに対応するSVオブジェクトを返します.
何らかの Perl の値へのリファレンスを受け取り, 参照されている値を
対応する B:OP もしくは B::SV クラス派生のオブジェクトに変換します.
main_root の様な関数は別として, これはperlの内部データ構造
の入り口となる"ハンドル"(initial "handle")を取得する主要な方法です.
ここから他のアクセスメソッドを使うことになります.
返されるオブジェクトはOPやSVが存在しているのと同じ間だけ有効です. 参照する構造が解放された後はそのオブジェクトを使わないでください.
Cでの変数amagic_generationに対応するSVオブジェクトを返します.
INIT ブロックを表現する AV オブジェクト(つまりB::AVクラスの オブジェクト)を返します.
CHECK ブロックを表現する AV オブジェクト(つまりB::AVクラスの オブジェクト)を返します.
BEGIN ブロックを表現する AV オブジェクト(つまりB::AVクラスの オブジェクト)を返します.
END ブロックを表現する AV オブジェクト(つまりB::AVクラスの オブジェクト)を返します.
グローバルcomppadlistの AV オブジェクト(つまりB::AVクラスの オブジェクト)を返します.
perl が ithreads でコンパイルされたときのみ.
Perlプログラムのメイン部分に対応する(擬似的な)CVを返します.
SYMREF からシンボルテーブルを歩き始め, 各シンボル(B::GVオブジェクト) に着く度にMETHODを呼び出します. 巡回がパッケージシンボルに辿り 着いたときシンボル名を引数にRECURSEを呼び出し, 関数が真を返した 時にのみ再帰的にパッケージを進みます.
PREFIX は巡回するSYMREFの名前です.
例えば:
# Walk CGI's symbol table calling print_subs on each symbol.
# Recurse only into CGI::Util::
walksymtable(\%CGI::, 'print_subs', sub { $_[0] eq 'CGI::Util::' },
'CGI::');
print_subs() は 自分で宣言した B::GV メソッドです. 後述の"B::GV Methods"を参照してください.
B::OP オブジェクトを返す関数及びopツリーの巡回
これらのクラスの階層図及び呼び出せるメソッドに関しては, 後述の "OVERVIEW OF CLASSES" 及び "SV-RELATED CLASSES" を参照してください.
Perlプラグラムのメイン部分に当たるルートop(つまり適当な B::OP から 派生したクラスのオブジェクト)を返します.
Perl プラグラムのメイン部分の始まるopを返します.
OP から構文ツリーを辿って各opでMETHODを呼び出します. ノードは子の
前に呼び出されます. walkoptree_debug(後述)がデバッグを有効に
するように呼び出されていたら, 各opでMETHODが呼び出される前に
walkoptree_debug が呼び出されます.
現在の walkoptree のデバッグフラグを返します. もし任意の引数
DEBUG が0でなければ, デバッグフラグをその値に設定します.
デバッグフラグが何を行うのかは前述のwalkoptreeを参照してください.
op番号OPNUMのPP関数名(例えば "pp_add")を返します.
文字列STRに対するperlが使うハッシュ値を "0x..." という形式の 文字列で返します.
I を perl内部の I32 型にキャストします.
-c コマンドラインオプションと同じことをします. 明らかなことですが,
これは BEGIN ブロックの中でのみ有効であって, それ以外では遅すぎます.
Cソースコードで文字列として使えるようにダブルクオートで囲んだ バージョンのSTRを返します.
Perlソースコードで文字列として使えるようにダブルクオートで囲んだ バージョンのSTRを返します.
オブジェクトのクラスを, 最初の"::"の前に当たる部分を省いて返します.
スレッド対応でコンパイルされているperlで, 特殊なスレッド毎のthreadsv 変数のリストを返します.
The C structures used by Perl's internals to hold SV and OP
information (PVIV, AV, HV, ..., OP, SVOP, UNOP, ...) are modelled on a
class hierarchy and the B module gives access to them via a true
object hierarchy. Structure fields which point to other objects
(whether types of SV or types of OP) are represented by the B
module as Perl objects of the appropriate class.
The bulk of the B module is the methods for accessing fields of
these structures.
Note that all access is read-only. You cannot modify the internals by using this module. Also, note that the B::OP and B::SV objects created by this module are only valid for as long as the underlying objects exist; their creation doesn't increase the reference counts of the underlying objects. Trying to access the fields of a freed object will give incomprehensible results, or worse.
B::IV, B::NV, B::RV, B::PV, B::PVIV, B::PVNV, B::PVMG, B::BM, B::PVLV, B::AV, B::HV, B::CV, B::GV, B::FM, B::IO. These classes correspond in the obvious way to the underlying C structures of similar names. The inheritance hierarchy mimics the underlying C "inheritance". For 5.9.1 and later this is:
B::SV
|
+--------------+----------------------+
| | |
B::PV B::IV B::RV
| \ / \
| \ / \
| B::PVIV B::NV
\ /
\____ __/
\ /
B::PVNV
|
|
B::PVMG
|
+-----+----+------+-----+-----+
| | | | | |
B::BM B::AV B::GV B::HV B::CV B::IO
| |
B::PVLV |
B::FM
For 5.9.0 and earlier, PVLV is a direct subclass of PVMG, so the base of this diagram is
|
B::PVMG
|
+------+-----+----+------+-----+-----+
| | | | | | |
B::PVLV B::BM B::AV B::GV B::HV B::CV B::IO
|
|
B::FM
Access methods correspond to the underlying C macros for field access,
usually with the leading "class indication" prefix removed (Sv, Av,
Hv, ...). The leading prefix is only left in cases where its removal
would cause a clash in method name. For example, GvREFCNT stays
as-is since its abbreviation would clash with the "superclass" method
REFCNT (corresponding to the C function SvREFCNT).
Returns a reference to the regular scalar corresponding to this B::SV object. In other words, this method is the inverse operation to the svref_2object() subroutine. This scalar and other data it points at should be considered read-only: modifying them is neither safe nor guaranteed to have a sensible effect.
Returns the value of the IV, interpreted as
a signed integer. This will be misleading
if FLAGS & SVf_IVisUV. Perhaps you want the
int_value method instead?
This method returns the value of the IV as an integer.
It differs from IV in that it returns the correct
value regardless of whether it's stored signed or
unsigned.
This method is the one you usually want. It constructs a string using the length and offset information in the struct: for ordinary scalars it will return the string that you'd see from Perl, even if it contains null characters.
Same as B::RV::RV, except that it will die() if the PV isn't a reference.
This method is less often useful. It assumes that the string stored in the struct is null-terminated, and disregards the length information.
It is the appropriate method to use if you need to get the name of a lexical variable from a padname array. Lexical variable names are always stored with a null terminator, and the length field (SvCUR) is overloaded for other purposes and can't be relied on here.
Only valid on r-magic, returns the string that generated the regexp.
Will die() if called on r-magic.
Only valid on r-magic, returns the integer value of the REGEX stored in the MAGIC.
This method returns TRUE if the GP field of the GV is NULL.
This method returns the name of the glob, but if the first character of the name is a control character, then it converts it to ^X first, so that *^G would return "^G" rather than "\cG".
It's useful if you want to print out the name of a variable.
If you restrict yourself to globs which exist at compile-time
then the result ought to be unambiguous, because code like
${"^G"} = 1 is compiled as two ops - a constant string and
a dereference (rv2gv) - so that the glob is created at runtime.
If you're working with globs at runtime, and need to disambiguate *^G from *{"^G"}, then you should use the raw NAME method.
Takes one arguments ( 'stdin' | 'stdout' | 'stderr' ) and returns true if the IoIFP of the object is equal to the handle whose name was passed as argument ( i.e. $io->IsSTD('stderr') is true if IoIFP($io) == PerlIO_stdin() ).
Like ARRAY, but takes an index as an argument to get only one element,
rather than a list of all of them.
For constant subroutines, returns the constant SV returned by the subroutine.
B::OP, B::UNOP, B::BINOP, B::LOGOP, B::LISTOP, B::PMOP,
B::SVOP, B::PADOP, B::PVOP, B::LOOP, B::COP.
これらのクラスは似た名前のC構造体と対応しています. 疑似継承構造はCの"継承"に基を成します.
B::OP
|
+---------------+--------+--------+
| | | |
B::UNOP B::SVOP B::PADOP B::COP
,' `-.
/ `--.
B::BINOP B::LOGOP
|
|
B::LISTOP
,' `.
/ \
B::LOOP B::PMOP
C構造体のフィールドにアクセスするときは, "クラス指示" 接頭辞("op_")
を取り除いたメソッドを使います.
これらのメソッドでは OP データ構造の似た名前のフィールドの値を
取得します. より詳細な情報は op.h の最初を参照してください.
これはop名を文字列で返します(例: "add", "rv2av").
これは関数名を文字列で返します(例: "PL_ppaddr[OP_ADD]", "PL_ppaddr[OP_RV2AV]").
これはopの説明をCのグローバルPL_op_desc配列から返します(例: "addition" "array deref").
Only when perl was compiled with ithreads.
Malcolm Beattie, mbeattie@sable.ox.ac.uk