微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我怎样才能在Perl源代码中找到某些功能的实现?

例如,我想找到’print’或’foreach’运算符的源代码.
我已经下载了Perl源代码,希望看到这个运算符的“真实”代码.

解决方法

Perl将源代码编译为名为Opcode Tree的图形.同时,此数据结构表示程序的语法和控制流程.要理解操作码,您可能需要从 Illustrated Perl Guts (illguts)开始.

要了解您的程序编译的Ops,您可以这样称呼它:

> perl -MO = Concise script.pl – 在语法树中获取操作码
> perl -MO = Concise,-exec script.pl – -exec选项命令操作执行顺序.有时,这不那么令人困惑.
> perl -MO = Concise,foo script.pl – 转储foo子例程的ops.

典型的操作码看起来像:

4 <$> const[PV "007B"] s/FOLD ->5
^  ^  ^                ^      ^
|  |  |                |      The next op in execution order
|  |  |                Flags for this op,documented e.g. in illguts. "s" is
|  |  |                scalar context. After the slash,op-specific stuff
|  |  The actual name of the op,may list further arguments
|  The optype ($: unop,2: binop,@:listop) – not really useful
The op number

Ops声明为PP(pp_const).要搜索该声明,请使用ack tool,这是一个带有Perl正则表达式的智能递归grep.要搜索代码顶部的所有C文件标题,我们会:

$ack 'pp_const' *.c *.h

输出(这里没有颜色):

op.c
29: * points to the pp_const() function and to an SV containing the constant
30: * value. When pp_const() is executed,its job is to push that SV onto the

pp_hot.c
40:PP(pp_const)

opcode.h
944:    Perl_pp_const,pp_proto.h
43:PERL_CALLCONV OP *Perl_pp_const(pTHX);

所以它在pp_hot.c,第40行声明.我倾向于使用vim pp_hot.c 40去那里.然后我们看到定义:

PP(pp_const)
{
    dVAR;
    dSP;
    XPUSHs(cSVOP_sv);
    RETURN;
}

要理解这一点,你应该对Perl API有一点了解,也许可以写一点XS.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐