设为首页 收藏本站
查看: 1367|回复: 0

[经验分享] The Perl Tutorial: Functions (4)

[复制链接]

尚未签到

发表于 2017-5-17 11:56:37 | 显示全部楼层 |阅读模式
Functions (函数)
  通常将可重复使用的代码组织起来形成函数。 Perl 用关键字 sub 声明函数,紧接着用 { 号开始 } 结束:
  Functions are used to organize your code into small pieces which can be reused. Perl declares functions using the sub keyword followed by the { sign to start the function and the } to end it:
  sub function1 {

CODE HERE

}
  调用 Perl 函数的方式:用 & 符号后紧随着函数名称。如果函数中包含有参数,应该将用圆括号将参数包含:
  In order to call a Perl function it's suggested that you call it using the & sign followed by the function name. If you have parameters, they can be also passed, so it's suggested that you enclose them in parentheses:
  &function1;
  在 Perl 中 , 函数作为过程被调用时不返回值 , 当然函数调用后也可以返回一个值(具体怎样操作,稍后将会讲述)。上面的示例代码所调用函数相当于一个过程,而下面的这个示例表示函数被调用后返回一个值(我们同样需要传参数值给函数)。
  In Perl, functions can serve as procedures which do not return a value (in reality they do, but that will be discussed later), or functions which do return a value. The above example is of a function that acts as a procedure. Below is an example of a function that acts as a function, because it returns a value (we are also passing values to the function):
  $answer = &add(1,2);
  传递给函数的参数值不必局限于标量,你也可以传递数组: @_
  You aren't limited to passing only scalar values to a function. You can also pass it arrays , but it will be easier to pass them at the end.
  Perl functions receive their values in an array : @_
  数组将其所存储的值全部传给函数,在上面的示例中,我们传递了两个值(数组形式),我们用下面的方式在函数中接收:
  This array holds all the values of the parameters passed to the function. So in the above example, where we had 2 values passed, we would retrieve them in the following manner:
  sub function1 {

($val1, $val2) = @_;

}
  或者也可以这么做:
  or we could do it in this manner:
  sub function1 {

$val1=$_[0];

$val2=$_[1];

}
  参数按值传递,意味着你正在使用的是原始参数的拷贝而不是原始参数本身。如果你想用引用的方式传递,必须直接使用变量的方式( $_[subscript] )。
  Perl 的变量范围与其它语言不同,在程序的开始不必声明变量,或者在函数中亦如此:你只要使用这些变量就可以了。变量的范围始终是全局的,如果你在一个函数使用一个变量,这个变量是全局的,它可以应用于其余的程序中。如果你想让这个变量仅在函数中可见,你必须声明它为一个局部变量(注:用关键字 local 指示一个局部变量):
  The parameters are being passed by value, meaning that you will be working with copies of the original parameters and not the actual parameters themselves. If you wish to pass them by reference, then you would have to work directly with the variable ($_[subscript]).
  Perl's scope of variables is different than most other languages. You don't have to declare your variables at the beginning of the program, or in the functions: you just use them. The scope of the variables is always global. If you use a variable in a function, it is global to the rest of the program. If you want the variable to be seen only by code within that particular function (and any other functions it may call), then you must declare it as a local variable:
  sub function1 {

local($myvar)

}
  上面的代码声明 $myvar 为局部变量,因此,它不能被其余程序所使用。
  The above code will declare $myvar as local, so it can't be seen by the rest of the program.
  函数同样可以相互嵌套调用自身,例如,递归函数。
  Functions can also be nested within each other, and you can create recursive functions that call themselves.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-378515-1-1.html 上篇帖子: Perl程序 拼接文件 下篇帖子: editplus中配置perl
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表