# 行末注释,类似于C中的//
" " 双引号double-quoted的内部会对@$等符号展开为变量或数组''单引号的内部不展开,仅仅视作原本的字符
'Hello/n'与"Hello/n"是两个不同的字符串
/转义字符slash'/'//' 是一个字符串,内容为'/
变量不能以数字开头使用变量以$解析
Perl中的数字科学计数法为-1.36E-23 或32.1e23大小写e都是允许的可以使用下划线对一个长数字进行分隔 61_298_040_283_768以0开头的数字是八进制数 如 03770x开头的数字是16进制数 如 0xff0b开头的数字是二进制数 如 0b111111
.操作符single period 简单连接字符串$str = $str ." "
x操作符 复制字符串例如:"strings!"x3 相当于"strings!strings!strings!"5x4 相当于"5555"
chomp操作 去掉行末换行符chomp($text)
对于第一次使用的变量,默认给予值undefundef被当作数字使用相当于0,或者空字符串
通过defined($var)测试var是否未初始化(值为undef)
if语句的真假条件:1.undef 假2.0 假,其余所有数字均为真3.空字符串为假,其它所有字符串为真4.特殊情况,'0',Perl中数字和字符串可以灵活转换使用,字符'0'也为假
数组变量A list is an ordered collection of scalars. An array is a variable that contains a list.数组类型没有限制,可以是任何变量(但不能是另一个数组)通过[]square brackets 访问数组变量中的元素$array[] @array数组index可以是小数,会忽略小数部分,index从0开始负数的index$array[-1] 数组最后一个元素$array[-2] 数组倒数第二个元素…………通常只使用-1
使用print @array 由于@array会被认为是标量上下文,输出是array元素的个数
$#array 数组最大的index0..$#array) 数组序数的列表$array[ $#array] 数组最后一个元素注意数组大小是$#array+1,不过通常不会用到
初始化一个数组 (1,2,3)
可以使用范围操作符..(9..100) 9到100间的整数注意:foreach $item (1.7..5.7){print $item."/t";}句点会被忽略,因此输出为1 2 3 4 5(5..1) 逆序是不允许的,这是一个空表
qw操作符 qw stands for "quoted words" or "quoted by whitespace,"通常使用方法:@rocks = qw/ bedrock slate lava /qw的边界符是可选的qw! fred barney betty wilma dino !qw# fred barney betty wilma dino #qw( fred barney betty wilma dino )qw{ fred barney betty wilma dino }qw[ fred barney betty wilma dino ]qw< fred barney betty wilma dino >连#都可以使用,视乎需要可以灵活使用qw! yahoo/! google excite lycos ! 转义字符/能够处理必须出现的边界符
用=进行赋值, 右侧表达式多余的值会被扔掉,不足的值变成undef($fred, $barney) = qw< flintstone rubble slate granite >;
几个赋值的例子@tiny = ( ); # the empty list 空表@giant = 1..1e5; # 使用范围符.. a list with 100,000 elements@stuff = (@giant, undef, @giant); # 可以包含undef元素 a list with 200,001 elements$dino = "granite";@rocks = qw/ bedrock slate lava /; @quarry = (@rocks, "crushed rock", @tiny, $dino); #在数组中定义的数组会被展开,空数组不会展开为undef元素quarry相当于(bedrock, slate, lava, crushed rock, granite)
通过赋值进行swap 交换是允许的:($betty[0], $betty[1]) = ($betty[1], $betty[0]);
pop 和 push 操作符 数组末端相当于栈顶,数组首元素相当于栈底,返回值为弹出的元素@array = 5..9;$fred = pop(@array); # $fred gets 9, @array now has (5, 6, 7, 8)pop @array; # @array now has (5, 6). (The 7 is discarded.)对空数组pop返回undef
push @array, 1..10; # 可以一次push多个元素@others = qw/ 9 0 2 1 0 /;push @array, @others; # 可以把一张表push进另一张表
shift 和 unshift 与pop push功能类似,不过是在数组首元素进行操作,数组首元素是栈顶,末元素是栈底
array 转换字符串,注意!电子邮件的@符号可能被解析为数组操作符!$email = "fred@bedrock.edu"; # 错误!!WRONG! Tries to interpolate @bedrock$email = "fred/@bedrock.edu"; # 正确!使用/进行转义$email = 'fred@bedrock.edu'; # 使用单引号可以避免解析
有相同的变量名和数组名时,必须使用括号为名称定界@fred = qw(eating rocks is wrong);$fred = "right"; # we are trying to say "this is right[3]"print "this is $fred[3]/n"; # prints "wrong" using $fred[3]print "this is ${fred}[3]/n"; 输出this is wrongthis is right[3]
foreach 循环foreach $eachitem (@list){#do some thing}
默认变量$_foreach (1..10) { # Uses $_ by default print "I can count to $_!/n";}
$_ = "Yabba dabba doo/n";print; # prints $_ by default当无须使用一个变量名时使用$_
reverse操作符 逆序一个数组,注意reverse返回逆序后的数组,但源数组不会被改变。如果需要逆序一个数组,可以:@fred = reverse @fred
sort操作符 排序一个数组,排序按照ASCII码表方式,从数组第一个字符位置比较起。类似字典法。在ASCII表中,数字排在字母前,大写字母排在小写字母前
Scalar and list ContextScalar 和 list 上下文同一个语句在Perl中可能有不同的含义,取决于上下文如:@list = @people; # 复制数组people$n = @people; # 右边是个标量,因此$n等于3, people表的元素个数
当list上下文中提交的是一个标量时,转化为有一个标量元素的list强制把标量上下文 使用scalar@rocks = qw( talc quartz jade obsidian );print "How many rocks do you have?/n";print "I have ", @rocks, " rocks!/n"; # WRONG, prints names of rocksprint "I have ", scalar @rocks, " rocks!/n";
<STDIN>标准输入@lines = <STDIN>; # read standard input in list context在UNIX 和Windows环境下,文件结束符不同UNIX Ctrl+DWindows Ctrl+Z常用chomp(@lines = <STDIN>); # 读取标准输入,并去掉行尾的换行符lines字符串数组保留每一行输入这种读取方式不能再次读取已经读取过的行,当文件输入非常大的时候,最好使用其他方法。