gpt4 book ai didi

perl - Perl 中的函数调用和 goto &NAME 有什么区别?

转载 作者:行者123 更新时间:2023-12-04 17:07:42 30 4
gpt4 key购买 nike

我正在阅读非常有趣的 Perl。但是在阅读时goto from here在 Perl 中,我有一个疑问。
我知道 goto 语句有三种类型。

goto LABEL.

goto EXPR.

goto &NAME.


但在这三种类型中,第三种有什么用 goto &NAME ?
这似乎也像是一个函数调用。
然后,
  • goto &NAME真正的区别是什么和正常 function call在 Perl 中?
  • 当我们使用 goto &NAME?

  • 任何人都可以请举例说明。
    提前致谢。

    最佳答案

    它在 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.



    然后按照你的问题的答案

    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 @_.



    对于正常的函数调用,函数退出后会在下一行继续执行。

    该段落的其余部分也非常值得一读,并回答了您的第二个问题

    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在某处定义,比较
    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 &deeperfunc_top退出。所以在 deeper之后完成,控制返回到 func_top之后称呼。
    从某种意义上说, func_topdeeper 取代.

    试图用 goto &func 传递参数导致错误,即使只是对于 goto &deeper() .

    关于perl - Perl 中的函数调用和 goto &NAME 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40714465/

    30 4 0