perl 子函数
来源:互联网 发布:艾瑞网怎么查数据 编辑:程序博客网 时间:2024/05/01 13:28
以下函数计算某年份是否为闰年
#!/usr/bin/perl
$my_year = 2000;
if ( is_leap_year( $my_year ) )
{ # Call function with an argument
print "$my_year is a leap year\n";
}
else
{
print "$my_year is not a leap year";
}
sub is_leap_year { # Function definition
my $year = shift(@_); # Shift off the year from
# the parameter list, @_
return ((($year % 4 == 0) &;& ($year % 100 != 0)) ||
($year % 400 == 0)) ? 1 : 0; # What is returned from the function
}
上例中my $year = 2000;
此代码中有一个关键字shift,给出相关解释。
在阅读别人别人的perll代码的时候经常发现在头部都是 ”
$变量=shift;”
首先我们查下看下perldoc中对于shift()的解释
perldoc -f shift
shift ARRAYshift Shifts the first value of the array off and returns it, shortening the array by 1 and moving everything down. If there are no elements in the array, returns the undefined value. If ARRAY is omitted, shifts the @_ array within the lexical scope of subroutines and formats, and the @ARGV array outside a subroutine and also within the lexical scopes established by the "eval STRING", "BEGIN {}", "INIT {}", "CHECK {}", "UNITCHECK {}" and "END {}" constructs. See also "unshift", "push", and "pop". "shift" and "unshift" do the same thing to the left end of an array that "pop" and "push" do to the right end.
shift off(可以理解为移除)数组中第一个变量并返回
perl中默认如果不指明参数那么就根据上下文获取默认参数
所以当我们定义这样一个pl文件
1.pl
$host = shift;
那么显然shift操作的参数为@_ 也就是 @ARGV
这句实现了直接获取用户传递的第一个参数。
总之就是shift没有数组作为参数时,就是移动@_这个默认的参数。
perl里经常用这种缺省方法的。
- perl 子函数
- Perl子函数参数传递
- Perl 子函数练习
- 打卡17-perl 子函数
- Perl 语言笔记(3) -- 子函数
- 如何在perl子函数中传递hash
- Perl学习笔记(2)默认变量,子函数,
- 子函数
- 子函数注意事项
- 汇编 子函数
- matlab 子函数
- 子函数配置
- 函数调用子函数,注意子函数的位置
- 子函数修改值
- 关于子函数调用
- Matlab定义子函数
- 数据库子函数练习
- 子函数返回类型int
- Servlet中操作文件
- aix下备份记录查询
- Android中BroadCastReceiver使用(整理)
- Oracle动态SQL
- 打印图形
- perl 子函数
- Android接口定义语言---AIDL(四)
- 配置Tomcat
- [SQL Server] sp_who, sp_who2和sp_who3
- 事务隔离级别
- ios 应用内支付,沙盒测试时出现“此Apple ID尚未在iTunes Store使用过。。。”
- 前后台参数传递(&,%处理)
- struts配置文件
- ios学习笔记(转)