Perl中的函数调用和goto& NAME有什么区别? [英] What is the difference between function call and goto &NAME in Perl?

查看:122
本文介绍了Perl中的函数调用和goto& NAME有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读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,

  1. 在Perl中goto &NAME和普通function call之间的真正区别是什么?
  2. 当我们使用goto& NAME时?
  1. What is the real difference between goto &NAME and normal function call in Perl?
  2. When we use goto &NAME?

有人可以举例说明吗?

谢谢.

推荐答案

它在 goto中说页面

goto &NAME形式与其他形式的 goto.实际上,这根本不是一般意义上的goto,并且 没有与其他标签相关的污名.

The goto &NAME form is quite different from the other forms of goto. 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 the goto, not even caller 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_topdeeper代替.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆