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

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

问题描述

可能重复:
何时应使用&调用Perl子例程?

Possible Duplicate:
When should I use the & to call a Perl subroutine?

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

推荐答案

它具有非常特殊的用途:

It has very specific uses:

  • &告诉Perl忽略潜艇的原型.
  • &可以允许您使用呼叫者的@_. (&foo;,没有任何括号或参数).
  • goto &foo;defined &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).

由于内置函数不能以&开头,因此某些人(大多数是初学者)使用它来区分用户子对象和内置函数.

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天全站免登陆