📄 perl 语言-perl 中文教程(第二章).htm
字号:
name=Image18></A></TD></TR>
<TR>
<TD><A onmouseover="MM_swapImage('Image19','','y1a.gif',1)"
onmouseout=MM_swapImgRestore()
href="http://www.sun126.com/perl5/perl5-15.htm"><IMG
src="Perl 语言-Perl 中文教程(第二章).files/y1.gif" border=0
name=Image19></A></TD></TR>
<TR>
<TD><A onmouseover="MM_swapImage('Image20','','y2a.gif',1)"
onmouseout=MM_swapImgRestore()
href="http://www.sun126.com/perl5/perl5-16.htm"><IMG
src="Perl 语言-Perl 中文教程(第二章).files/y2.gif" border=0
name=Image20></A></TD></TR>
<TR>
<TD><A onmouseover="MM_swapImage('Image21','','y3a.gif',1)"
onmouseout=MM_swapImgRestore()
href="http://www.sun126.com/perl5/perl5-17.htm"><IMG
src="Perl 语言-Perl 中文教程(第二章).files/y3.gif" border=0
name=Image21></A></TD></TR></TBODY></TABLE></TD>
<TD class=myFont vAlign=top width=30><IMG height=1
src="Perl 语言-Perl 中文教程(第二章).files/x.gif" width=10> </TD>
<TD class=myFont vAlign=top>
<DIV align=center><IMG height=30
src="Perl 语言-Perl 中文教程(第二章).files/top.gif" width=180><BR><SPAN
class=myFont><SPAN class=myFont><FONT face=宋体>翻译:</FONT></SPAN></SPAN>
<SPAN class=myFont><SPAN class=myFont>flamephoenix</SPAN></SPAN> <BR>
<HR width="100%" SIZE=1>
<DIV align=left></DIV></DIV>
<P align=center><STRONG>第二章 简单变量 </STRONG><BR></P>
<P><A href="http://www.sun126.com/perl5/perl5-2.htm#1、整型">一、整型</A> <BR><A
href="http://www.sun126.com/perl5/perl5-2.htm#二、浮点数">二、浮点数</A> <BR><A
href="http://www.sun126.com/perl5/perl5-2.htm#三、字符串">三、字符串</A> </P>
<P>基本上,简单变量就是一个数据单元,这个单元可以是数字或字符串。<BR><BR><A name=1、整型>一、整型</A> <BR>1、整型
<BR> PERL最常用的简单变量,由于其与其它语言基本相同,不再赘述。<BR>
例:<BR> $x = 12345;<BR> if (1217 + 116 == 1333)
{<BR> # statement block goes here<BR>
}<BR> 整型的限制:<BR>
PERL实际上把整数存在你的计算机中的浮点寄存器中,所以实际上被当作浮点数看待。在多数计算机中,浮点寄存器可以存贮约16位数字,长于此的被丢弃。整数实为浮点数的特例。<BR>2、8进制和16进制数<BR>
8进制以0打头,16进制以0x打头。<BR> 例:$var1 = 047; (等于十进制的39)<BR> $var2 =
0x1f; (等于十进制的31)<BR><BR><A name=二、浮点数>二、浮点数</A> <BR> 如 11.4 、 -0.3
、.3 、 3. 、 54.1e+02 、 5.41e03<BR>
浮点寄存器通常不能精确地存贮浮点数,从而产生误差,在运算和比较中要特别注意。指数的范围通常为-309到+308。<BR>
例:<BR><BR> #!/usr/local/bin/perl<BR> $value = 9.01e+21 + 0.01
- 9.01e+21;<BR> print ("first value is ", $value, "\n");<BR>
$value = 9.01e+21 - 9.01e+21 + 0.01;<BR> print ("second value is ",
$value, "\n");<BR><BR>
---------------------------------------------------------<BR><BR> $
program3_3<BR> first value is 0<BR> second value is
0.01<BR><BR><A name=三、字符串>三、字符串</A> <BR>
惯用C的程序员要注意,在PERL中,字符串的末尾并不含有隐含的NULL字符,NULL字符可以出现在串的任何位置。<BR>.
双引号内的字符串中支持简单变量替换,例如:<BR> $number = 11;<BR> $text = "This text
contains the number $number.";<BR> 则$text的内容为:"This text contains
the number 11."<BR><BR>.双引号内的字符串中支持转义字符<BR><B>Table 3.1. Escape sequences
in strings.</B> </P>
<DIV align=center>
<CENTER>
<TABLE borderColor=forestgreen cellSpacing=0 cellPadding=2 width="60%"
border=1>
<TBODY>
<TR>
<TD class=myFont vAlign=top width=145><B>Escape Sequence</B></TD>
<TD class=myFont vAlign=top width=335><B>Description</B></TD></TR>
<TR>
<TD class=myFont vAlign=top width=145><TT>\a</TT></TD>
<TD class=myFont vAlign=top width=335>Bell (beep) </TD></TR>
<TR>
<TD class=myFont vAlign=top width=145><TT>\b</TT></TD>
<TD class=myFont vAlign=top width=335>Backspace </TD></TR>
<TR>
<TD class=myFont vAlign=top width=145><TT>\cn</TT></TD>
<TD class=myFont vAlign=top width=335>The Ctrl+<TT>n</TT> character
</TD></TR>
<TR>
<TD class=myFont vAlign=top width=145><TT>\e</TT></TD>
<TD class=myFont vAlign=top width=335>Escape </TD></TR>
<TR>
<TD class=myFont vAlign=top width=145><TT>\E</TT></TD>
<TD class=myFont vAlign=top width=335>Ends the effect of
<TT>\L</TT>, <TT>\U</TT> or <TT>\Q</TT> </TD></TR>
<TR>
<TD class=myFont vAlign=top width=145><TT>\f</TT></TD>
<TD class=myFont vAlign=top width=335>Form feed </TD></TR>
<TR>
<TD class=myFont vAlign=top width=145><TT>\l</TT></TD>
<TD class=myFont vAlign=top width=335>Forces the next letter into
lowercase </TD></TR>
<TR>
<TD class=myFont vAlign=top width=145><TT>\L</TT></TD>
<TD class=myFont vAlign=top width=335>All following letters are
lowercase </TD></TR>
<TR>
<TD class=myFont vAlign=top width=145><TT>\n</TT></TD>
<TD class=myFont vAlign=top width=335>Newline </TD></TR>
<TR>
<TD class=myFont vAlign=top width=145><TT>\r</TT></TD>
<TD class=myFont vAlign=top width=335>Carriage return </TD></TR>
<TR>
<TD class=myFont vAlign=top width=145><TT>\Q</TT></TD>
<TD class=myFont vAlign=top width=335>Do not look for special
pattern characters </TD></TR>
<TR>
<TD class=myFont vAlign=top width=145><TT>\t</TT></TD>
<TD class=myFont vAlign=top width=335>Tab </TD></TR>
<TR>
<TD class=myFont vAlign=top width=145><TT>\u</TT></TD>
<TD class=myFont vAlign=top width=335>Force next letter into
uppercase </TD></TR>
<TR>
<TD class=myFont vAlign=top width=145><TT>\U</TT></TD>
<TD class=myFont vAlign=top width=335>All following letters are
uppercase </TD></TR>
<TR>
<TD class=myFont vAlign=top width=145><TT>\v</TT></TD>
<TD class=myFont vAlign=top width=335>Vertical tab
</TD></TR></TBODY></TABLE><BR></CENTER></DIV>
<P align=left> \L、\U、\Q功能可以由\E关闭掉,如:<BR> $a = "T\LHIS IS A
\ESTRING"; # same as "This is a
STRING"<BR><BR>.要在字符串中包含双引号或反斜线,则在其前加一个反斜线,反斜线还可以取消变量替换,如:<BR> $res
= "A quote \" and A backslash \\";<BR> $result =
14;<BR> print ("The value of \$result is
$result.\n")的结果为:<BR> The value of $result is
14.<BR><BR>.可用\nnn(8进制)或\xnn(16进制)来表示ASCII字符,如:<BR> $result =
"\377"; # this is the character 255,or EOF<BR> $result =
"\xff"; # this is also
255<BR><BR>.单引号字符串<BR> 单引号字符串与双引号字符串有两个区别,一是没有变量替换功能,二是反斜线不支持转义字符,而只在包含单引号和反斜线时起作用。单引号另一个特性是可以跨多行,如:<BR> $text
= 'This is two<BR> lines of text<BR> ';
<BR> 与下句等效:<BR> $text = "This is two\nlines of
text\n"; <BR><BR>.字符串和数值的互相转换<BR> 例1:<BR> $string =
"43";<BR> $number = 28;<BR> $result = $string +
$number; # $result =
71<BR> 若字符串中含有非数字的字符,则从左起至第一个非数字的字符,如:<BR> $result =
"hello" * 5; # $result = 0<BR> $result = "12a34" +1; # $result
=
13<BR><BR>.变量初始值<BR> 在PERL中,所有的简单变量都有缺省初始值:"",即空字符。但是建议给所有变量赋初值,否则当程序变得大而复杂后,很容易出现不可预料且很难调试的错误。<BR></P></TD></TR></TBODY></TABLE>
<DIV align=center></DIV>
<DIV align=center><BR></DIV>
<DIV align=center><SPAN class=myFont><A
href="http://www.sun126.com/bcyj.htm">上页</A> <A
href="http://www.sun126.com/perl5/perl5-1.htm">下页</A> <A
href="http://www.sun126.com/perl5/perl5index.htm">回目录</A> <A
href="http://www.sun126.com/perl5/perl5-2.htm#a"><FONT face="Arial, 宋体">Go
Top</FONT></A></SPAN><BR><BR></DIV>
<TABLE height=50 cellSpacing=0 cellPadding=0 width="100%" bgColor=#000000
border=0>
<TBODY>
<TR>
<TD bgColor=#cccc99 height=4>
<DIV align=center><IMG height=4 src="" width=4></DIV></TD></TR>
<TR>
<TD height=50>
<DIV align=center><FONT class=myfont size=2><SPAN class=myfont><FONT
color=#99cc99><A href="http://www.sun126.com/bbs/ccb/index.cgi"><FONT
color=#99cc99>中国CCB论坛</FONT></A> 整理 麻辣
2003.7.10</FONT></SPAN></FONT><FONT class=myfont color=#99cc99
size=2><SPAN class=myfont><FONT
color=#99cc66><BR></FONT></SPAN></FONT><SPAN class=myfont><FONT
class=myfont><SPAN class=myfont><FONT face="Arial, Helvetica, sans-serif"
color=#99cc99>© 2000
http://www.sun126.com</FONT></SPAN></FONT></SPAN></DIV></TD></TR></TBODY></TABLE></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -