perl 中 &function 和 function() 的区别 [英] Difference between &function and function() in perl

查看:31
本文介绍了perl 中 &function 和 function() 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
我应该什么时候使用 &调用 Perl 子程序?

在 perl 脚本中,为什么有时会以不同的方式编写调用函数的方法.我看到 &function 有时还有 function().他们都是一样的,这只是一种人们想要炫耀的风格吗?如果它们相同,为什么它们都可用,难道一个就足够了吗?我猜这两种方法之间存在一些语义差异,可以将它们区分开来......但是在什么样的情况下?

In perl scripts, why is that method of invoking the function is written differently some times. I see &function and sometimes function(). Are they both the same and this is just a style that one would want to flaunt? If they are the same, why are they both available, would not one just suffice? I am guessing there is some semantic difference between the both the methods which distinguishes them from each other ... but at what kind of circumstances?

---由于超时原因我无法回答我自己的问题-我正在更新问题本身部分的答案.当我有机会更新答案块时,我会把它放在那里.

--- Since I cannot answer my own question for timeout reasons -- I am updating the answer in the section of question itself. When I get a chance to update the answer block, I will put it there.

我在Learning Perl"一书中找到了相关的文字……不过还是谢谢你的提示.第 4 章:子程序——省略与号.

I found the relevant text in 'Learning Perl' book..thanks for the tip though. Chapter 4: Subroutines -- Omitting the Ampersand.

我对 perl 函数的 & 符号 & 用法更感兴趣.如果在调用之前已经定义了子例程,则可以在调用函数时不使用 & 调用子例程,类似于调用内置函数.&如果要调用的函数使用与其中一个内置函数相同的名称,并且在调用之前已定义,则还用于区分内置函数和用户定义的函数.

I was more interested in ampersand & usage for perl functions. If a subroutine is already defined before being invoked, then subroutine can be invoked without using & while calling the function similar to invoking the builtin functions. & is also used to distinguish between the builtin functions and the user defined functions if the function to be invoked uses the same name that of one of the builtin function, provided it is defined before being invoked.

()的用法,只是为了证明将参数传递给子程序是合理的,而如果不使用,则以@_形式传递当前参数的默认列表.如果在 () 中为子例程指定了参数,则假定它是一个子例程并且即使在解析时之前未定义也会被调用.

Usage of (), is merely to justify the passing of the arguments to the subroutines, while if not used, the default list of current arguments are passed in the form @_. If the arguments are specified in () for a subroutine, it is assumed to be a subroutine and is invoked even if not previously defined while parsing.

推荐答案

它有非常具体的用途:

  • & 告诉 Perl 忽略 sub 的原型.
  • & 可以让你使用调用者的@_.(&foo; 没有括号或参数).
  • goto &foo;定义的 &foo.
  • 获取引用(例如 &foo).
  • & tells Perl to ignore the sub's prototype.
  • & can allow you to use the caller's @_. (&foo; with no parens or arguments).
  • goto &foo; and defined &foo.
  • Getting a reference (e.g. &foo).

有些人(主要是初学者)用它来区分用户 subs 和内置函数,因为内置函数不能以 & 开头.

Some people (mostly beginners) use it to distinguish user subs from builtin functions, since builtin functions cannot be preceded by &.

这篇关于perl 中 &function 和 function() 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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