更新记录
2015/08/17 拆开来记,start with foreach
2015/09/01 更新到子程序,最近有点懒惰了
##结构
###foreach控制结构
1
2
3
4
5
|
#!/usr/bin/perl
use 5.010;
foreach $rock (qw/ my girlfriend is Mo /){
print "One word is named $rock\n";
}
|
arvon@Mo:~/arvon_perl> perl foreach_one.pl
One word is named my
One word is named girlfriend
One word is named is
One word is named Mo
1
2
3
4
5
6
7
8
|
#!/usr/bin/perl
use 5.010;
@rocks = qw/ hello world Mo /;
foreach $rock(@rocks){
$rock = "\t$rock";
$rock .= "\n";
print "\$rock now is $rock";
}
|
arvon@Mo:~/arvon_perl> perl foreach_two.pl
$rock now is hello
$rock now is world
$rock now is Mo
###Perl的默认变量$
1
2
3
4
5
|
#!/usr/bin/perl
use 5.010;
foreach (1..10){ #使用了默认变量$_
print "I can count to $_!\n";
}
|
arvon@Mo:~/arvon_perl> perl variable_one.pl
I can count to 1!
I can count to 2!
I can count to 3!
I can count to 4!
I can count to 5!
I can count to 6!
I can count to 7!
I can count to 8!
I can count to 9!
I can count to 10!
###reverse操作符
- reverse操作符会读取列表的值,并按相反的次序返回该列表。
1
2
3
4
5
6
7
|
#!/usr/bin/perl
use 5.010;
@fred = 6..10;
@barney = reverse(@fred);
@wilma = reverse 6..10;
@fred = reverse @fred;
print "@fred, @barney, @wilma, \n";
|
arvon@Mo:~/arvon_perl> perl reverse_array.pl
10 9 8 7 6, 10 9 8 7 6, 10 9 8 7 6,
###sort操作符
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#!/usr/bin/perl
use 5.010;
@rocks = qw/ happy birthday to Mo /;
@sorted = sort(@rocks);
@back = reverse sort @rocks;
@rocks = sort @rocks;
@numbers = sort 97..102;
print "
@rocks \
@sorted\
@back\
@rocks\
@numbers";
|
arvon@Mo:~/arvon_perl> perl sort_string.pl
Mo birthday happy to
Mo birthday happy to
to happy birthday Mo
Mo birthday happy to
100 101 102 97 98 99
###each操作符
- 每次对数组调用each,会返回数组中下一个元素所对应的两个值–该元素的索引以及该元素的值
1
2
3
4
5
6
7
8
9
|
#!/usr/bin/perl
use 5.012;
my @rocks = qw/ bedrock slate rubble granite /;
while( my( $index, $value ) = each @rocks){
say "$index: $value";
}
foreach $index(0 .. $#rocks ){
print "$index: $rocks[$index]\n";
}
|
子程序
- 子程序名称
子程序名称以字母、数字、下划线组成,不能以数字开头,子程序名称属于独立的名字空间
定义子程序
- 定义子程序用sub、子程序名以及花括号封闭起来的代码块,for example:
1
2
3
4
|
sub marine {
$n += 1; #全局变量$n
print "Hello, sailor number $n!\n";
}
|
调用子程序
1
2
3
4
5
6
7
8
9
|
#!/usr/bin/perl
sub marine{
$n += 1;
print "Hello, sailor number $n!\n";
}
&marine; #打印hello,sailor number 1!
&marine; #打印hello,sailor number 2!
&marine; #打印hello,sailor number 3!
&marine; #打印hello,sailor number 4!
|
返回值
- 任何的perl子程序都有返回值,但不是所有的返回值都是有用的,Larry将之简化,在子程序的执行过程中,它会不断进行运算,而最后一次运算的结果(不管是什么)都会被自动当成子程序的返回值。
1
2
3
4
5
6
7
8
9
10
11
12
|
#!/usr/bin/perl
$fred = 2;
$barney = 3;
$wilma = &sum_of_fred_and_barney;
sub sum_of_fred_and_barney{
print "Hey, you called the sum_of_fred_and_barney subroutine!\n";
$fred + $barney;
}
#&sum_of_fred_and_barney;
$betty = 3 * &sum_of_fred_and_barney;
print "\$wilma is $wilma.\n";
print "\$betty is $betty.\n";
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#!/usr/bin/perl
$fred = 2;
$barney = 3;
sub larger_of_fred_or_barney{
if ($fred > $barney){
$fred;
print "\$fred is $fred\n";
}
else{
$barney;
print "\$barney is $barney\n";
}
}
&larger_of_fred_or_barney;
|
参数
- 要传递参数列表到子程序里,只要在子程序调用的后面加上被括号圈引得列表表达式就可以了,for example:
1
2
3
4
5
6
7
8
9
10
11
|
#!/usr/bin/perl
sub max{
if ($_[0] > $_[1]){
$_[0];
print "\$_[0] is $_[0]\n";
}else {
$_[1];
print "\$_[1] is $_[1]\n";
}
}
$n = &max(3, 4);
|
子程序中的私有变量
- 默认情况下,perl里的所有变量都是全局变量,即在程序的任何地方都可以访问他们。随时可以借助my操作符来创建私有变量(lexical variable)
1
2
3
4
5
6
|
#!/usr/bin/perl
sub max {
my($m, $n);
($m, $n_) = @_;
if ($m > $n){ $m } else { $n }
}
|
变长的参数列表
- 打印最大值,“高水线(high-watermark)”算法
1
2
3
4
5
6
7
8
9
10
11
12
|
#!/usr/bin/perl
$maximum = &max(3, 5, 10, 4, 6);
sub max{
my($max_so_far) = shift @_;
foreach (@_){
if ($_ > $max_so_far){
$max_so_far = $_;
}
}
$max_so_far;
print "Max is $max_so_far\n";
}
|
空参数列表
关于词法(my)变量
- 词法变量可以用在任何语句块内,而不仅限于子程序语句块。比如说,它可以在if、while或foreach的语句块里使用
- 求次方
1
2
3
4
5
|
#!/usr/bin/perl
foreach (1..10){
my($square) = $_ * $_;
print "$_ squared is $square.\n";
}
|
1
2
|
my $fred, $barney; #只声明了fred这一个变量
my($fred, $barney); #两个都声明了
|
- 在日常perl编程中,你最好对每个新变量都使用my声明,最好对每个新变量都使用my声明,让它保持在自己所在的词法作用域内。
1
2
3
4
|
#!/usr/bin/perl
foreach my $rock (qw/bedrock slate lava /){
print "One rock is $rock.\n";
}
|
use strict编译指令(pragma)
- 告诉perl我愿意接受更严格的限制使用use strict这个编译指令放在程序开头
- 自perl5.12开始,如果使用编译指令指定最低perl版本号的话,就相当于隐式打开了约束指令
return操作符
- return操作符可以让子程序执行到一半的时候停止执行
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#!/usr/bin/perl
use strict;
my @names = qw/ fred barney betty dino wilma pebbles bamm-bam /;
my $result = &which_element_is("dino", @names);
sub which_element_is{
my($what, @array) = @_;
foreach (0..$#array){
if ($what eq $array[$_]){
return $_;
}
}
-1;
}
|