$camels = '123';
print $camels + 1 ,"\n";
[oracle@dwh1 big]$ perl 1.pl
124
同样的,一个引用在"解引用"环境表现为一个引用,否则就像一个普通标量一样工作,比如,我们可以说:
$fido = new Camel "Amelia";
if (not $fino) {die "dead came;";}
$fido -> saddle();
在这里,我们首先创建了一个指向Camel对象的引用,并将它赋给变量$fido,在第二行中,我们将$fido当前
一个布尔值来判断它是否为真,如果它不为真,程序将抛出一个例外。在这个例子中,这将意味着new Camel构造
函数创建Camel对象失败。
最后一行,我们将$fido作为一个引用,并调用Camel对象的saddle()方法。
1.2.3 复数变量:
和C里一样,数组是以0为基的,你可以用下标0和3来表示数组的第一道第四个元素
$home[n] , 这里n是下标(元素编码减一)
[oracle@dwh1 big]$ cat 2.pl
$home[0] = "couch";
$home[1] = "chair";
$home[2] = "table";
$home[3] = "stove";
print "\@home is @home\n";
[oracle@dwh1 big]$ perl 2.pl
@home is couch chair table stove
散列:
[oracle@dwh1 big]$ cat 3.pl
%longday = (
"SUN" => "Sunday",
"Mon" => "Monday",
"Tue" => "Tuesday"
);
print "SUN=>$longday{SUN}\n";
[oracle@dwh1 big]$ perl 3.pl
SUN=>Sunday
1.4 文件句柄:
[oracle@jhoa 20150316]$ cat 1.pl
print STDOUT "Enter a number :"; ###请求一个数字
$number = <>; ##输入数字
print STDOUT "The number is $number.\n";
1.5.4 单目算术操作符:
##先将$a的值赋值给$b ,$a在自加变为6
[oracle@jhoa 20150316]$ cat 2.pl
$a = 5;
$b = $a ++;
print "\$b is $b\n";
print "\$a is $a\n";
[oracle@jhoa 20150316]$ perl 2.pl
$b is 5
$a is 6
-----------------------------------------
[oracle@jhoa 20150316]$ cat 2.pl
$a = 5;
$b = ++$a;
print "\$b is $b\n";
print "\$a is $a\n";
[oracle@jhoa 20150316]$ perl 2.pl
$b is 6
$a is 6
$b被赋值为$a自增后的值 6
[oracle@jhoa 20150316]$ cat 3.pl
@users = qw/ aa bb cc dd ee root ff gg hh lp ii jj/;
foreach $user (@users){
if ($user eq "root" or $user eq "lp"){
next;
}
if ($user eq "special") {
print "Found the special account:\n";
last;
}
else {print "\$user is $user\n";
}
}
[oracle@jhoa 20150316]$ perl 3.pl
$user is aa
$user is bb
$user is cc
$user is dd
$user is ee
$user is ff
$user is gg
$user is hh
$user is ii
$user is jj
1.7 正则表达式:
split:
[oracle@jhoa 20150316]$ cat 4.pl
($good,$bad,$ugly) = split(/,/, "vi,emacs,teco");
print "$good---$bad---$ugly\n";
[oracle@jhoa 20150316]$ perl 4.pl
vi---emacs---teco
+ 是一个特殊字符,表示匹配"+前边内容一次或多次。
\s 空白 \t \n \r \f
\w [a-zA-Z_0-9] 匹配任何单个字符
\d 数字
. 点号 匹配所有的字符
\d{7,11} 将匹配最少7位数字,但不会多于11位数字。
+ 代表最少一次
* 表示0次或多次
? 表示零或一次
替换xxxx字符:
[oracle@jhoa 20150316]$ cat 5.pl
$_ = "fred xxxxxxx barney";
s/x+//;
print "\$_ is $_\n";
[oracle@jhoa 20150316]$ perl 5.pl
$_ is fred barney
1.7.2 取消贪婪匹配:
在perl里你可以强制进行非贪婪匹配,在量词后面加上一个问号来表示最小匹配。
我们同样的用户名匹配就可以写成/.*?:/。 这里.*? 现在尽可能少地匹配字符,而不是尽可能多的匹配字符。