如果对 Perl 如何解析结构有疑问,您可以通过 B::Deparse 模块运行代码,该模块将从编译后的内部表示生成 Perl 源代码。对于您的第一个示例:
$ perl -MO=Deparse,-p -e '@ary = (1, 3, sort 4, 2); print @ary;'
(@ary = (1, 3, sort(4, 2)));
print(@ary);
-e syntax OK
如您所见,sort 将两个参数置于其右侧。
就执行顺序而言,您可以通过B::Concise 模块找到它(我已经添加了 cmets):
$ perl -MO=Concise,-exec -e '@ary = (1, 3, sort 4, 2); print @ary;'
1 <0> enter
2 <;> nextstate(main 1 -e:1) v:{
3 <0> pushmark s # start of list
4 <$> const[IV 1] s # 1 is added to list
5 <$> const[IV 3] s # 3 is added to list
6 <0> pushmark s # start of sort's argument list
7 <$> const[IV 4] s # 4 is added to sort's argument list
8 <$> const[IV 2] s # 2 is added to sort's argument list
9 <@> sort lK # sort is run, and returns its list into the outer list
a <0> pushmark s
b <#> gv[*ary] s
c <1> rv2av[t2] lKRM*/1
d <2> aassign[t3] vKS/COMMON # the list is assigned to the array
e <;> nextstate(main 1 -e:1) v:{
f <0> pushmark s # start of print's argument list
g <#> gv[*ary] s # the array is loaded into print's argument list
h <1> rv2av[t5] lK/1
i <@> print vK # print outputs it's argument list
j <@> leave[1 ref] vKP/REFC
-e syntax OK
第二个例子:
$ perl -MO=Deparse,-p -e 'print $foo, exit;'
print($foo, exit);
-e syntax OK
$ perl -MO=Concise,-exec -e 'print $foo, exit;'
1 <0> enter
2 <;> nextstate(main 1 -e:1) v:{
3 <0> pushmark s
4 <#> gvsv[*foo] s # add $foo to the argument list
5 <0> exit s # call `exit` and add its return value to the list
6 <@> print vK # print the list, but we never get here
7 <@> leave[1 ref] vKP/REFC
-e syntax OK
如您所见,exit 内置函数在尝试为print 组装参数列表时运行。由于exit 导致程序退出,print 命令永远无法运行。
最后一个:
$ perl -MO=Deparse,-p -e 'print ($foo & 255) + 1, "\n";'
((print(($foo & 255)) + 1), '???'); # '???' means this was optimized away
-e syntax OK
$ perl -MO=Concise,-exec -e 'print ($foo & 255) + 1, "\n";'
1 <0> enter
2 <;> nextstate(main 1 -e:1) v:{
3 <0> pushmark v
4 <0> pushmark s
5 <#> gvsv[*foo] s
6 <$> const[IV 255] s
7 <2> bit_and[t2] sK
8 <@> print sK
9 <$> const[IV 1] s
a <2> add[t3] vK/2
b <@> list vK
c <@> leave[1 ref] vKP/REFC
-e syntax OK