常量
在程序运行中,其值不能改变且为一定值的量称为常量。PERL的常量有两种:数字常量和字符串常量。、
标量:单件事物。数字,字符串
可以用操作符对标量进行操作,结果通常也是一个标量。
常量用途:
1. 给变量赋值。
2. 作参数传递给函数。
数字常量
数据标量:
PERL里,总是按“双精度浮点数”来保存和运算。不存在整数。所谓整数,其实也是双精度浮点数。
Perl允许在整数直接量里插入下划线,将若干位分开:
21_321_567_654
非十进制的整数直接量:
0:八进制 0123 # 83
0x:十六进制 0xff
0b:二进制 0b101110
数字操作符:+ - * / %
%:取模操作符,先取整后再求余。
**:乘幂,2**3,表2的3次方。
字符串常量
在PERL中,字符串一般用双引号或单引号括起来,包含任何字符。
单引号与双引号的区别:
1. 单引号内的字符串直接量:不转换
#!/usr/bin/perl
$object = "apple";
$string = 'this is an $object';#单引号
print $string;
输出:this is an $object
2. 双引号内的字符串直接量:转换(变量插入)。
#!/usr/bin/perl
$object = "apple";
$string = "this is an $object";#双引号
print $string;
输出:this is an apple
3.双引号识别转义字符而单引号只识别两个特殊转义字符(/’,//)
$string = 'he//she is a student';
$string1 = '/'nothing here/'';
$string2 = 'no/nyes';
print "识别//:",$string,"/n";
print "识别/':",$string1,"/n";
print "不能识别:",$string2,"/n";
输出:
识别/:he/she is a student
识别':'nothing here'
不能识别:no/nyes
字符串操作:
.