简单变量有整型、浮点型和字符串。$varname用来表示一个变量:
- #整型
- $a = 23; #十进制
- $b = 037; #八进制
- $c = 0xff; #十六进制
- print "\$a = $a \$b = $b \$c = $c\n";
- #浮点型
- $f = 33.5;
- $g = 3.35e1;
- $h = 335e-1;
- print "\$f = $f \$g = $g \$h = $h\n";
- #字符串
- $s = "Hello";
- $u = "\137"; #八进制ASCII码
- $v = "\x6f"; #十六进制ACSII码
- $w = '$s and \$s #单引号不替换变量,不支持转义字符,也不支持注释
- the secode line'; #还可以跨行
- print "\$s = $s \$u = $u \$v = $v \$w = $w\n";
程序输出:
- $a = 23 $b = 31 $c = 255
- $f = 33.5 $g = 33.5 $h = 33.5
- $s = Hello $u = _ $v = o $w = $s and \$s #单引号不替换变量,不支持转义字符,也不支持注释
- the secode line
从上面的程序可以看到双引号里的字符串支持转义字符,可以有替换变量,而单引号却不支持。值得注意的是,Perl里的整型其实是存放在浮点寄存器里的,被当成浮点数对待。
下表是支持的转义字符:
Escape Sequence
|
Description
|
\a
|
Bell (beep)
|
\b
|
Backspace
|
\cn
|
The Ctrl+n character
|
\e
|
Escape
|
\E
|
Ends the effect of \L, \U or \Q
|
\f
|
Form feed
|
\l
|
Forces the next letter into lowercase
|
\L
|
All following letters are lowercase
|
\n
|
Newline
|
\r
|
Carriage return
|
\Q
|
Do not look for special pattern characters
|
\t
|
Tab
|
\u
|
Force next letter into uppercase
|
\U
|
All following letters are uppercase
|
\v
|
Vertical tab
|
字符串也可以转换为数值:
- #字符串和数值的转换
- $string = "53";
- $number = 47;
- $result = $string + $number;
- print "\$result=$result\n"; # $result=100
- #字符串中开头的所有数字字符可以视为一个数值。
- $result = "hello" * 5;
- print "\$result=$result\n"; # $result=0
- $result = "12a34" + 1;
- print "\$result=$result\n"; # $result=13
阅读(937) | 评论(2) | 转发(1) |