一、运行perl的三种方式
1.$ perl -e comand,运行简单命令
2.$ perl scrit.pl,scrip.pl是perl脚本
3.$ /path/to/script,script第一行是#!/path/to/perl且具有可执行权限
习惯用法是第三种,而且在第一行还有两行如下,其中use strict指示以严格模式执行,遇到问题时退出,而use warnings指示遇到问题时发出警告信息。
#!/path/to/perl
use strict;
use warnings;
二、perl的三种基本数据类型
1.标量scaler
包括字符串、整数和浮点数三种,定义和使用方式如下:
my $str = 'hello' . " perl\n";
my $num = 128;
my $score = 86.5;
print "$str $num $score";
2.数组array
类似于C的数组,但元素类型可以不一样,定义和使用方式如下:
my @animals = ("camel", "llama", "owl");
my @numbers = (23, 42, 69);
my @mixed = ("camel", 42, 1.23);
print $animals[0]; # "camel"
print $mixed[2]; # 1.23
print $#numbers; # numbers元素个数
print @numbers; # numbers元素个数
@animals[0,1]; # gives ("camel", "llama");
@animals[1..$#animals]; # gives all except the first element
my @sorted = sort @animals; # sort返回排序好的数组
my @backwards = reverse @numbers; # reverse返回倒置的数组
3.哈希hash
字符串到任意类型的K/V对,定义和使用方式如下:
my %fruit_color = ("apple", "red", "banana", "yellow");
my %fruit_color = (
apple => "red",
banana => "yellow",
); # 和上面的等价,但是更清楚
print $fruit_color{"apple"}; # 打印"red"
my @fruits = keys %fruit_colors; # keys得到哈希表的键数组
my @colors = values %fruit_colors; # values得到哈希表的值数组
my $variables = { # 定义嵌套哈希表,因为哈希表的值可以是任意类型
scalar => {
description => "single item",
sigil => '$',
},
array => {
description => "ordered list of items",
sigil => '@',
},
hash => {
description => "key/value pairs",
sigil => '%',
},
};
print "Scalars begin with a $variables->{'scalar'}->{'sigil'}\n";
三、基本语法
1.程序构成和语句格式
perl程序没有风格上类似C,但是没有main函数,程序从第一行开始执行,"#"之后到行尾的内容是注释,每条语句以分号";"结束,引号以外的空白符被忽略。
2.条件语句
if ( condition ) {
...
} elsif ( other condition ) {
...
} else {
...
}
unless ( condition ) {
...
}
perl对条件的判断归结为:字符串空为false,整数和浮点数为0false,其他为真。if语句的执行部分一定要用花括号包围起来,哪怕只有一条语句,或者将if写在后面,如下:
# the traditional way
if ($zippy) {
print "Yow!";
}
# the Perlish post-condition way
print "Yow!" if $zippy;
print "We have no bananas" unless $bananas;
3.循环语句
while ( condition ) {
...
}
until ( condition ) {
...
}
print "LA LA LA\n" while 1; # loops forever
for ($i = 0; $i <= $max; $i++) {
...
}
print $list[$_] foreach 0 .. $max;
foreach my $key (keys %hash) {
print "The value of $key is $hash{$key}\n";
}
4.操作符
# Arithmetic
+ addition
- subtraction
* multiplication
/ division
# Numeric comparison
== equality
!= inequality
< less than
> greater than
<= less than or equal
>= greater than or equal
# String comparison
eq equality
ne inequality
lt less than
gt greater than
le less than or equal
ge greater than or equal
# Boolean logic
&& and
|| or
! not
四、文件和IO
1.打开和关闭文件
对文件操作前要打开文件,操作完后关闭文件
open(my $in, "<", "input.txt") or die "Can't open input.txt: $!";
open(my $out, ">", "output.txt") or die "Can't open output.txt: $!";
open(my $log, ">>", "my.log") or die "Can't open my.log: $!";
close $in or die "$in: $!";
2.读文件
<>操作符用来读取文件,当左边是标量时读取一行,当左边是数组时读取全部所有行
my $line = <$in>; # 读一行
my @lines = <$in>; # 读所有行
<>常用在while循环中对每行进行处理
while (<$in>) { # assigns each line in turn to $_
print "Just read in this line: $_";
}
3.写文件
print是写文件最常用的方式,它有两个参数,第一个是文件句柄,第二个是要写的内容,第一个可以忽略,默认是标准输出STDOUT
print STDERR "This is your final warning.\n";
print $out $record;
print $log $logmessage;
五、正则表达式
1.匹配
if (/foo/) { ... } # true if $_ contains "foo"
if ($a =~ /foo/) { ... } # true if $a contains "foo"
2.替换
s/foo/bar/; # replaces foo with bar in $_
$a =~ s/foo/bar/; # replaces foo with bar in $a
$a =~ s/foo/bar/g; # replaces ALL INSTANCES of foo with bar in $a
3.括号和引用
if ($email =~ /([^@]+)@(.+)/) {
print "Username is $1\n";
print "Hostname is $2\n";
}
六、子程序
子程序定义以sub关键字开始,随后是子程序名称和程序块
sub logger {
my $logmessage = shift;
open my $logfile, ">>", "my.log" or die "Could not open my.log: $!";
print $logfile $logmessage;
}
子程序的使用与内置函数一样,名称加括号(括号可选)内参数
logger("We have a logger subroutine!");
或者logger "We have a logger subroutine!";
阅读(865) | 评论(0) | 转发(0) |