Perl Learning - 6 (subroutine, function, my)
发布时间:2020-12-15 21:02:48 所属栏目:大数据 来源:网络整理
导读:In Perl Subroutines are the functions that user defines,Functions mean the buildin functions Perl designs. Like chomp(),reverse(),sort() and print(),they are functions. ? Keyword 'sub' is used to define a subroutine. ? sub marine { ??? $n+
In Perl Subroutines are the functions that user defines,Functions mean the buildin functions Perl designs.
Like chomp(),reverse(),sort() and print(),they are functions.
?
Keyword 'sub' is used to define a subroutine.
?
sub marine {
??? $n+=1; ??? print "Hello,sailor number $n!n"; }
?
Subroutine can be defined in any position of your perl program,but when there're two subroutines with the same name,the later one will cover ahead one,that's not a good progaming habit.
?
Use the subroutine name and a '&' before it to call your subroutine.
?
&marine;?# print Hello,sailor number 1!
&marine;?# print Hello,sailor number 2! &marine;?# print Hello,sailor number 3! &marine;?# print Hello,sailor number 4!
?
Every subroutine has a return value,if no 'return' expression is used,the last evaluated value of expression is the return value. But not all return values are valuable.
?
sub sum_of_fred_and_barney{
??? print "Hey,you called the sum_of_fred_and_barney subroutine!n"; ??? $fred + $barney;?# the return value }
?
A different example:
?
sub sum_of_fred_and_barney{
??? print "Hey,you called the sum_of_fred_and_barney subroutine!n"; ??? $fred + $barney;????# NOT the return value ??? print "Hey,I'm returning a value now!n";??? # the return value '1' }
?
The last expression is a print statement,it's valuated value is '1' (print successfully returns 1).
The last evaluated value doesn't have to be the last line of a subroutine. It just has to be last expression.
?
sub larger_of_fred_or_barney{
??? if($fred > $barney){ ?$fred; ??? }else{ ?$barney; ??? } }
?
Subroutines can have arguments,the way is putting list of auguments in pair of () after subroutine name.
?
$n=&max(10,15);?#two arguments
?
The arguments list will be put in array @_,subroutine can access @_ to determine numbers/values of arguments.
The first argument is in $_[0],then second $_[1] .. and so on... Those $_[ ] has nothing to do with $_,they just look like. We can write a subroutine &max,the improved &larger_of_fred_or_barney.
?
sub max{
??? if($_[0] > $_[1]){ ?$_[0]; ?}else{ ?$_[1]; ??? } }
?
The array @_ will be stored before a subroutine is called,and be recovered after the call.
So don't worry another @_ will be changed by a subroutine,like foreach won't change a scalar with same name of control variable.
?
By default,all variables are global. That means in any part of the perl grogram,those variables can be seen and used.
We can use 'my' to create a private variables.
?
sub max{
??? my($m,$n); ??? ($m,$n)=@_; ??? if($m>$n){$m}else{n} }
?
If the number of variables is bigger than 2,then the subroutine will ignore the unnecessary variables.
If the number of variables is less than 2,then $n will get undef (here similar to '0').
We can use if() the judge the number of variables in @_ like:
?
sub max{
??? if(@_!=2){ ??? print "WARNING! &max should get exactly two arguments!n"; ??? } .... .. }
?
It works,but actually there're better ways to let subroutine fit all situations,no matter how many variables.Will talk the way in next tip.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |