Perl中的函数调用和goto& NAME有什么区别? [英] What is the difference between function call and goto &NAME in Perl?
问题描述
我正在阅读Perl,这很有趣.但是,在从Perl中阅读转到此处时,我对此表示怀疑.
I'm reading Perl which is quite interesting. But while reading goto from here in Perl I got a doubt.
我知道goto语句具有三种类型.
I know that goto statement has three types.
转到标签.
转到EXPR.
转到& NAME.
goto &NAME.
但是在这三种类型中,第三个goto &NAME
的用途是什么?
这似乎也像一个函数调用.
然后,
But in this three types, what is the use of third one goto &NAME
?
This is also seems to be like a function call.
Then,
- 在Perl中
goto &NAME
和普通function call
之间的真正区别是什么? - 当我们使用goto& NAME时?
- What is the real difference between
goto &NAME
and normalfunction call
in Perl? - When we use goto &NAME?
有人可以举例说明吗?
谢谢.
推荐答案
它在 goto中说页面
goto &NAME
形式与其他形式的goto
.实际上,这根本不是一般意义上的goto,并且 没有与其他标签相关的污名.
The
goto &NAME
form is quite different from the other forms ofgoto
. In fact, it isn't a goto in the normal sense at all, and doesn't have the stigma associated with other gotos.
然后按照您问题的答案
相反,它 退出当前子例程(丢失由
local()
设置的所有更改) 然后立即使用@_
的当前值.
Instead, it exits the current subroutine (losing any changes set by
local()
) and immediately calls in its place the named subroutine using the current value of@_
.
对于正常的函数调用,执行将在函数退出后在下一行继续执行.
With a normal function call the execution continues on the next line after the function exits.
该段的其余部分也很值得阅读,并回答了您的第二个问题
The rest of that paragraph is well worth reading as well, and answers your second question
由
AUTOLOAD
子例程使用,希望加载另一个子例程,然后假装该子例程已被首先调用(除了当前子例程中对@_
的任何修改都将传播到另一个子例程中) .)在goto
之后,甚至caller
都无法判断该例程是首先被调用的.
This is used by
AUTOLOAD
subroutines that wish to load another subroutine and then pretend that the other subroutine had been called in the first place (except that any modifications to@_
in the current subroutine are propagated to the other subroutine.) After thegoto
, not evencaller
will be able to tell that this routine was called first.
一个基本的例子.在某个地方定义了子例程deeper
,然后进行比较
A basic example. With a subroutine deeper
defined somewhere, compare
sub func_top {
deeper( @_ ); # pass its own arguments
# The rest of the code here runs after deeper() returns
}
使用
sub func_top {
goto &deeper; # @_ is passed to it, as it is at this point
# Control never returns here
}
在语句goto &deeper
中退出子func_top
.因此,在deeper
完成之后,控件将在func_top
调用之后返回.
从某种意义上说,func_top
被deeper
代替.
At the statement goto &deeper
the sub func_top
is exited. So after deeper
completes, the control returns to after the func_top
call.
In a sense, func_top
is replaced by deeper
.
尝试使用goto &func
传递参数会导致错误,即使仅对于goto &deeper()
也是如此.
Trying to pass arguments with goto &func
results in errors, even just for goto &deeper()
.
这篇关于Perl中的函数调用和goto& NAME有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!