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

📄 513-516.html

📁 linux-unix130.linux.and.unix.ebooks130 linux and unix ebookslinuxLearning Linux - Collection of 12 E
💻 HTML
字号:
<HTML>

<HEAD>

<TITLE>Linux Unleashed, Third Edition:Perl</TITLE>

<SCRIPT>
<!--
function displayWindow(url, width, height) {
        var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>

 -->




<!--ISBN=0672313723//-->

<!--TITLE=Linux Unleashed, Third Edition//-->

<!--AUTHOR=Tim Parker//-->

<!--PUBLISHER=Macmillan Computer Publishing//-->

<!--IMPRINT=Sams//-->

<!--CHAPTER=28//-->

<!--PAGES=513-516//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="510-513.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="516-518.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<P>The <TT>each</TT> operator enables you to easily iterate through each element that is stored in an associative array. When you run the <TT>each</TT> operator on an associative array, it returns the first element of the array. Successive calls of <TT>each</TT> on the same array return the next element in the array until the last element is reached. The following code goes through the steps the <TT>coffee</TT> array used in the last example and displays the price of each kind of coffee stored in the array.</P>

<!-- CODE SNIP //-->

<PRE>

while ((&#36;type, &#36;cost) = each(%coffee)) &#123;

    print &#147;The price of &#36;type coffee is &#36;cost\n&#148;;

&#125;

</PRE>

<!-- END CODE SNIP //-->

<H3><A NAME="Heading10"></A><FONT COLOR="#000077">Perl Programming Constructs</FONT></H3>

<P>Perl provides many of the same programming constructs that are found in all high-level programming languages. This section describes each of the programming constructs provided by Perl.

</P>

<H4 ALIGN="LEFT"><A NAME="Heading11"></A><FONT COLOR="#000077">Statement Blocks</FONT></H4>

<P>Perl statement blocks consist of one or more Perl statements enclosed in curly braces. The following code shows the sample syntax for a Perl statement block:

</P>

<!-- CODE SNIP //-->

<PRE>

&#123;

        statement1;

        statement2;

        &#133;

&#125;

</PRE>

<!-- END CODE SNIP //-->

<P>Statement blocks can be used anywhere that a single Perl statement can be used. They are most commonly used to group statements that are to be executed as part of an iteration statement, such as a <TT>for</TT> or <TT>while</TT> statement or as part of a conditional statement, such as an <TT>if</TT> statement.</P>

<H4 ALIGN="LEFT"><A NAME="Heading12"></A><FONT COLOR="#000077">if Statements</FONT></H4>

<P>Perl&#146;s <TT>if</TT> statement is used to execute statement blocks conditionally. The syntax of the <TT>if</TT> statement is similar to the <TT>if</TT> statement syntax used in C. This syntax is shown by the following:</P>

<!-- CODE SNIP //-->

<PRE>

if (expression) &#123;

        statements;

&#125;

</PRE>

<!-- END CODE SNIP //-->

<P>In this form, the <TT>if</TT> statement evaluates the expression, and if the result of that evaluation is True, it executes the statements contained in the statement block. If the expression evaluates to False, the statements in the statement block are not evaluated.</P>

<P>The <TT>if</TT> statement can be optionally accompanied by one or more <TT>else if</TT> clauses and one <TT>else</TT> clause. The syntax for a statement that contains two <TT>else if</TT> clauses and an <TT>else</TT> clause is shown in the following code:</P>

<!-- CODE //-->

<PRE>

if (expression1) &#123;

        statements;

&#125; elsif (expression2) (

        statements;

&#125; elsif (expression3) &#123;

        statements;

&#125; else &#123;

        statements;

&#125;

</PRE>

<!-- END CODE //-->

<P>In this form, the <TT>if</TT> statement evaluates <TT>expression1</TT>, and if the result of that evaluation is True, it executes the statements in the first statement block. If <TT>expression1</TT> evaluates to False, <TT>expression2</TT> is evaluated. If <TT>expression2</TT> evaluates to True, the statements in the second statement block are executed. If <TT>expression2</TT> evaluates to False, the process is repeated for the third expression. If <TT>expression3</TT> evaluates to False, the statements in the last statement block are executed. The important thing to remember here is that one and only one of the statement blocks is executed.</P>

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR><B>Note:&nbsp;&nbsp;</B><BR>In Perl, an expression evaluates to True if it has a value other than zero and to False if it has a value of zero. This is different than utilities like test, which are the opposite. The reason for the difference is because Perl uses the C language&#146;s syntax for consistency among programming languages.<HR></FONT>

</BLOCKQUOTE>

<P>The following example illustrates the use of an <TT>if</TT> statement:</P>

<!-- CODE //-->

<PRE>

print &#147;Please enter your name or \&#148;help\&#148; for a \n&#148;;

print &#147;description of this program\n&#148;;

&#36;name=&lt;STDIN&gt;;

chop(&#36;name);

if ( &#36;name eq &#147;&#148; ) &#123;

        print &#147;This program cannot proceed without your name.\n&#148;;

        exit;

&#125; elsif ( &#36;name eq &#147;help&#148;) &#123;

        print &#147;This program reads in the user&#146;s name and \n&#148;;

        print &#147;prints a welcome message on the screen if the\n&#148;;

        print &#147;name that was entered was not null.\n&#148;;

&#125; else &#123;

        print &#147;Hello &#36;name, welcome to Perl.\n&#148;;

&#125;

</PRE>

<!-- END CODE //-->

<P>This program asks for your name by displaying a prompt to the screen and waiting for you to enter your name on the command line. The third line of the program assigns whatever you type on the command line to the <TT>&#36;name</TT> variable. The <TT>if</TT> statement then checks to make sure that you typed something when you were asked for your name. If the <TT>&#36;name</TT> variable is empty, the program prints a statement that informs you that you must enter your name if you want to proceed. Next, the statement checks to see if you typed the name <TT>help</TT> on the command line. If you did, the program displays a description of itself to the screen. If the name you typed was not null and was not <TT>help</TT>, the program prints a welcome message.</P>

<P>A new command called <TT>chop</TT> is introduced in this example. The <TT>chop</TT> command removes the last character from the argument that is passed to it. You need to do this in the example because the last character of the entered name is a newline character. If you don&#146;t remove the newline character, your last <TT>print</TT> command prints the following output if you, for example, type in the name <TT>John</TT> on the command line:</P>

<!-- CODE SNIP //-->

<PRE>

Hello John

, welcome to Perl.

</PRE>

<!-- END CODE SNIP //-->

<H4 ALIGN="LEFT"><A NAME="Heading13"></A><FONT COLOR="#000077">unless Statements</FONT></H4>

<P>The Perl <TT>unless</TT> statement is the opposite of the Perl <TT>if</TT> statement. It evaluates an expression, and if the expression returns a value of False, it executes its statement block. The syntax of the <TT>unless</TT> statement is</P>

<!-- CODE SNIP //-->

<PRE>

unless (expression) &#123;

        statements;

&#125;

</PRE>

<!-- END CODE SNIP //-->

<P>Any statement you can write with an <TT>unless</TT> statement can also be written as an <TT>if</TT> statement. The only difference is that you have to negate the expression when you rewrite it as an <TT>if</TT>. For this reason, the <TT>unless</TT> statement is not used very often. The following code illustrates a possible use of an <TT>unless</TT> statement.</P>

<!-- CODE SNIP //-->

<PRE>

print &#147;Please enter your account number.\n&#148;

&#36;account = &lt;STDIN&gt;

unless (&#36;account &lt; 1000 ) &#123;

        print &#147;Since you are a preferred customer you will\n&#148;

        print &#147;get a 10% discount\n&#148;

&#125;

</PRE>

<!-- END CODE SNIP //-->

<P>This program asks for your account number, and if the account number is greater than or equal to 1000, it displays the message indicating that you are entitled to a 10% discount. If your account number is less than 1000, the program continues processing after the end of the <TT>unless</TT> statement.</P><P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="510-513.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="516-518.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>





</td>
</tr>
</table>

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

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