Perl教程
宗旨
- 一件事情可以使用许多方法来完成
- 胶水语言
基础信息
perldoc 命令,用于获取perl文档,perldoc perl
perldoc -tf print
#!/usr/bin/perl
use strick;
安装模块
yum install perl-JSON
编译
编程
注释
# 这是注释
变量
变量,无明确类型,可变类型
$ 变量前缀
@ 数组前缀
% 哈希前缀
特殊变量
$_ 默认值变量
$”
$/
$2
$$
数组
字符串
列表 和 数组
qw
(5, 'apple', $x. 3.14159)
( 1 .. 1 0 )
@boy=qw(Greg Peter Bobby);
$boy[0];
@bogy[1,2]; 数组分组
($a, $b, $c)=qw (apples oranges bananas);
$# 返回数组中最后一个有效的索引值
pop 弹出最后面的一个元素
push(@a,$e) 压入一个元素到最后面
push(@a,@e) 压入一个元素组的全部元素到最后面
shift 弹出最前面的一个元素
unshift
foreach $film (@movies) {
}
%hash = ('abc'=>'val',"def"=>"ccd")
foreach $film (keys %movies) {
}
流程控制
- 数字0为假。
- 空的字符串(“”)和字符串“0”为假。
- 未定义值u n d e f为假。
- 其他东西均为真。
if () {} elsif {} else {}
xxxx if ()
while () {}
for () {} 类似Ck
foreach $var (@array) {}
last #break
next #continue
redo
exit
语句块,标签 MYBLOCK: { }
文件
open
close
<=> 飞船运算符 > = <的结合体
<> 菱形运算符,用于读取文件
函数
sub fun {
@_
return
}
fun();
&fun();
srand()
rand(200)
srand; #要先宣告srand函数,才能产生随机数的效果
$int=rand(10); #$int的值会大于0而且小于10如果希望产生的乱数是整数的话,就要再加上int #这个函数
$int=int(rand(10)); #$int的值是一个整数,且值在0和9之间
示例
#print "start"."\n";
open(IN,"tmp.f2.nbr.2.txt");
chomp(@array = <IN>);
close(IN);
#print $array[2]."\n";
#exit(0);
open(IN,"tmp.f1.nbr.2.txt");
$n=0;
while($line=<IN>){
chomp($line);
#$line='000913-8048652';
#print $line."\n";
$r = &binarySearch(\@array,$line);
#print $r."\n";
if( $r < 0) {
print $line."\n";
#print "not find:".$line."\n";
}
$n=$n+1;
if($n % 1000 == 0) {
#last;
#print $n."\n";
}
}
close(IN);
#二分查找算法
sub binarySearch {
($list, $query) = @_;
($low, $high) = ($[, $#$list);
#print "[$low,$high] $query\n";
#return -1;
while ( $low <= $high ) {
my $try = int( ($low + $high) / 2 );
#print "[$low,$high][$try] $list->[$try] vs $query\n";
# note: "lt" and "gt" because we are comparing strings
# if we were comparing numbers then we would use "<" and ">"
$list->[$try] lt $query and do { $low = $try + 1; next };
$list->[$try] gt $query and do { $high = $try - 1; next };
return $try; # By this point it must be equal!
}
return -$try-1; # not find
}
调试
perl -d 程序文件 可以进入调试方式运行,每次运行一条语句
h [cmd] 显示帮助
n 运行下一条语句
print 变量 打印变量
l 列出程序下面10行
b 行号 设置断点
d 行号 取消断点
c 运行直到断点或程序末尾
q 退出
正则
m/ /mgs
s///mgs
tr///
=~
!~
集合
数组遍历
for $one (@array) {
something
}
@words = split(/ /, "all of ars")
% hash
keys
value
exists
调用子进程
system
``
exec
函数
int int(5.6) =5
length length("nose") =4
lc lc("ME TOO") ="me too"
uc uc("hal 9000") ="HAL 9000"
cos cos(50) =.964966
rand rand(5) 返回从0到小于该参数值之间的一个随机数字。如果该参数被省略,则返回0至1之间的一个数字
print
split
join
scalar
sort 可用语句块
open
close
chomp
index
rindex
substr
push
pop
shift
unshift
opendir
closedir
chdir
mkdir
unlink
rename
chmod
stat
system
`ls`
qx{}
引用
&ab=\$ab
$$ab
常用
参数解析
http://www.php-oa.com/2009/04/04/perl_getopt-long.html
use strict;
use Getopt::Long;
my $STATE_OK=0;
my $STATE_WARNING=1;
my $STATE_CRITICAL=2;
my $STATE_UNKNOWN=3;
my $input = @ARGV;
my $host = "127.0.0.1";
my $port = 9425;
my $warning = 0;
my $critical = 0;
my $help = undef;
my $debug = undef;
my $remaining = undef;
my $deadnodes = undef;
my $failedvolumes = undef;
my $filetotal = undef;
my $unreplicated = undef;
my $blacknodes = undef;
my $jobsinfo = undef;
my $cptime = undef;
my $editlog = undef;
GetOptions (
'h' => \$help, 'help' => \$help,
'd' => \$debug, 'debug' => \$debug,
'ht=s' => \$host, 'host=s' => \$host,
'pt=i' => \$port, 'port=i' => \$port,
'rema' => \$remaining, 'remaining' => \$remaining,
'dead' => \$deadnodes, 'deadnodes' => \$deadnodes,
'volu' => \$failedvolumes, 'failedvolumes' => \$failedvolumes,
'file' => \$filetotal, 'filetotal' => \$filetotal,
'unre' => \$unreplicated, 'unreplicated' => \$unreplicated,
'blck' => \$blacknodes, 'blacknodes' => \$blacknodes,
'jobs' => \$jobsinfo, 'jobsinfo' => \$jobsinfo,
'cpti' => \$cptime, 'cptime' => \$cptime,
'elog' => \$editlog, 'editlog' => \$editlog,
'w=s' => \$warning, 'warning=s' => \$warning,
'c=s' => \$critical, 'critical=s' => \$critical,
);
sub process_args {
while (@ARGV) {
$a = shift @ARGV;
if ($a eq "-h") {
Usage();
} elsif ($a eq "-f") {
$file= shift @ARGV;
} elsif ($a eq "-s") {
$string= shift @ARGV ;
} else {
Usage();
}
}
}
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 jaytp@qq.com