分类:
2009-06-04 16:45:36
标量一般为单数。包含数值类型和字符串。
Perl把所有数值按双精度浮点数来处理。
数值举例:
浮点型
255.000 255.0 7.25e45 # 7.25 times 10 to the 45th power (a big number) -6.5e24 # negative 6.5 times 10 to the 24th # (a big negative number) -12e-24 # negative 12 times 10 to the -24th # (a very small negative number)-1.2E-23 # another way to say that - the E may be uppercase整型:
0 2001 -40 25561298040283768 也可以这样书写:61_298_040_283_768,逗号有其他用途。
非十进制: 0377 # 377 octal, same as 255 decimal 0xff # FF hex, also 255 decimal 0b11111111 # also 255 decimal转换可以使用oct( ) or hex( ).同样可以使用分隔符:
0x1377_0B770x50_65_72_7C 运算:
2 + 3 # 2 plus 3, or 5 5.1 - 2.4 # 5.1 minus 2.4, or 2.7 3 * 12 # 3 times 12 = 36 14 / 2 # 14 divided by 2, or 7 10.2 / 0.3 # 10.2 divided by 0.3, or 34 10 / 3 # always floating-point divide, so 3.3333333...取模:10.5 % 3.2 is computed as 10 % 3
取幂:**,不适合负数。可以使用Math::Complex来实现。
Perl中没有NUL,是用字符数来统计的。
单引号字符串:
除了单引号和反斜线之外,都是代表自己。
'fred' # those four characters: f, r, e, and d
'barney' # those six characters
'' # the null string (no characters)
'Don\'t let an apostrophe end this string prematurely!'
'the last character of this string is a backslash: \\'
'hello\n' # hello followed by backslash followed by n
'hello
there' # hello, newline, there (11 characters total)
'\'\\' # single quote followed by backslash
\n代表\n, 不代表转义。
双引号可以代表转义,有变量替换。
"barney" # just the same as 'barney'
"hello world\n" # hello world, and a newline
"The last character of this string is a quote mark: \""
"coke\tsprite" # coke, a tab, and sprite
转义字符表见教材。
字符串操作符
点号可以连接字符串。"hello" . "world" # same as "helloworld"
乘号可以复制:"hello" . "world" # same as "helloworld"。数值类型先转换为整数再复制。
数值和字符的自动转换:由操作符决定,比如;"12" * "3" 得36,"12fred34" * " 3"也是如此。非数值转为数值时值为0. "Z" . 5 * 7 # same as "Z" . 35, or "Z35"
使用方法:
$ perl -w my_program
#!/usr/bin/perl –w
#!perl –w
5.6以后可以使用:use warnings;
使用后:Argument "12fred34" isn't numeric
还可以使用诊断:use diagnostics;这2个东东都可参考perldiag的手册页。
另外一种方法:$ perl -Mdiagnostics ./my_program
区分大小写的。使用的时候都要加上$。
另有: +=,.=,**=等运算符。
$meal = "brontosaurus steak";
$barney = "fred ate a $meal"; # $barney is now "fred ate a brontosaurus steak"
$barney = 'fred ate a ' . $meal; # another way to write that
输出时可以不加引号:print $fred;
变量分割;
$what = "brontosaurus steak";
$n = 3;
print "fred ate $n $whats.\n"; # not the steaks, but the value of $whats
print "fred ate $n ${what}s.\n"; # now uses $what
print "fred ate $n $what" . "s.\n"; # another way to do it
print 'fred ate ' . $n . ' ' . $what . "s.\n"; # an especially difficult way
运算符的优先级参见教材31页。比较操作符见教材33页。
Perl没有专门的布尔类型,数值0表示false,其他表示true。字符串’’表示false。其他类型转换为数字或者字符串。字符串'0'也表示false。
输入的结束会有回车。CPAN的Term::ReadLine可以更好的控制输入。
$line =
if ($line eq "\n") {
print "That was just a blank line!\n";
} else {
print "That line of input was: $line";
}
可以使用chomp去掉回车。
$text = "a line of text\n"; # Or
the same thing from
chomp($text); # Gets rid of the newline character
chomp($text =
$text =
chomp($text); # ...but in two steps
有无括号均可,只去除一个换行符号。
变量初始具有undef值,针对数值类型为0。针对字符串为空串。
$madonna =
if ( defined($madonna) ) {
print "The input was $madonna";
} else {
print "No input available!\n";
}