要定义自己的子程序,使用关键字sub,子程序的名字(无&这个符号),组成子程序的缩进的代码块(花括号中),如:
#!/usr/bin/perl
$n = 0;
sub add{
$n += 1;
print "Hello, sailor number $n\n";
}
&add;
&add;
sub add_a_b{
print "the sum is \n";
$f + $b;
}
sub larger_one{
if($f > $b){
$f;
}else{
$b;}
}
$f = 3;
$b = 4;
$sum = &add_a_b;
print $sum . "\n";
$larger = &larger_one;
print "the larger one is $larger\n";
阅读(478) | 评论(0) | 转发(0) |