understanding subroutines
subroutines(sometimes called "functions") are a means of encapsulating code.
subroutines are reusable.
subroutines can hide complexity.
subroutines can call other subroutines.
subroutines can call themselves.
this is called "recursion"http://www.cnblogs.com/hanleilei/admin/EditPosts.aspx?IsDraft=1
defining.pl
1
#
!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use subs qw( message error);#加上这个语句,就可以在调用函数的时候去掉括号,否则只能把函数定义放在调用前面
6
7 main( @ARGV);
8
9 sub main
10 {
11 message " This is the template.pl exercise file from Perl 5 Essential Training. ";
12 message " message two ";
13 error " message three ";
14 }
15
16 sub message
17 {
18 my $m = shift or return;
19 print( " $m\n ");
20 }
21
22 sub error
23 {
24 my $e = shift || ' unkown error ';
25 print( " $0: $e\n ");
26 exit 0;
27 }
2
3 use strict;
4 use warnings;
5 use subs qw( message error);#加上这个语句,就可以在调用函数的时候去掉括号,否则只能把函数定义放在调用前面
6
7 main( @ARGV);
8
9 sub main
10 {
11 message " This is the template.pl exercise file from Perl 5 Essential Training. ";
12 message " message two ";
13 error " message three ";
14 }
15
16 sub message
17 {
18 my $m = shift or return;
19 print( " $m\n ");
20 }
21
22 sub error
23 {
24 my $e = shift || ' unkown error ';
25 print( " $0: $e\n ");
26 exit 0;
27 }
注意:上面的例子的message语句是没有括号的,因为从一开始先申明了调用message的子函数,否则需要把申明子函数放在main函数之前。
argument.pl
#
!/usr/bin/perl
#
use strict;
use warnings;
main( @ARGV);
sub main
{
my $s = " This is the template.pl exercise file from Perl 5 Essential Training. ";
my $y = " 42 ";
my $z = " zee ";
message( $s, $y, $z);
}
sub message
{
my ( $s, $y, $z)= @_;
print( " $s,($y),($z)\n ");
}
sub error
{
my $e = shift || ' unkown error ';
print( " $0: $e\n ");
exit 0;
}
#
use strict;
use warnings;
main( @ARGV);
sub main
{
my $s = " This is the template.pl exercise file from Perl 5 Essential Training. ";
my $y = " 42 ";
my $z = " zee ";
message( $s, $y, $z);
}
sub message
{
my ( $s, $y, $z)= @_;
print( " $s,($y),($z)\n ");
}
sub error
{
my $e = shift || ' unkown error ';
print( " $0: $e\n ");
exit 0;
}
注意以上的message子函数的参数传递:先传递给一个默认数组,然后传递给标量。
scope.pl
#
!/usr/bin/perl
#
use strict;
use warnings;
my $g= " yet other string ";
main( @ARGV);
sub main
{
my $m = " some other string ";
message( " This is the template.pl exercise file from Perl 5 Essential Training. ");
message( $m);
}
sub message
{
my $m = shift or return;
print( " $g:$m\n ");
}
sub error
{
my $e = shift || ' unkown error ';
print( " $0: $e\n ");
exit 0;
}
#
use strict;
use warnings;
my $g= " yet other string ";
main( @ARGV);
sub main
{
my $m = " some other string ";
message( " This is the template.pl exercise file from Perl 5 Essential Training. ");
message( $m);
}
sub message
{
my $m = shift or return;
print( " $g:$m\n ");
}
sub error
{
my $e = shift || ' unkown error ';
print( " $0: $e\n ");
exit 0;
}
上面的例子描述了标量的作用域。
return.pl
#
!/usr/bin/perl
#
use strict;
use warnings;
main( @ARGV);
sub main
{
my $num = addnum( 2, 4);
message( $num);
message(addnum( 5, 8));
}
sub addnum{
my ( $v1, $v2)= @_;
return $v1+ $v2;
}
sub message
{
my $m = shift or return;
print( " $m\n ");
}
sub error
{
my $e = shift || ' unkown error ';
print( " $0: $e\n ");
exit 0;
}
#
use strict;
use warnings;
main( @ARGV);
sub main
{
my $num = addnum( 2, 4);
message( $num);
message(addnum( 5, 8));
}
sub addnum{
my ( $v1, $v2)= @_;
return $v1+ $v2;
}
sub message
{
my $m = shift or return;
print( " $m\n ");
}
sub error
{
my $e = shift || ' unkown error ';
print( " $0: $e\n ");
exit 0;
}
上面的例子描述了子函数的返回值,和C一样