<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:


.*? 取消贪婪匹配







转载于:https://www.cnblogs.com/zhaoyangjian724/p/6199992.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值