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

📄 4.html

📁 介绍linux下文件和设备编程
💻 HTML
📖 第 1 页 / 共 5 页
字号:
请看下面的例子:<br>1#!/usr/bin/perl<br>2#<br>3#AnexampletoshowhowarraysworkinPerl<br>4#<br>5@amounts=(10,24,39);<br>6@parts=('computer','rat',&quot;kbd&quot;);<br>7<br>8$a=1;$b=2;$c='3';<br>9@count=($a,$b,$c);<br>10<br>11@empty=();<br>12<br>13@spare=@parts;<br>14<br>15print'@amounts=';<br>16print&quot;@amounts\n&quot;;<br>17<br>18print'@parts=';<br>19print&quot;@parts\n&quot;;<br>20<br>21print'@count=';<br>22print&quot;@count\n&quot;;<br>23<br>24print'@empty=';<br>25print&quot;@empty\n&quot;;<br>26<br>27print'@spare=';<br>28print&quot;@spare\n&quot;;<br>29<br>30<br>31#<br>32#Accessingindividualitemsinanarray<br>33#<br>34print'$amounts[0]=';<br>35print&quot;$amounts[0]\n&quot;;<br>36print'$amounts[1]=';<br>37print&quot;$amounts[1]\n&quot;;<br>38print'$amounts[2]=';<br>39print&quot;$amounts[2]\n&quot;;<br>40print'$amounts[3]=';<br>41print&quot;$amounts[3]\n&quot;;<br>42<br>43print&quot;Itemsin\@amounts=$#amounts\n&quot;;<br>44$size=@amounts;print&quot;SizeofAmount=$size\n&quot;;<br>45print&quot;Item0in\@amounts=$amounts[$[]\n&quot;;<br>以下是显示结果:<br>@amounts=102439<br>@parts=computerratkbd<br>@count=123<br>@empty=<br>@spare=computerratkbd<br>$amounts[0]=10<br>$amounts[1]=24<br>$amounts[2]=39<br>$amounts[3]=<br>Itemsin@amounts=2<br>SizeofAmount=3<br>Item<br>&nbsp;&nbsp;&nbsp; 第5行,三个整数值赋给了数组@amounts。第6行,三个字符串赋给了数组@parts。第8行,字符串和数字分别赋给了三个变量,然后将三个变量赋给了数组@count。11行创建了一个空数组。13行将数组@spare赋给了数组@parts。15到28行输出了显示的前5行。34到41行分别存取数组@amounts的每个元素。注意$amount[3]不存在,所以显示一个空项。43行中使用$#array方式显示一个数组的最后一个下标,所以数组@amounts的大小是($#amounts+1)。44行中将一个数组赋给了一个标量,则将数组的大小赋给了标量。45行使用了一个Perl中的特殊变量$[,用来表示一个数组的起始位置(缺省为0)。<p>相关数组<br>&nbsp;&nbsp;&nbsp; 一般的数组允许我们通过数字下标存取其中的元素。例如数组@food的第一个元素是$food[0],第二个元素是$food[1],以此类推。但Perl允许创建相关数组,这样我们可以通过字符串存取数组。其实,一个相关数组中每个下标索引对应两个条目,第一个条目叫做关键字,第二个条目叫做数值。这样,你就可以通过关键字来读取数值。相关数组名以百分号(%)开头,通过花括号({})引用条目。例如:<p>%ages=(&quot;MichaelCaine&quot;,39,<br>&quot;DirtyDen&quot;,34,<br>&quot;Angie&quot;,27,<br>&quot;Willy&quot;,&quot;21indogyears&quot;,<br>&quot;TheQueenMother&quot;,108);<br>这样我们可以通过下面的方法读取数组的值:<br>$ages{&quot;MichaelCaine&quot;};#Returns39<br>$ages{&quot;DirtyDen&quot;};#Returns34<br>$ages{&quot;Angie&quot;};#Returns27<br>$ages{&quot;Willy&quot;};#Returns&quot;21indogyears&quot;<br>$ages{&quot;TheQueenMother&quot;};#Returns108<p><p><center><A HREF="#Content">[目录]</A></center><hr><br><A NAME="I135" ID="I135"></A><center><b><font size=+2>文件操作</font></b></center><br>文件句柄和文件操作<br>&nbsp;&nbsp;&nbsp; 我们可以通过下面的程序了解一下文件句柄的基本用法。此程序的执行结果和UNIX系统<br>的cat命令一样:<br>#!/usr/local/bin/perl<br>#<br>#Programtoopenthepasswordfile,readitin,<br>#printit,andcloseitagain.<br>$file='/etc/passwd';#Namethefile<br>open(INFO,$file);#Openthefile<br>@lines=&lt;INFO&gt;;#Readitintoanarray<br>close(INFO);#Closethefile<br>print@lines;#Printthearray<p>&nbsp;&nbsp;&nbsp; open函数打开一个文件以供读取,其中第一个参数是文件句柄(filehandle)。文件句柄用来供Perl在以后指向该文件。第二个参数指向该文件的文件名。close函数关闭该文件。<br>open函数还可以用来打开文件以供写入和追加,只须分别在文件名之前加上&gt;和&gt;&gt;:<br>open(INFO,$file);#Openforinput<br>open(INFO,&quot;&gt;$file&quot;);#Openforoutput<br>open(INFO,&quot;&gt;&gt;$file&quot;);#Openforappending<br>open(INFO,&quot;&lt;$file&quot;);#Alsoopenforinput<p>&nbsp;&nbsp;&nbsp; 另外,如果你希望输出内容到一个已经打开供写入的文件中,你可以使用带有额外参数的print语句。例如:<br>printINFO&quot;Thislinegoestothefile.\n&quot;;<br>&nbsp;&nbsp;&nbsp; 最后,可以使用如下的语句打开标准输入(通常为键盘)和标准输出(通常为显示器):<br>open(INFO,'-');#Openstandardinput<br>open(INFO,'&gt;-');#Openstandardoutput<br>&nbsp;&nbsp;&nbsp; 一个Perl程序在它一启动时就已经有了三个文件句柄:STDIN(标准输入设备),STDOUT(标准输出设备)和STDERR(标准错误信息输出设备)。如果想要从一个已经打开的文件句柄中读取信息,可以使用&lt;&gt;运算符。<p>&nbsp;&nbsp;&nbsp; 使用read和write函数可以读写一个二进制的文件。其用法如下:<br>read(HANDLE,$buffer,$length[,$offset]);<br>&nbsp;&nbsp;&nbsp; 此命令可以把文件句柄是HANDLE的文件从文件开始位移$offset处,共$length字节,读到$buffer中。其中$offset是可选项,如果省略$offset,则read()从当前位置的前$length个字节读取到当前位置。可以使用下面的命令查看是否到了文件末尾:<br>eof(HANDLE);<br>&nbsp;&nbsp;&nbsp; 如果返回一个非零值,则说明已经到达文件的末尾。<br>&nbsp;&nbsp;&nbsp; 打开文件时可能出错,所以可以使用die()显示出错信息。下面打开一个叫做“test.data”的文件:<br>open(TESTFILE,&quot;test.data&quot;)||die&quot;\n$0Cannotopen$!\n&quot;;<p><p><center><A HREF="#Content">[目录]</A></center><hr><br><A NAME="I136" ID="I136"></A><center><b><font size=+2>流程控制</font></b></center><br>foreach循环<br>&nbsp;&nbsp;&nbsp; 在Perl中,可以使用foreach循环来读取数组或其他类似列表结构中的每一行。请看下面的例子:<p>foreach$morsel(@food)#Visiteachiteminturn<br>#andcallit$morsel<br>{<br>print&quot;$morsel\n&quot;;#Printtheitem<br>print&quot;Yumyum\n&quot;;#Thatwasnice<br>}<p>&nbsp;&nbsp;&nbsp; 每次要执行的命令用花括号括出。第一次执行时$morsel被赋予数组@food的第一个元素的值,第二次执行时$morsel被赋予数组@food的第二个元素的值,以此类推直到数组的最后一个元素。<p>判断运算<br>&nbsp;&nbsp;&nbsp; 在Perl中任何非零的数字和非空的字符串都被认为是真。零、全为零的字符串和空字符串都为假。<p>下面是一些判断运算符:<br>$a==$b如果$a和$b相等,则返回真。<br>$a!=$b如果$a和$b不相等,则返回真。<br>$aeq$b如果字符串$a和字符串$b相同,则返回真<br>$ane$b如果字符串$a和字符串$b不相同,则返回真。<p>你可以使用逻辑运算符:<br>($a&amp;&amp;$b)$a与$b。<br>($a||$b)$a或$b。<br>!($a)非$a。<p>for循环<br>&nbsp;&nbsp;&nbsp; Perl中的for结构和C语言中的for结构基本一样:<p>for(initialise;test;inc){<br>first_action;<br>second_action;<br>etc<br>}<p>&nbsp;&nbsp;&nbsp; 下面是一个for循环的例子,用来显示从0到9的数字:<p>for($i=0;$i&lt;10;++$i)#Startwith$i=1<br>#Doitwhile$i&lt;10<br>#Increment$ibeforerepeating<br>{<br>print&quot;$i\n&quot;;<br>}<p>while和until循环<br>&nbsp;&nbsp;&nbsp; 下面是一个while和until循环的例子。它从键盘读取输入直到得到正确的口令为止。<p>#!/usr/local/bin/perl<br>print&quot;Password?&quot;;#Askforinput<br>$a=&lt;STDIN&gt;;#Getinput<br>chop$a;#Removethenewlineatend<br>while($ane&quot;fred&quot;)#Whileinputiswrong...<br>{<br>print&quot;sorry.Again?&quot;;#Askagain<br>$a=&lt;STDIN&gt;;#Getinputagain<br>chop$a;#Chopoffnewlineagain<br>}<p>当输入和口令不相等时,执行while循环。<br>你也可以在执行体的末尾处使用while和until,这时需要用do语句:<p>#!/usr/local/bin/perl<br>do<br>{<br>&quot;Password?&quot;;#Askforinput<br>$a=&lt;STDIN&gt;;#Getinput<br>chop$a;#Chopoffnewline<br>}<br>while($ane&quot;fred&quot;)#Redowhilewronginput<p>条件结构<br>&nbsp;&nbsp;&nbsp; Perl也允许if/then/else表达式。请看下面的例子:<p>if($a){<br>print&quot;Thestringisnotempty\n&quot;;<br>}<br>else{<br>print&quot;Thestringisempty\n&quot;;<br>}<br>注意在Perl中,空字符被认为是假。<p>&nbsp;&nbsp;&nbsp; If结构中也可以使用嵌套结构:<p>if(!$a)#The!isthenotoperator<br>{<br>print&quot;Thestringisempty\n&quot;;<br>}<br>elsif(length($a)==1)#Ifabovefails,trythis<br>{<br>print&quot;Thestringhasonecharacter\n&quot;;<br>}<br>elsif(length($a)==2)#Ifthatfails,trythis<br>{<br>print&quot;Thestringhastwocharacters\n&quot;;<br>}<br>else#Now,everythinghasfailed<br>{<br>print&quot;Thestringhaslotsofcharacters\n&quot;;<p><p><center><A HREF="#Content">[目录]</A></center><hr><br><A NAME="I137" ID="I137"></A><center><b><font size=+2>字符匹配</font></b></center><br>&nbsp;&nbsp;&nbsp; Perl字符匹配功能十分强大。字符匹配功能的核心是规则表达式(RE),也就是字符匹配过程中涉及到的格式。=~运算符用来进行格式匹配和替换。例如:<br>如果:<br>$s='Oneifbylandandtwoifbysea';<br>则:<br>if($s=~/ifbyla/){print&quot;YES&quot;}<br>else{print&quot;NO&quot;}<br>&nbsp;&nbsp;&nbsp; 将会显示YES,因为ifbyla在字符串$s中。再例如:<br>if($s=~/one/){print&quot;YES&quot;}<br>else{print&quot;NO&quot;}<br>&nbsp;&nbsp;&nbsp; 将显示NO,因为RE是对大小写敏感的。如果使用i选项,则将忽略大小写,则下面会显示出YES:<br>if($s=~/one/i){print&quot;YES&quot;}<br>else{print&quot;NO&quot;}<p>下面列出了RE中许多具有特殊意义的字符:<p>.任何字符除了换行符(\n)<br>^一行和一个字符串的开始<br>$一行和一个字符串的结束<br>*其前一个字符重复零次或多次<br>+其前一个字符重复一次或多次<br>?其前一个字符重复零次或一次<p>例如:<br>if($x=~/l.mp/){print&quot;YES&quot;}<br>对于$x=“lamp”、“lump”、“slumped”将显示YES,但对于$x=“lmp”或“lessa

⌨️ 快捷键说明

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