美文网首页
编写perl module 并调用

编写perl module 并调用

作者: 余绕 | 来源:发表于2020-04-18 15:27 被阅读0次

如下为包含 &sum 和 &max两个子函数的module:module.pm

package module;

#sum 子函数
sub sum{
$a=shift @_;
$b=shift @_;
$c=shift @_;
$d=$c+$a+$b;
return $d;
}

#max子函数
sub max{

$a=shift @_;
$b=shift @_;
$c=shift @_;

my $temp;

$temp= $a>$b?$a:$b; #expression?A:B:三目操作符,如果判断语句expression正确则取值A,如果错误则取值B

$temp>$c?$temp:$c;

}
1;#module的结尾必须为1,这是规定!

写完module之后保存在桌面上

使用module

use lib "C:/Users/Tao/Desktop"; #设置module的目录

use module; #使用module.pm这里不用加.pm

@a=qw/1112 3121 2102/;

$sum=module::sum(@a);

$max=module::max(@a);

print "sum of the ". '@a'. " is $sum\n"."$max is the biggest in the ".'@a'."\n";

#程序运行结果如下:
sum of the @a is 6335
3121 is the biggest in the @a

相关文章

网友评论

      本文标题:编写perl module 并调用

      本文链接:https://www.haomeiwen.com/subject/xpcbvhtx.html