基本上,簡單變量就是一個數(shù)據(jù)單元,這個單元可以是數(shù)字或字符串。
一、整型
1、整型
PERL最常用的簡單變量,由于其與其它語言基本相同,不再贅述。
例:
$x = 12345;
if (1217 + 116 == 1333) {
# statement block goes here
}
整型的限制:
PERL實際上把整數(shù)存在你的計算機中的浮點寄存器中,所以實際上被當(dāng)作浮點數(shù)看待。在多數(shù)計算機中,浮點寄存器可以存貯約16位數(shù)字,長于此的被丟棄。整數(shù)實為浮點數(shù)的特例。
2、8進制和16進制數(shù)
8進制以0打頭,16進制以0x打頭。
例:$var1 = 047; (等于十進制的39)
$var2 = 0x1f; (等于十進制的31)
二、浮點數(shù)
如 11.4 、 -0.3 、.3 、 3. 、 54.1e+02 、 5.41e03
浮點寄存器通常不能精確地存貯浮點數(shù),從而產(chǎn)生誤差,在運算和比較中要特別注意。指數(shù)的范圍通常為-309到+308。
例:
#!/usr/local/bin/perl
$value = 9.01e+21 + 0.01 - 9.01e+21;
print ("first value is ", $value, "/n");
$value = 9.01e+21 - 9.01e+21 + 0.01;
print ("second value is ", $value, "/n");
---------------------------------------------------------
$ program3_3
first value is 0
second value is 0.01
三、字符串
慣用C的程序員要注意,在PERL中,字符串的末尾并不含有隱含的NULL字符,NULL字符可以出現(xiàn)在串的任何位置。
. 雙引號內(nèi)的字符串中支持簡單變量替換,例如:
$number = 11;
$text = "This text contains the number $number.";
則$text的內(nèi)容為:"This text contains the number 11."
.雙引號內(nèi)的字符串中支持轉(zhuǎn)義字符
Table 3.1. Escape sequences in strings.
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 |