使用B::Deparse模块对perl代码反汇编

Perl用很多默认操作和习惯用法,如果对某些代码不确定,perl编译器的真实理解方式,可以用Deparse模块反汇编看一下。

比如下面代码:

while(<STDIN>){
    print "hello\n";
}

perl编译器会默认对<STDIN>加上defined关键词,来保证<STDIN>接收的不是空字符之类的代表假的布尔值。我们可以用反汇编手段验证一下:

1、把以上内容保存到t.pl文件中。

fly@noi:~$ perl -MO=Deparse t.pl
while (defined($_ = readline STDIN)) {
    print "hello\n";
}
t.pl syntax OK     #连语法是否正确也顺便检测了。

2、官方表述:

B::Deparse is a backend module for the Perl compiler that generates perl source code, based on the internal compiled structure that perl itself creates after parsing a program. The output of B::Deparse won't be exactly the same as the original source, since perl doesn't keep track of comments or whitespace, and there isn't a one-to-one correspondence between perl's syntactical constructions and their compiled form, but it will often be close. When you use the -p option, the output also includes parentheses even when they are not required by precedence, which can make it easy to see if perl is parsing your expressions the way you intended.

B::Deparse是一个用于perl编译器生成perl源代码的后端模块,它基于内部编译结构(perl在解析一个程序之后生成的结构)。B::Deparse不会生成和原代码相同的输出。因为perl没有跟踪注释和空白,并且生成的代码和源代码也不是一一对应的关系。但它常常会被关闭(不知道它指的什么?)。当你使用-p选项,输出总是包括园括号,甚至它们是不需要的(没有优先级问题),但是,这样做能更清楚的查看perl的表达式的解析(那个表达式优先执行)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值