我也算学了基本脚本语言的人,Au3、Pyhon、PHP、JavaScript、Shell 都用过,但这个家伙是有点让我膛目,语法简直简练到不能再简练了。一下午的时间看了看他的基础语法和示例,做一下记录。
#!perl
# 这是一行 Perl 的注释
print "Hello Perl";
=pod
这是 perl 的多行注释
这是 perl 的多行注释
这是 perl 的多行注释
=cut
# 基础变量
$var1 = 123456; # 十进制
$var2 = 047; # 八进制
$var3 = 0x1f; # 十六进制
# 浮点型,不是非常精准
$var4 = 9.01e+21 + 0.01 - 9.01e+21;
print ("first value is ", $var4, "\n");
# 字符串中变量替换,仅支持在双引号中使用
$number = 11;
$text = "This text contains the number $number\n";
print $text;
# 单引号可以跨多行,但不能使用转移和变量替换
$text1 = "This is two
lines of text
";
print $text1;
# 字符串和数值互相转换
$string = "43";
$number = 100;
$result = $string + $number; # $result = 71
print $result;
$result = "Hello" * 5; # $result = 0
print ("Hello * 5 = ", $result, "\n");
$result = "12a34" + 1; # $result = 13
print ("12a34 + 1 = ", $result, "\n");
# 字符串比较和逻辑操作符
if ($text eq $text1) {
print "text = text1!\n";
} elsif ($var1 > $var2) {
print "$var1 > $var2! \n";
} else {
print "error!";
}
# 字符串自增
$stringvar = "abc";
$stringvar++;
print $stringvar; # print abd
$stringvar = "abcZZZ";
$stringvar++;
print $stringvar; # print abdAAA
print "\n";
# 字符串拼接
$newstring = "potato" . "head";
print $newstring, "\n"; # potatohead
$newstring = "t" x 5;
print $newstring, "\n"; # ttttt
# 逗号运算符
$val = 26;
$result = (++$val, $val + 5); # $result = 32
print $result, "\n";
# 数组变量
@array = (1, 2, 3);
$scalar = $array[0];
$array[3] = 5; # now @array is (1,2,3,5)
$scalar = $array[4]; # now $scalar = null
$array[6] = 17; # now @array is (1,2,3,5,"","",17)
# 用数组给列表赋值
@list1 = (2, 3, 4);
@list2 = (1, @list1, 5);# @list = (1,2,3,4,5)
# 用数组给普通多个变量赋值
@array = (5, 7, 11);
($var1, $var2) = @array; # $var1 = 5, $var2 = 7, 11 被忽略
print ("var1 = $var1, var2 = $var2\n");
@array = (5, 7);
($var1, $var2, $var3) = @array; # $var1 = 5, $var2 = 7, var3 = ""
print ("var1 = $var1, var2 = $var2, var3 = $var3\n");
# 字符串中的方括号和变量替换
print ("\$array[0] = $array[0]\n");
# 列表范围
@number = (1..10);
print "@number\n";
@day_of_month = ("01".."31");
print "@day_of_month\n";
$fred = "Fred";
print (("Hello, " . $fred . "!\n") x 2);
# 子数组
@array = (1, 2, 3, 4, 5);
@subarray = @array[0, 1]; # @subarray = (1, 2)
@subarray2 = @array[1..3]; # @subarray2 = (2, 3, 4)
@array[0, 1] = ("string", 46); # @array = ("string", 46, 3, 4, 5)
@array[0..3] = (11, 22, 33, 44); # @array = (11, 22, 33, 44, 5)
@array[0..2] = @array[3, 4]; # @array = (44, 5, "", 44, 5)
print "@array\n";
@array[1, 2] = @array[2, 1]; # 可用于交换元素
print "@array\n";
# 数组的库函数
@array = ("this", "is", "a", "test");
@array2 = sort(@array);
print "@array2\n";
@array2 = reverse(@array);
print "@array2\n";
@array2 = reverse sort (@array);
print "@array2\n";
@list = ("rabbit", "12345", "quertz"); # rabbi 1234 quert
chop (@list);
print "@list\n";
$string = join("", "My", "name", "is", "Jia Deng");
print "$string\n"; # MynameisJia Deng
@list = ("words", "and");
$string = join("::", @list, "colons");
print "$string\n"; # words::and::colons
@spt = split("::", $string);
print "@spt\n"; # words and colons
# 条件判断
if ( "aa" eq "aa" ) {
print "==\n";
} else {
print "!=\n";
}
# 循环
$idx = 1;
while ($idx <= 100) {
print "$idx\t";
$idx++;
}
print "\n";
until ($idx <= 1) {
print "$idx\t";
$idx--;
}
print "\n";
# 类 C 语言 break 和 continue
$i = 0;
while ($i < 100) {
if ($i == 20) {
# 到 20 停止循环
last;
}
print "$i\t";
$i++;
}
print "\n";
$i = 0;
while ($i < 30) {
if ($i == 20) {
# 将跳过 20
$i++;
next;
}
print "$i\t";
$i++;
}
print "\n\n";
# for
@list = (1, 2, 3, 4, 5);
foreach $temp (@list) {
if ($temp == 2) {
$temp = 20;
}
print "$temp\t";
}
print "\n";
# 单行条件
print ("\@list length = " . @list . "\n") if (@list < 10);
# 关联数组
%fruit = ("apples" => 17, "bananas" => 9, "oranges" => 100);
foreach (keys(%fruit)) {
print ("$_ = $fruit{$_}\t");
}
print "\n";
# 删除一个关联数组的元素
delete $fruit{apples};
foreach (keys(%fruit)) {
print ("$_ = $fruit{$_}\t");
}
print "\n";
# each() 遍历关联数组
%records = ("Maris", 61, "Aaron", 755, "Young", 511);
while (($key, $value) = each(%records)) {
print ("$key => $value\t");
}