Perl(Practical Extraction and Report Language 实用摘录与报表语言),具有高级语言强大的能力和灵活性,可以提供脚本语言(如sed和awk)的所有功能,擅长文本的处理。
Linux系统:(一般默认已经安装了,不需要再安装)
Windows系统:使用ActivePerl
在Perl中也有类似单数和复数的区别,标量(Scalar)代表单件事物。标量可以是数字或者字符串。
可以分为以下几种类型:
对于数字常用操作符有:
+、-、*、/、%(取模)、**(乘幂)
字符串由字母、数字及标点符号组成。字符串由两种形式:单引号中的字符串和双引号中的字符串。
define:标量变量以“$”开头,后面接Perl标识符(由字母、数字、下划线组成,必须字母或者下划线开始)
要慎重使用Perl默认的一些变量
$ARGV, $_ ,$1, $2, … $n, $$, $@, $? , $! …
perl的一些特殊内置变量:
变量 | 含义 |
---|---|
$n | 包含上次模式匹配的第n个字符串 |
$_ | 默认输入和模式匹配的内容(如果不指定接收输入的量或执行模式匹配的字符串) |
$$ | 运行当前Perl脚本程序的进程号 |
$@ | 命令eval的错误消息。如果为空则表示eval命令执行成功 |
$? | 存放进程结束状态 |
$! | die命令抛出的信息(系统错误字符串) |
@ARGV | 传给Perl脚本的命令行参数列表 |
@_ | 传给子程序的参数列表 |
@INC | 再导入模块时需要搜索的目录列表 |
#eg:
print “The price is \$5.\n”
print 'The price is $5'
#eg:
$str = “today is ${year}_${mon}_${day}\n”
操作 | 操作符 |
---|---|
逻辑与 | && /and |
逻辑或 | || / or |
逻辑非 | !/not |
逻辑异或 | xor |
位操作符: &、|、~、^、<<、>>
note:不要将&用于负整数,因为perl会把它们转化为无符号数
赋值操作符: =、+=、-=、/=、**=、&=、|=、~=、^=、<<=、>>=、.=
自增自减操作符:++、–
note:++可以用于字符串,但在结尾字符为‘z’、‘Z’、‘9’时进位;不要直接使用–,将字符串转为数字再进行自减;
条件操作符:? :
undef
defined
defined函数用来判断变量的值是否为undef,是返回0,不是返回1.
如果将Perl中的标量看成是单数的概念,那么列表和数组就是复数的概念。
列表是变量的有序集合,而数组是存储列表的变量;列表指的是数据,而数组指的是变量;列表的值不一定要放在数组里,但数组变量都包含一个列表(可能是空列表)
数组或列表中的每个元素都是单独的标量变量,这里变量都有相应的整数作为索引,索引值从0开始,依次加1。
eg:(5,8,9) #包含5,8,9三个数的一个列表;(“my”,“name”,“is”,“xiaoming”)#字符串列表
#eg:
$array[0]= "just";
$array[1]= "for";
$array[2]= "test";
$#: 名字为arrs的 数组的第一个索引值为0,最后一个索引值为 $#arrs
负的数组索引值:数组最后元素的索引值为-1,倒数第二个元素的索引值为-2,…
eg:数组arrs有3个元素,则有效的负数索引值为-1,-2,-3
#eg:
($fred,$bar)=("stone","rubble","bin";)
列表赋值中额外的值会被自动忽略,如果有多余的变量会被赋予undef。
#eg:
(array[0],array[1]) = qw/array list/;
数组的定义由@开始,后面接上数组名
eg:
my @arrays = qw/array list/;
my @arrays = (); #空数组
@arrays = reverse @arrays;
数组列表操作符使用具体例子:
#!/user/bin/perl -w
#----------------------------------------------------------------
#Description:
# the example unsed to descrip the use of operations for
# list and arrays in Perl
#
#----------------------------------------------------------------
#--------------------------------------
# example for pop operation
print "\nExample for pop operator:\n";
print "-"x40 . "\n";
my @arrs_1 = (1,3,4,9,10,4);
print "original arrays = @arrs_1 \n";
my $val = pop(@arrs_1);
print "after one pop operation:arrs_1 = @arrs_1 and obtained value = $val \n";
pop (@arrs_1);
print "after two pop operations and arrs_1 = @arrs_1 \n";
#--------------------------------------
# example for push operation
print "\nExample for push operator:\n";
print "-"x40 . "\n";
push (@arrs_1,100);
print "after one push 100 operation and arrs_1 = @arrs_1 \n";
my @tests = qw(100 104 109);
push (@arrs_1,@tests);
print "after two push operations, tests = @tests and arrs_1 = @arrs_1 \n";
#--------------------------------------
# example for shift operation
my @arrs = (1,2);
print "\nExample for shift and unshift :\n";
print "-"x40 . "\n";
print "original arrays = @arrs \n";
$val = shift(@arrs);
print "after one shift operation, arrs = @arrs and obtained value = $val \n";
$val = shift(@arrs);
print "after two shift operations, arrs = @arrs and obtained value = $val \n";
$val = shift(@arrs);
print "after three shift operations :";
if (defined $arrs[0]){
print"arrs = @arrs";
}
else {
print "arrs = undef";
}
if (defined $val){
print "and obtained value = $val \n";
}
else {
print "and obtained value = undef";
}
unshift (@arrs,7);
print "after one unshift operation and arrs = @arrs \n";
@tests = 10 .. 20;
unshift (@arrs,@tests);
print "after two unshift operation: tests = @tests and arrs = @arrs \n";
#--------------------------------------
# example for foreach operation
print "\nExample for push operator:\n";
print "-"x40 . "\n";
$val = 1000;
print "before foreach : val = $val \n";
print "test foreach operator : arrs = ";
my $num = 0;
foreach my $val (@arrs){
print " "if (num > 0);
print "$val";
$num ++;
}
print "\n";
print "after foreach : val = $val \n";
#--------------------------------------
# example for reverse operation
print "\nExample for push operator:\n";
print "-"x40 . "\n";
@arrs = reverse @arrs;
print "after revers and arrs = @arrs \n";
#--------------------------------------
# example for sort operation
print "\nExample for push operator:\n";
print "-"x40 . "\n";
@arrs = sort @arrs;
print "after the default sort operation and arrs = @arrs \n";
@arrs = sort {$a <=> $b} @arrs;
print "after the default sort operation use \"<=>\" and arrs = @arrs \n";
上下文
上下文是指表达式所处的位置,Perl解析表达式的时候,要么希望得到一个标量,要么希望得到一个列表。表达式所在的位置,Perl期望得郅什么,就是该表达式的上下文。
同一个表达式在不同的上下文会有不同的含义。
scalar函数
可以使用scalar函数强制指定标量上下文。
例子
#!/user/bin/perl -w
use strict;
# ---------------------------------------------------------------------
# Description:
# The example used to show the example Scalar and List Context
# ---------------------------------------------------------------------
my @arrays = qw(example for scalar and list context);
print "\narrays = @arrays \n\n"; #list context
my @sorted = sort @arrays; #list context
print "after sorted and arrays = @sorted\n\n";
print "after sorted and scalar arrays = " . scalar @sorted . "\n\n"; #scalar context
my $num = @arrays; #scalar context
print "num = $num\n\n";
my @reversed = reverse @arrays;
print "reversed arrays = @reversed \n\n";
my $reversed_str = reverse @arrays;
print "reversed string = $reversed_str \n\n"
子程序定义:
。使用sub关键字定义子程序,由子程序名和花括号中的缩进代码组成;
。子程序可以在程序任意位置定义;
。无需对子程序进行事先申明(子程序定义时全局的);
。当出现重名子程序时,后面的定义会覆盖前面一个;
子程序调用:使用与号“&” 加上子程序名来调用已定义的子程序。
&display();
&display;
sub display {
print "This is a example for subroutings \n";
}
子程序参数:
。在子程序调用的后面加上括号圈引得列表表达式可以传递参数列表到子程序中;
。子程序内部通过数组变量@_来接收参数;(@_是子程序的私有变量,在当前的子程序调用中,@_总是包含了它的参数列表)
子程序中的私有变量:
。默认情况下,Perl中的变量都是全局变量;
。可以使用操作符my来申明私有变量;
。my申明的私有变量的作用范围被圈定在语句块中;
sub get_max_val {
my ($val1,$val2) = @_ ;
}
#!/user/bin/perl -w
use strict;
# ----------------------------------------------
# THe example used to show the example of subrooutines
my $val1 = 10;
my $val2 = 19;
my $max = &get_max_val ($val1,$val2);
print "\nval1 = $val1,val2 = $val2 ,and max value = $max \n\n";
$max = &get_max_val (my $m, my $n);
sub get_max_val {
my ($val1,$val2) = @_;
if (!defined $val1 || !defined $val2){
print "the given data is uninitialized.Exiting ...\n";
exit;
}
return $val1 if ($val1 > $val2);
return $val2 if ($val2 > $val1);
}
哈希是一种数据结构,和数组相同,可以容纳很多值(无上限),并能随机存取。哈希和数组的区别在于:数组使用数字来进行索引,而哈希使用名字(保证唯一的字符串)。哈希的索引值称为键值(key),和索引相对应的是值
(value)。因此哈希中存储的是一序列的键/值对。
另一种理解hash的方法是,把它看作一堆数据,每一个数据都有一个相应的标签。可以通过标签访问此标签对应的元素。但其中是没有“第一个”元素的概念的。
为什么使用哈希?
可以把哈希看做一个简单的数据库,可以根据一组数据检索出对应的另一组数据。如果你的任务是关于“查询重复的”,“唯一的”,“交叉引用的”“查询表”,hash很可能在这类应用中能帮上忙。
一些哈希的应用场景.
。根据主机名找对应的IP地址
。根据IP地址找主机名
。按单词统计出现的次数
。按驾照号码找出对应的姓名
哈希的申明和赋值
。使用“%”符号对哈希进行申明 :my %tab
。使用列表对哈希进行赋值,必须有偶数个成员(哈
希必须是键/值对)
。可用箭头符号"=>"将哈希赋值中的键/值区分开来
。哈希松绑操作时,键/值对不一定按照赋值顺序松绑 :my @arrays = %tab #哈希->列表
;
。哈希复制
my %newhash = %hash;
my %inversehash = reverse %hash;
#哈希值唯一的情况下才有效
my $value = $scores{$key};
my $vaIue2 = $scores{"xiaoming"};
print "Xiaoli's score is $scores{"xiaoli"}"\n";
my @names = keys %scores;
my @scores = values %scores;
while (my ($names,$score)= each %scores);
my $person = "xiaoming";
if (exists $scores{$person}) {
print "we have obtained the score for $person \n";
}
delete $scores {$person};
peint Data :: Dunmper -> Dump ([\%scores],['scores']);
#!/user/bin/perl -w
use strict;
use Data::Dumper;
#-------------------------------------------------------
# This is a simple complete example for hash application
my $tab = " ";
my %scores = (
"xiaoming" => 78,
"xiaoli" => 89,
"xiaotong" => 89,
"xiaosong" => 90,
"xiaowang" => 96,
"xiaoshi" => 84,
);
print "\n the original score hash table content :\n";
print "-"x40 . "\n";
print Data::Dumper ->Dump([\%scores],['scores']);
my $summary = "";
$summary .= "\nthe following is the score summary information:\n";
$summary .= "-"x50 . "\n";
my $sum = 0;
while (my ($name,$score) = each %scores) {
$summary .= "${tab}${tab} $name ${tab} -- $score \n";
$sum += $score;
}
$summary .= "\n\n";
my @names = keys %scores;
my @scores = values %scores;
print "\n The obtained name list = @names \n";
print "\n The obtained score list = @scores \n";
my $num = @names;
my $average = $sum / $num;
print "\n The average score is $average \n\n";
print $summary;
my $person = "xiaoming";
if (exists $scores{$person}) {
print "We have obtained the score for $person \n";
}
delete $scores {$person};
print "\n The following is the scores hash context after delete $person information from the hash:\n";
print "-"x50 . "\n";
print Data::Dumper ->Dump([\%scores],['scores']);
print "\n";
$person = "xiaoli";
delete $scores{$person};
print "\n The following is the scores hash context after delete $person information from the hash:\n";
print "-"x50 . "\n";
print Data::Dumper ->Dump([\%scores],['scores']);
print "\n";