Perl用很多默认操作和习惯用法,如果对某些代码不确定,perl编译器的真实理解方式,可以用Deparse模块反汇编看一下。
比如下面代码:
while(<STDIN>){ print "hello "; }
perl编译器会默认对<STDIN>加上defined关键词,来保证<STDIN>接收的不是空字符之类的代表假的布尔值。我们可以用反汇编手段验证一下:
1、把以上内容保存到t.pl文件中。
fly@noi:~$ perl -MO=Deparse t.pl while (defined($_ = readline STDIN)) { print "hello "; } 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的表达式的解析(那个表达式优先执行)。