Perlランタイム内見

whoami
id:polamjag
- https://about.polamjag.info/
- 株式会社はてな Webアプリケーションエンジニア
- 最近はTypeScript, Terraform HCL, Perlの順で(Claudeが)読み書き
- 資格
- YAPC::Asia 2015 @ ビッグサイトがMy first YAPC

今日話さないこと
- Perlに入門する方法
本日のお品書き
YAPC::Fukuoka 2025でのトーク👇を踏まえて、
Perlのネイティブ拡張のためのしくみであるXSに滲み出ている部分から、Perlのランタイムの実装やアーキテクチャなどの雰囲気を見学しよう、という趣旨でやっていきます
XS入門の良かったところ(自己評価)
- いざというときにXS付きのライブラリを読みにいく気になる
- Perlランタイムの構造に多少は思いを馳せることができるようになる
- 不具合を踏み抜いたときにソースコードを覗きにいく気になる…かもしれない
- プロファイラの実行結果なんかを眺めるときの気持ちも多少は変わる…かもしれない
XS
例: Cpanel::JSON::XSという、こういう感じで使うCPANモジュール
use Cpanel::JSON::XS qw(encode_json); my $foo = { hello => 'from Perl hashref' }; # puts JSON string like `{"hello": "from Perl hashref"}` to stderr warn encode_json($foo);
の実装:
void encode_json (SV *scalar, SV *typesv = &PL_sv_undef) ALIAS: _to_json = 0 encode_json = F_UTF8 PPCODE: { JSON json; json_init (&json); json.flags |= ix; PUTBACK; scalar = encode_json (aTHX_ scalar, &json, typesv); SPAGAIN; XPUSHs (scalar); }
Perlランタイムの間取り図
- perlguts - Introduction to the Perl API
- perlsource - A guide to the Perl source tree
- DeepWiki https://deepwiki.com/Perl/perl5
- これが一番間取り図感がある気がする(?)
- ※図面と現況が異なる場合は現況を優先いたします
- https://deepwiki.com/Perl/perl5/2-core-architecture
- これが一番間取り図感がある気がする(?)
SV
void encode_json (SV *scalar, SV *typesv = &PL_sv_undef)
- SV = scalar value
(Perlブートキャンプ 32, 33ページより)
sv.h
sv.h#L246-L249
struct STRUCT_SV { /* struct sv { */ SV_HEAD_(void*); SV_HEAD_UNION_; };
sv.h#L207-L211
/* start with 2 sv-head building blocks */ #define SV_HEAD_(ptrtype) \ ptrtype sv_any; /* pointer to body */ \ U32 sv_refcnt; /* how many references to us */ \ U32 sv_flags /* what we are */
- FYI: Perlのメモリ管理はリファレンスカウント方式である
sv.h#L219-L232
#define SV_HEAD_UNION_ \ union { \ char* svu_pv; /* pointer to malloced string */ \ IV svu_iv; \ UV svu_uv; \ NV_BODYLESS_UNION_ \ SV* svu_rv; /* pointer to another SV */ \ SV** svu_array; \ HE** svu_hash; \ GP* svu_gp; \ PerlIO *svu_fp; \ SV** svu_fields; \ } sv_u \ SV_HEAD_DEBUG_
PUTBACK とか SPAGAIN
PUTBACK; scalar = encode_json (aTHX_ scalar, &json, typesv); SPAGAIN;
pp.h#L207
#define PUTBACK PL_stack_sp = sp
pp.h#L148
#define SPAGAIN sp = PL_stack_sp
spがローカル変数、PL_stack_spがランタイムの状態PUTBACK〜SPAGAINは、その間の処理により、sp/PL_stack_spの値を同期するためのイディオム
- https://deepwiki.com/Perl/perl5/2.2-execution-engine#stack-based-execution-model
aTHX_
PUTBACK; scalar = encode_json (aTHX_ scalar, &json, typesv); SPAGAIN;
- の
aTHX_ scalarって何?という話
perl.h#L259-L273
#ifdef MULTIPLICITY # define tTHX PerlInterpreter* /* Effectively this says not to warn even if the * context is unused. This makes sense; we often * have functions where that is currently a * placeholder in case it is ever needed. For * core, regen/embed.pl adds code for compilers * that don't understand this */ # define pTHX tTHX my_perl __attribute__unused__ # define aTHX my_perl # define aTHXa(a) aTHX = (tTHX)a # define dTHXa(a) pTHX = (tTHX)a # define dTHX pTHX = PERL_GET_THX # define pTHX_ pTHX, # define aTHX_ aTHX,
MULTIPLICITYとはなんぞや → https://perldoc.perl.org/perlguts#How-multiple-interpreters-and-concurrency-are-supportedaTHX_の_はカンマに見立てている(!)
というわけで、aTHX_ はPerl interpreterのコンテキストを引数として渡すためのマクロ
あらためてXS
おわかりいただけただろうか
void encode_json (SV *scalar, SV *typesv = &PL_sv_undef) ALIAS: _to_json = 0 encode_json = F_UTF8 PPCODE: { JSON json; json_init (&json); json.flags |= ix; PUTBACK; scalar = encode_json (aTHX_ scalar, &json, typesv); SPAGAIN; XPUSHs (scalar); }
説明を省いた部分はYAPC::Fukuoka 2025でのトークを見てくれ!
おまけコーナー
Perl
- https://www.perl.org/
- https://metacpan.org/dist/perl
- https://github.com/Perl/perl5
- いわゆる「仕様 = 実装」
- たとえばJavaScript(ECMAScript)の処理系にV8・SpiderMonkey・JavaScriptCoreなどがあるように、複数の処理系が広く使われている状況ではない
- オルタナティブなPerlの処理系を実装する試みがなされてこなかったわけではない
- "Only perl can parse Perl"
- たとえばJavaScript(ECMAScript)の処理系にV8・SpiderMonkey・JavaScriptCoreなどがあるように、複数の処理系が広く使われている状況ではない
"Only perl can parse Perl"
- Only
perl(処理系) can parse Perl (プログラミング言語)- 静的解析は実際とても難しい(構文解析と実行時の状態の境界があいまい)
- Perl and Undecidability (2008)
- 第27回 Perlにおける静的解析(1) | gihyo.jp (2014)
- Perl5の静的解析入門 / The static analysis of Perl5 - Speaker Deck (YAPC::Tokyo 2019)
- https://github.com/motemen/Camello (2026)
- 静的解析は実際とても難しい(構文解析と実行時の状態の境界があいまい)
- 実行時にキーワードや中置演算子などを勝手に増やせる(演算子オーバーロードではなく、**増やせる**)
- ランタイム本体のソースコードを改変する必要なく、モジュールからパーサーをフックできる
- XS::Parse::Infix::FromPerlで勝手にパイプライン演算子を追加する - ぱいぱいにっき
- https://metacpan.org/pod/XS::Parse::Infix
- "Support for custom infix operators as added in Perl development release v5.37.7, and is therefore present in Perl v5.38.0"
- Perl Hackers Hub | gihyo.jp は全体的におすすめ
Rubyとかならわかるんですけど…という方向けの比較用コーナー
*1:会場提供ありがとうございます


