⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 perl2.htm

📁 Perl作为一门重要的工程语言
💻 HTM
字号:
<html><!-- #BeginTemplate "/Templates/index.dwt" --><head><meta http-equiv=Content-Type content="text/html; charset=gb_2312-80"><title>ChinaPerl</title>
<LINK rel=stylesheet href=mystyle.css type=text/css>
</head><body>
<table border=0 cellspacing=0 bgcolor=#80FFFF width=100%>
  <tr>
    <td align=left rowspan=2 width=150>
      <div align="center"><img src=image/perl.gif height=85 width="85"></div>
    </td>
    <td align=center><img src=image/perl.gif height=85 width="85"></td>
  </tr><tr>
    <td colspan=2 align=center>&nbsp;</td>
  </tr></table>
<table border=0 width=100% height=400 cellspacing=0 cellpadding=0>
  <tr>
    <td width=20% valign=top bgcolor="#dddddd"> 
<table border=0 width=100% cellpadding=0><tr><td width=5%></td>
          <td width=95%> 
            <p align=center>Perl 5教程<br>
              <a target=_blank href=http://flamephoenix.126.com>by flamephoenix</a><a href="http://perl.yesky.net"><br>
              <font color="red">修订 by RNY<br>
              </font></a>2000-08-30</p>
            <p align=center><a href="index.htm">目录</a></p>
            <p align=center>第一部分 Perl语言</p>
            <p><a href=perl1.htm>第一章 概述</a></p>
            <p><a href=perl2.htm>第二章 简单变量</a></p>
            <p><a href=perl3.htm>第三章 操作符</a></p>
            <p><a href=perl4.htm>第四章 列表和数组变量</a></p>
            <p><a href=perl5.htm>第五章 文件读写</a></p>
            <p><a href=perl6.htm>第六章 模式匹配</a></p>
            <p><a href=perl7.htm>第七章 控制结构</a></p>
            <p><a href=perl8.htm>第八章 子程序</a></p>
            <p><a href=perl9.htm>第九章 关联数组(哈希表)</a></p>
            <p><a href=perl10.htm>第十章 格式化输出</a></p>
            <p><a href=perl11.htm>第十一章 文件系统</a></p>
            <p><a href=perl12.htm>第十二章 引用(指针)</a></p>
            <p><a href=perl13.htm>第十三章 面向对象编程</a></p>
            <p><a href=perl14.htm>第十四章 包和模块</a></p>
            <p><a href=func.htm>附录一 函数集</a></p>
            <p align=center>第二部分 Perl的CGI应用</p>
            <p><a href=cgi1.htm>第一章 cgilib例</a></p>
            <p><a href=cgi2.htm>第二章 动态创建图像</a></p>
            <p><a href=cgi3.htm>第三章 计数器的编写方法</a></p>
            <p><a href=cgi4.htm>第四章 Web Server设置</a></p>
            第五章、...<br></td></tr></table></td>
    <td width=80% valign=top>
      <div align="center"></div>
      <table border=0 width=100% cellpadding=0>
        <tr bgcolor="#00FF80"> 
          <td colspan="2"> 
            <div align="center"> Perl教程 | <a href="perl5manual.zip">下载全文</a> 
              | <a target=_blank href=http://www.turbolinux.com.cn/cgi-bin/linux/wwwthreads.pl?action=list&Board=perl>Perl论坛</a> 
              | <a href="http://perl.yesky.net">推荐站点</a> | 关于我们 </div>
          </td>
        </tr>
        <tr> 
          <td colspan="2"><!-- #BeginEditable "body" --> 
            <p align=center>第二章 简单变量</p>
            <p align=center><a target=_blank href=http://flamephoenix.126.com>by 
              flamephoenix</a></p>
            <a href=#1、整型>一、整型</a><br>
            <a href=#二、浮点数>二、浮点数</a><br>
            <a href=#三、字符串>三、字符串</a>
            <p></p>
            <p>基本上,简单变量就是一个数据单元,这个单元可以是数字或字符串。<br>
              <a name=1、整型>一、整型</a><br>
              1、整型<br>
              &nbsp;&nbsp;PERL最常用的简单变量,由于其与其它语言基本相同,不再赘述。<br>
              &nbsp;&nbsp; 例:<br>
              &nbsp;&nbsp; $x = 12345;<br>
              &nbsp;&nbsp; if (1217 + 116 == 1333) {<br>
              &nbsp;&nbsp; # statement block goes here<br>
              &nbsp;&nbsp; }<br>
              &nbsp;&nbsp;整型的限制:<br>
              &nbsp;&nbsp; PERL实际上把整数存在你的计算机中的浮点寄存器中,所以实际上被当作浮点数看待。在多数计算机中,浮点寄存器可以存贮约16位数字,长于此的被丢弃。整数实为浮点数的特例。<br>
              2、8进制和16进制数<br>
              &nbsp; 8进制以0打头,16进制以0x打头。<br>
              &nbsp; 例:$var1 = 047; (等于十进制的39)<br>
              &nbsp; $var2 = 0x1f; (等于十进制的31)<br>
              <a name=二、浮点数>二、浮点数</a><br>
              &nbsp; 如 11.4 、 -0.3 、.3 、 3. 、 54.1e+02 、 5.41e03<br>
              &nbsp; 浮点寄存器通常不能精确地存贮浮点数,从而产生误差,在运算和比较中要特别注意。指数的范围通常为-309到+308。<br>
              &nbsp; 例:<br>
              <br>
              &nbsp; #!/usr/local/bin/perl<br>
              &nbsp; $value = 9.01e+21 + 0.01 - 9.01e+21;<br>
              &nbsp; print (&quot;first value is &quot;, $value, &quot;\n&quot;);<br>
              &nbsp; $value = 9.01e+21 - 9.01e+21 + 0.01;<br>
              &nbsp; print (&quot;second value is &quot;, $value, &quot;\n&quot;);<br>
              <br>
              &nbsp; ---------------------------------------------------------<br>
              <br>
              &nbsp; $ program3_3<br>
              &nbsp; first value is 0<br>
              &nbsp; second value is 0.01<br>
              <a name=三、字符串>三、字符串</a><br>
              &nbsp; 惯用C的程序员要注意,在PERL中,字符串的末尾并不含有隐含的NULL字符,NULL字符可以出现在串的任何位置。<br>
              . 双引号内的字符串中支持简单变量替换,例如:<br>
              &nbsp; $number = 11;<br>
              &nbsp; $text = &quot;This text contains the number $number.&quot;;<br>
              &nbsp; 则$text的内容为:&quot;This text contains the number 11.&quot;<br>
              <font color="red">perl 替换时,会尽可能寻找最长的变量名,可能会造成麻烦,可以在变量名前后有大括号括起来,如<br>
              $num=2;<br>
              $text = &quot;This text contains the number ${num}ber.&quot;;<br>
              #text is now &nbsp; &quot;This text contains the number 2ber.&quot;<br>
              </font> <br>
              .双引号内的字符串中支持转义字符<br>
              <b>Table 3.1. Escape sequences in strings.</b></p>
            <div align=center>
              <center>
                <table border=1 width=60%>
                  <tr>
                    <td valign=top width=145><b>Escape Sequence</b></td>
                    <td valign=top width=335><b>Description</b></td>
                  </tr>
                  <tr>
                    <td valign=top width=145><tt>\a</tt></td>
                    <td valign=top width=335>Bell (beep)</td>
                  </tr>
                  <tr>
                    <td valign=top width=145><tt>\b</tt></td>
                    <td valign=top width=335>Backspace</td>
                  </tr>
                  <tr>
                    <td valign=top width=145><tt>\cn</tt></td>
                    <td valign=top width=335>The Ctrl+<tt>n</tt> character</td>
                  </tr>
                  <tr>
                    <td valign=top width=145><tt>\e</tt></td>
                    <td valign=top width=335>Escape</td>
                  </tr>
                  <tr>
                    <td valign=top width=145><tt>\E</tt></td>
                    <td valign=top width=335>Ends the effect of <tt>\L</tt>, <tt>\U</tt> 
                      or <tt>\Q</tt></td>
                  </tr>
                  <tr>
                    <td valign=top width=145><tt>\f</tt></td>
                    <td valign=top width=335>Form feed</td>
                  </tr>
                  <tr>
                    <td valign=top width=145><tt>\l</tt></td>
                    <td valign=top width=335>Forces the next letter into lowercase</td>
                  </tr>
                  <tr>
                    <td valign=top width=145><tt>\L</tt></td>
                    <td valign=top width=335>All following letters are lowercase</td>
                  </tr>
                  <tr>
                    <td valign=top width=145><tt>\n</tt></td>
                    <td valign=top width=335>Newline</td>
                  </tr>
                  <tr>
                    <td valign=top width=145><tt>\r</tt></td>
                    <td valign=top width=335>Carriage return</td>
                  </tr>
                  <tr>
                    <td valign=top width=145><tt>\Q</tt></td>
                    <td valign=top width=335>Do not look for special pattern characters</td>
                  </tr>
                  <tr>
                    <td valign=top width=145><tt>\t</tt></td>
                    <td valign=top width=335>Tab</td>
                  </tr>
                  <tr>
                    <td valign=top width=145><tt>\u</tt></td>
                    <td valign=top width=335>Force next letter into uppercase</td>
                  </tr>
                  <tr>
                    <td valign=top width=145><tt>\U</tt></td>
                    <td valign=top width=335>All following letters are uppercase</td>
                  </tr>
                  <tr>
                    <td valign=top width=145><tt>\v</tt></td>
                    <td valign=top width=335>Vertical tab</td>
                  </tr>
                </table>
              </center>
            </div>
            <p align=left>&nbsp;\L、\U、\Q功能可以由\E关闭掉,如:<br>
              &nbsp;$a = &quot;T\LHIS IS A \ESTRING&quot;; # same as &quot;This 
              is a STRING&quot;<br>
              <br>
              .要在字符串中包含双引号或反斜线,则在其前加一个反斜线,反斜线还可以取消变量替换,如:<br>
              &nbsp;&nbsp;$res = &quot;A quote \&quot; and A backslash \\&quot;;<br>
              &nbsp;&nbsp;$result = 14;<br>
              &nbsp;&nbsp;print (&quot;The value of \$result is $result.\n&quot;)的结果为:<br>
              &nbsp;&nbsp;The value of $result is 14.<br>
              <br>
              .可用\nnn(8进制)或\xnn(16进制)来表示ASCII字符,如:<br>
              &nbsp;&nbsp;$result = &quot;\377&quot;; # this is the character 
              255,or EOF<br>
              &nbsp;&nbsp;$result = &quot;\xff&quot;; # this is also 255<br>
              <br>
              .单引号字符串<br>
              &nbsp;&nbsp;单引号字符串与双引号字符串有两个区别,一是没有变量替换功能,二是反斜线不支持转义字符,而只在包含单引号和反斜线时起作用。单引号另一个特性是可以跨多行,如:<br>
              &nbsp;&nbsp;$text = 'This is two<br>
              &nbsp;&nbsp;lines of text<br>
              &nbsp;&nbsp;';<br>
              &nbsp;&nbsp;与下句等效:<br>
              &nbsp;&nbsp;$text = &quot;This is two\nlines of text\n&quot;;<br>
              <br>
              .字符串和数值的互相转换<br>
              &nbsp;&nbsp;例1:<br>
              &nbsp;&nbsp;$string = &quot;43&quot;;<br>
              &nbsp;&nbsp;$number = 28;<br>
              &nbsp;&nbsp;$result = $string + $number; # $result = 71<br>
              &nbsp;&nbsp;若字符串中含有非数字的字符,则从左起至第一个非数字的字符,如:<br>
              &nbsp;&nbsp;$result = &quot;hello&quot; * 5; # $result = 0<br>
              &nbsp;&nbsp;$result = &quot;12a34&quot; +1; # $result = 13<br>
              <br>
              .变量初始值<br>
              &nbsp;&nbsp;在PERL中,所有的简单变量都有缺省初始值:&quot;&quot;,即空字符。但是建议给所有变量赋初值,否则当程序变得大而复杂后,很容易出现不可预料且很难调试的错误。<br>
            </p>
            <p align=center><a href=perl1.htm>上一章</a> <a href=perl3.htm>下一章</a> 
              <a href=index.htm>目录</a></p>
            <br>
            <!-- #EndEditable --></td>
        </tr>
      </table>
    </td></tr></table></body><!-- #EndTemplate --></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -