在perl脚本里,每行最后必须以分号结束

很多时候都会去print,print自身没有换行符

perl 定义变量和引用变量都需要加$

[oracle@jhoa perl2]$ perl -e '$aa=xx;print "$aa" x 4 ."\n"'
xxxxxxxx

x 4 连续4次

shell 返回值为0 表示真 非0为假

Perl 返回0为假

Perl 字符窜为空 为假


数值比较 字符比较

> gt
>= ge
< lt

== eq
!= ne


完全和Shell相反


[oracle@jhoa big]$ cat 4.pl
%longday = (
"Sun" => "Sunday",
"Mon" => "Monday",
"Tue" => "Tuesday",
"Wed" => "Wednesday",
"Thu" => "Thursday",
"Fri" => "Friday",
"Sat" => "Saturday",
);
print "$longday{Wed}\n";
[oracle@jhoa big]$ perl 4.pl
Wednesday




[oracle@jhoa big]$ cat 5.pl
$wife{"Jacob"} = ["Leah", "Rachel", "Bilhah", "Zilpah"];
print "$wife{Jacob}[0]\n";
[oracle@jhoa big]$ perl 5.pl
Leah

[oracle@jhoa big]$ cat 6.pl
$var="my name is foo";
if ($var =~ /foo/) { print "right\n" }
[oracle@jhoa big]$ perl 6.pl
right

[oracle@jhoa big]$ cat 6.pl
$var="my name is foo";
{$var =~ s/foo/too/} { print "$var\n" }
[oracle@jhoa big]$ perl 6.pl
my name is too

[oracle@jhoa big]$ cat 7.pl
open (FILE, "<", "aa") ;
while (<FILE>){
if ($_ =~ /http:/) {
print $_;
}
}
[oracle@jhoa big]$ cat aa
http://123.com
aaaaaaaaa
http://123.com
aaaaahttp://123.com
[oracle@jhoa big]$ perl 7.pl
http://123.com
http://123.com
aaaaahttp://123.com


Perl代码简写:

名字ASCII 定义代码

空白[\t\n\r\f] \s

单词[a-zA-Z_0-9] \w 包含字母和数字

数字[0-9] \d

\D 任何非数字

\W匹配非词


[oracle@jhoa big]$ cat 13.pl
$var='aaaaaa';
if ( $var =~ /\w/){print "ok\n"};
[oracle@jhoa big]$ perl 13.pl
ok

\w匹配词
-------------------------------------------

[oracle@jhoa big]$ cat 13.pl
$var='11111';
if ( $var =~ /\W/){print "ok\n"};
[oracle@jhoa big]$ perl 13.pl
[oracle@jhoa big]$

[oracle@jhoa big]$ cat 13.pl
$var='11111';
if ( $var =~ /\w/){print "ok\n"};
[oracle@jhoa big]$ perl 13.pl
ok
-------------------------------------------

\W匹配非词

[oracle@jhoa big]$ cat 13.pl
$var='!';
if ( $var =~ /\w/){print "ok\n"};
[oracle@jhoa big]$ perl 13.pl


[oracle@jhoa big]$ cat 13.pl
$var='!';
if ( $var =~ /\W/){print "ok\n"};
[oracle@jhoa big]$ perl 13.pl
ok


使用 \d{7,11} 将匹配最少 7 位数字, 但不会多于 11 位数字。

[oracle@jhoa big]$ cat 8.pl
open (FILE, "<", "bb") ;
while (<FILE>){
if ($_ =~ /\d{7,11}/) {
print $_;
}
}
[oracle@jhoa big]$ perl 8.pl
1234567
12345678
123456789
12345678910
1234567891011

? 表示前面的子表达式零次或1次

+ 表示前面的表达式一次或多次

* 匹配前面的表达式零次或多次

. 匹配\n之外的任何字符

perl 正则替换

[oracle@jhoa big]$ cat 10.pl
$str = "I love Perl";
print "1.\$str is $str\n";
$str =~ s/Perl/Bash/;
print "2.\$str is $str\n";

[oracle@jhoa big]$ perl 10.pl
1.$str is I love Perl
2.$str is I love Bash
-----------------------------------------------------
[oracle@jhoa big]$ cat 9.pl
$var = "fred xxxxxxx barney";
$var =~ s/x*//;
print "\$var is $var\n";

[oracle@jhoa big]$ perl 9.pl
$var is fred xxxxxxx barney

x*(表示零次或多次“x”) 将0个或者多个x替换为空,相当于啥也没干

[oracle@jhoa big]$ cat 9.pl
$var = " fred xxxxxxx barney";
print "\$var is $var\n";
$var =~ s/x+//;
print "\$var is $var\n";
[oracle@jhoa big]$ perl 9.pl
$var is fred xxxxxxx barney
$var is fred barney


单词边界:

\b匹配单词边界,就是位于单词(\w)和非单词字符(\W)之间的零宽度的地方。

[oracle@jhoa big]$ cat 11.pl
#$var = "The Great Fred";
$var = "Frederick the Great";
if ($var =~/\bFred\b/){print $var};

[oracle@jhoa big]$ perl 11.pl

不能匹配Frederick the Great 因为在“Frederick”中的“d”后面没有跟着非单词字符。

Fred erick the Great[oracle@jhoa big]$ cat 11.pl
#$var = "The Great Fred";
$var = "Fred erick the Great";
if ($var =~/\bFred\b/){print $var};

[oracle@jhoa big]$ perl 11.pl
Fred erick the Great[oracle@jhoa big]$

1.7.4 反引用
[oracle@jhoa big]$ cat 12.pl
$var = "53:02:03";
if ($var =~ /\b\d{1,2}\b:\b\d{1,2}\b:\b\d{1,2}\b/){print $var};
[oracle@jhoa big]$ perl 12.pl
53:02:03[oracle@jhoa big]$