<1> perl概述

[root@wx03 1]# cat a1.pl 
$arr=[1,2,3,4,5,6];
print $arr->[4]."\n";
$hash={a=>1,b=>2,c=>3};
print $hash->{b}."\n";
$sub = sub {return "aaaaaaaaa\n"};
print $sub;
print "\n";
print &$sub;
print "\n";

[root@wx03 1]# perl a1.pl 
5
2
CODE(0x251fd20)
aaaaaaaaa


复杂数据结构:

[root@wx03 1]# cat a2.pl 
$wife{"Jacob"}=["Leah","Rachel","Bilhah","Zilpah"];
print $wife{"Jacob"}->[2]."\n";
[root@wx03 1]# perl a2.pl 
Bilhah

[root@wx03 1]# cat a3.pl 
$kids_of_wife{"Jacob"} = {  
"Leah" => ["Reuben", "Simeon", "Levi", "Judah", "Issachar", "Zebulun"],  
"Rachel" => ["Joseph", "Benjamin"],  
"Bilhah" => ["Dan", "Naphtali"],  
"Zilpah" => ["Gad", "Asher"],};
print $kids_of_wife{"Jacob"}->{Rachel}->[1]."\n";  
[root@wx03 1]# perl a3.pl 
Benjamin


[root@wx03 1]# cat t1.pl 
$var=5;
$var .=10;
print $var."\n";

[root@wx03 1]# perl t1.pl 
510




///
[root@wx03 1]# cat a5.pl 
%grades;
open (GRADES ,test) || die;
while (my $line=<GRADES>){
my ($student,$grade) = split (/\s+/,$line);
$grades{$student} .=$grade ." ";
print $grades{$student}."\n";
};

use Data::Dumper;
$str=Dumper(%grades);
print "\$str is $str\n";
[root@wx03 1]# perl a5.pl 
123 
123 456 
$str is $VAR1 = 'abc';
$VAR2 = '123 456 ';

[root@wx03 1]# cat test 
abc 123
abc 456

perl 中的真值:

1.除了""和"0"外,所有字符串都为true;

2.除0以外,所有数字都为true;

3.所有引用都为true;

4. 所有未定义值都为false


正则表达式:

[root@wx03 1]# cat a7.pl 
$var1="a:";
$var2="aa:";
$var3=":";
if ($var1 =~/[a-zA-Z]+:/){print "ok1\n"};
if ($var2 =~/[a-zA-Z]+:/){print "ok2\n"};
if ($var3 =~/[a-zA-Z]+:/){print "ok3\n"};
[root@wx03 1]# perl a7.pl 
ok1
ok2
[root@wx03 1]# 

+ 是一个特殊字符,表示"我们前面的内容出现一次或多次"




[root@wx03 1]# cat a8.pl 
$var1="a";
$var2="a1";
if ($var1 =~/a./){print "ok1\n"};
if ($var2 =~/a./){print "ok2\n"};
[root@wx03 1]# perl a8.pl 
ok2

.号表示任意单个字符

[root@wx03 1]# cat a9.pl 
$var1="a1";
$var2="1";
$var3="aaa1";
if ( $var1 =~ /a*1/ ){print "ok1\n";};
if ( $var2 =~ /a*1/ ){print "ok2\n";};
if ( $var3 =~ /a*1/ ){print "ok3\n";};
[root@wx03 1]# perl a9.pl 
ok1
ok2
ok3

* 表示前面的项出现0次或多次



perl 贪婪匹配:


[root@wx03 1]# cat a10.pl 
$var="larry:abcdffwrwr:ddad:fsffsf34";
if ( $var =~/(.+:)/){print "\$1 is $1\n";};
if ( $var =~/(.*?:)/){print "\$1 is $1\n";};
[root@wx03 1]# perl a10.pl 
$1 is larry:abcdffwrwr:ddad:
$1 is larry:


.*? 取消贪婪匹配







posted @ 2016-05-23 22:20  czcb  阅读(143)  评论(0编辑  收藏  举报