perl 解释器是逐行还是一起解析代码?
Does the perl interpreter parse code line-by-line or all together?
根据我对perl解释器的理解,代码首先被解析成opcode。然后在执行期间解释此操作码图。我想知道解析是逐行发生还是一起发生。
我有一些代码在开头带有 exit
语句,但是当我 运行 脚本时,perl 报告了一个位于 exit
语句下方的错误。 (错误是缺少分号。)如果 perl 解释器逐行工作,它如何报告位于 exit 语句下方的错误?还是解析阶段报错?
If the perl interpreter works line-by-line how can it report an error that lies below the exit statement? Or is the error reported during the parsing stage?
相关错误是在 parsing/compilation 阶段报告的。即使您使用了 -c
也会被报告。这些被称为 "compile-time errors".
在那个阶段无法检测到某些错误。那些叫做 "runtime errors".
According to my understanding of the perl interpreter, the code is first parsed into an opcode. This opcode graph is then interpreted during execution. I want to know if the parsing happens line-by-line or all together.
文件整体编译,然后编译后的形式从头开始执行。 BEGIN
和 use
语句偏离此;它们在编译后立即执行(即在编译文件的其余部分之前)。
$ perl -e'
BEGIN { print "Start of compilation.\n"; }
print "Start of execution.\n";
# ...
BEGIN { print "End of compilation.\n"; }
print "End of execution.\n";
'
Start of compilation.
End of compilation.
Start of execution.
End of execution.
使用-c
会导致Perl 在开始执行之前退出。 (BEGIN
和 use
语句仍然正常执行。)
$ perl -c -e'
print "This statement was executed.\n"
my $x = 4;
$x += 5;
print "$x\n";
BEGIN { print "This statement was compiled.\n"; }
'
This statement was compiled.
-e syntax OK
编译结果如下所示:
$ perl -MO=Concise,-exec -e'
my $x = 4;
$x += 5;
print "$x\n";
'
1 <0> enter
2 <;> nextstate(main 1 -e:2) v:{
3 <$> const[IV 4] s
4 <0> padsv[$x:1,2] sRM*/LVINTRO
5 <2> sassign vKS/2
6 <;> nextstate(main 2 -e:3) v:{
7 <0> padsv[$x:1,2] sRM
8 <$> const[IV 5] s
9 <2> add[t2] vKS/2
a <;> nextstate(main 2 -e:4) v:{
b <0> pushmark s
c <0> padsv[$x:1,2] s
d <$> const[PV "\n"] s
e <2> concat[t3] sK/2
f <@> print vK
g <@> leave[1 ref] vKP/REFC
-e syntax OK
Perl 的伟大之处在于编译器和解释器工作 hand-in-hand。编译后的代码可以通过编译过程part-way执行,解释器可以在运行时
请求代码被编译
一般来说,一个程序会被编译character-by-character然后执行,但是如果编译器遇到BEGIN
块(或者use
语句,它的工作方式类似于BEGIN
) 然后调用 Perl 解释器在编译程序的其余部分之前立即执行该块
一旦编译器到达文件末尾,代码就会由解释器执行。但是 运行 时间 Perl 也可能使用 eval
来调用编译器
如果主源文件中的任何地方存在致命错误,例如缺少分号,那么 Perl 编译器会在解释器开始执行编译后的代码之前立即报告错误
根据我对perl解释器的理解,代码首先被解析成opcode。然后在执行期间解释此操作码图。我想知道解析是逐行发生还是一起发生。
我有一些代码在开头带有 exit
语句,但是当我 运行 脚本时,perl 报告了一个位于 exit
语句下方的错误。 (错误是缺少分号。)如果 perl 解释器逐行工作,它如何报告位于 exit 语句下方的错误?还是解析阶段报错?
If the perl interpreter works line-by-line how can it report an error that lies below the exit statement? Or is the error reported during the parsing stage?
相关错误是在 parsing/compilation 阶段报告的。即使您使用了 -c
也会被报告。这些被称为 "compile-time errors".
在那个阶段无法检测到某些错误。那些叫做 "runtime errors".
According to my understanding of the perl interpreter, the code is first parsed into an opcode. This opcode graph is then interpreted during execution. I want to know if the parsing happens line-by-line or all together.
文件整体编译,然后编译后的形式从头开始执行。 BEGIN
和 use
语句偏离此;它们在编译后立即执行(即在编译文件的其余部分之前)。
$ perl -e'
BEGIN { print "Start of compilation.\n"; }
print "Start of execution.\n";
# ...
BEGIN { print "End of compilation.\n"; }
print "End of execution.\n";
'
Start of compilation.
End of compilation.
Start of execution.
End of execution.
使用-c
会导致Perl 在开始执行之前退出。 (BEGIN
和 use
语句仍然正常执行。)
$ perl -c -e'
print "This statement was executed.\n"
my $x = 4;
$x += 5;
print "$x\n";
BEGIN { print "This statement was compiled.\n"; }
'
This statement was compiled.
-e syntax OK
编译结果如下所示:
$ perl -MO=Concise,-exec -e'
my $x = 4;
$x += 5;
print "$x\n";
'
1 <0> enter
2 <;> nextstate(main 1 -e:2) v:{
3 <$> const[IV 4] s
4 <0> padsv[$x:1,2] sRM*/LVINTRO
5 <2> sassign vKS/2
6 <;> nextstate(main 2 -e:3) v:{
7 <0> padsv[$x:1,2] sRM
8 <$> const[IV 5] s
9 <2> add[t2] vKS/2
a <;> nextstate(main 2 -e:4) v:{
b <0> pushmark s
c <0> padsv[$x:1,2] s
d <$> const[PV "\n"] s
e <2> concat[t3] sK/2
f <@> print vK
g <@> leave[1 ref] vKP/REFC
-e syntax OK
Perl 的伟大之处在于编译器和解释器工作 hand-in-hand。编译后的代码可以通过编译过程part-way执行,解释器可以在运行时
请求代码被编译一般来说,一个程序会被编译character-by-character然后执行,但是如果编译器遇到BEGIN
块(或者use
语句,它的工作方式类似于BEGIN
) 然后调用 Perl 解释器在编译程序的其余部分之前立即执行该块
一旦编译器到达文件末尾,代码就会由解释器执行。但是 运行 时间 Perl 也可能使用 eval
来调用编译器
如果主源文件中的任何地方存在致命错误,例如缺少分号,那么 Perl 编译器会在解释器开始执行编译后的代码之前立即报告错误