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

📄 ch6.htm

📁 this is a book on pearl , simple example with explanation is given here. it could be beneficial for
💻 HTM
📖 第 1 页 / 共 3 页
字号:
Start the statement block.<BR>Create a local version of </I><TT><I>$firstVar</I></TT><I>with a value of </I><TT><I>A</I></TT><I>.<BR>Print </I><TT><I>$firstVar</I></TT><I>repeated five times.<BR>End the statement block.<BR>Print the global </I><TT><I>$firstVar</I></TT><I>.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>$firstVar = 10;{   my($firstVar) = &quot;A&quot;;   print $firstVar x 5 . &quot;\n&quot;;}print(&quot;firstVar = $firstVar\n&quot;);</PRE></BLOCKQUOTE><P>This program displays:<BLOCKQUOTE><PRE>AAAAAfirstVar = 10</PRE></BLOCKQUOTE><P>You can see that the value of <TT>$firstVar</TT>has been uNChanged by the statement block even though a variablecalled <TT>$firstVar</TT> is usedinside it. This shows that the variable used inside the statementblock does indeed have a local scope.<BR><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Tip</B></TD></TR><TR><TD><BLOCKQUOTE>Statement blocks are also good to use when you temporarily need to send debugging output to a file. Then, when all the bugs have been found and the need for debugging is over, you can remove the statement block quickly and easily because all the code is in one spot.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H2><A NAME="StatementTypes"><FONT SIZE=5 COLOR=#FF0000>Statement Types</FONT></A></H2><P>Just as there were several types of expressions, there are alsoseveral types of statements. Table 6.5 lists seven different typesof statements.<BR><P><CENTER><B>Table 6.5&nbsp;&nbsp;Perl Statement Types</B></CENTER><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD WIDTH=183><I>Statement Type</I></TD><TD WIDTH=407><I>Description</I></TD></TR><TR><TD WIDTH=183>No-action statements</TD><TD WIDTH=407>These statements evaluate a value but perform no actions.</TD></TR><TR><TD WIDTH=183>Action statements</TD><TD WIDTH=407>These statements perform some action.</TD></TR><TR><TD WIDTH=183>Assignment statements</TD><TD WIDTH=407>These statements assign a value to one or more variables. They are discussed, along with the assignment operator, in <A HREF="ch4.htm" >Chapter 4</A> &quot;Operators.&quot;</TD></TR><TR><TD WIDTH=183>Decision statements</TD><TD WIDTH=407>These statements allow you to test a condition and choose among one or more actions. Decision statements are discussed in <A HREF="ch7.htm" >Chapter 7</A> &quot;Control Statements.&quot;</TD></TR><TR><TD WIDTH=183>Jump statements</TD><TD WIDTH=407>These statements let you uNConditionally change the program flow to another point in your code. For instaNCe, you could use the <TT>redo</TT> keyword to send your program flow back to the beginning of a statement block. Jump statements are discussed in <A HREF="ch7.htm" >Chapter 7</A> &quot;Control Statements.&quot;</TD></TR><TR><TD WIDTH=183>Loop statements</TD><TD WIDTH=407>These statements let you perform a series of statements repeatedly while some condition is true or until some condition is true. Loop statements are discussed in <A HREF="ch7.htm" >Chapter 7</A> &quot;Control Statements.&quot;</TD></TR><TR><TD WIDTH=183>Modified Statements</TD><TD WIDTH=407>These statements let you use the <TT>if</TT>, <TT>unless</TT>, <TT>until</TT>, and <TT>while</TT> keywords to change the behavior of a statement.</TD></TR></TABLE></CENTER><P><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Note</B></TD></TR><TR><TD><BLOCKQUOTE>A <I>keyword</I> is a word that is reserved for use by Perl. These words (<TT>if</TT>, <TT>elsif</TT>, <TT>else</TT>, <TT>while</TT>, <TT>unless</TT>, <TT>until</TT>, <TT>for</TT>, <TT>foreach</TT>, <TT>last</TT>, <TT>next</TT>, <TT>redo</TT>, and <TT>continue</TT>) are integral to the language and provide you with the ability to control program flow.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P><I>No-action statements</I> are evaluated by Perl and have a valuebut perform no actions. For instaNCe, the Perl statement <TT>10+ 20</TT>; has a value of 30, but because no variableswere changed, no work was done. The value of 20 is not storedanywhere, and it is quickly forgotten when the next statementis seen.<P>What good is a <I>no-action statement </I>if no work is done?A lot of Perl programmers use these simple statements as returnvalues in fuNCtions. For instaNCe:<BLOCKQUOTE><PRE>sub firstSub {    doSomething();    condition == true ? &quot;Success&quot; : &quot;Failure&quot;;}</PRE></BLOCKQUOTE><P>Because Perl returns the value of the last evaluated statementwhen leaving a fuNCtion, you can use no-action statements to letPerl know what value should be returned to the main program. Noticethat even though the ternary operator was used, because thereare no fuNCtion calls or unary operators, no work can be done.<BR><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Note</B></TD></TR><TR><TD><BLOCKQUOTE>I still like to use the <TT>return()</TT> fuNCtion to explicitly identify the return values. The previous example looks like this when using the <TT>return()</TT> fuNCtion:</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><BLOCKQUOTE><PRE>sub firstSub {    doSomething();    return(condition == true ? &quot;Success&quot; : &quot;Failure&quot;);}</PRE></BLOCKQUOTE><P><I>Action statements</I> use expressions to perform some task.They can iNCrement or decrement a variable and call a fuNCtion.<P><I>Modified statements</I> use expressions in conjuNCtion witha modifying keyword to perform some action. There are four modifyingkeywords: <TT>if</TT>, <TT>unless</TT>,<TT>until</TT>, and <TT>while</TT>.The basic syntax of a modified statement is<BLOCKQUOTE><PRE>EXPRESSION modifier (CONDITION);</PRE></BLOCKQUOTE><P>Let's look at some examples of modified statements.<H3><A NAME="ExampleUsingtheifModifier">Example: Using the if Modifier</A></H3><P>The <TT>if</TT> modifier tells Perlthat the expression should be evaluated only if a given conditionis true. The basic syntax of a modified statement with the <TT>if</TT>modifier is<BLOCKQUOTE><PRE>EXPRESSION if (CONDITION);</PRE></BLOCKQUOTE><P>This is a compact way of saying<BLOCKQUOTE><PRE>if (CONDITION) {    EXPRESSION;}</PRE></BLOCKQUOTE><P>Let's prove that the <TT>if</TT> modifierworks. Here's an example showing that the <TT>if</TT>modifier can prevent the evaluation of an expression.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Initialize the </I><TT><I>$firstVar</I></TT><I>and </I><TT><I>$secondVar</I></TT><I>variables to 20.<BR>INCrement </I><TT><I>$firstVar</I></TT><I>if and only if </I><TT><I>$secondVar</I></TT><I>is equal to 10.<BR>Print the values of </I><TT><I>$firstVar</I></TT><I>and </I><TT><I>$secondVar</I></TT><I>.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>$firstVar  = 20;$secondVar = 20;$firstVar++ if ($secondVar == 10);print(&quot;firstVar  = $firstVar\n&quot;);print(&quot;secondVar = $secondVar\n&quot;);</PRE></BLOCKQUOTE><P>This program prints:<BLOCKQUOTE><PRE>firstVar  = 20secondVar = 20</PRE></BLOCKQUOTE><P>The program doesn't iNCrement <TT>$firstVar</TT>because the value of <TT>$secondVar</TT>is 20 at the time the condition is evaluated. If you changed the10 to a 20 in the condition, Perl would iNCrement <TT>$firstVar</TT>.<P>You can find out about the <TT>if</TT>statement-as opposed to the <TT>if</TT>modifier-in <A HREF="ch7.htm" >Chapter 7</A> &quot;Control Statements.&quot;<BR><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Note</B></TD></TR><TR><TD><BLOCKQUOTE>The condition expression can be as complex as you'd like. However, I believe that one of the goals of statement modifiers is to make programs easier to read and understand. Therefore, I use modifiers only with simple conditions. If complex conditions need to be met before an expression should be evaluated, using the <TT>if</TT> keyword is probably a better idea.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H3><A NAME="ExampleUsingtheunlessModifier">Example: Using the unless Modifier</A></H3><P>The <TT>unless</TT> modifier is theopposite of the <TT>if</TT> modifier.This modifier evaluates an expression unless a condition is true.The basic syntax of a modified statement with the <TT>unless</TT>modifier is<BLOCKQUOTE><PRE>EXPRESSION unless (CONDITION);</PRE></BLOCKQUOTE><P>This is a compact way of saying<BLOCKQUOTE><PRE>if (! CONDITION) {    EXPRESSION;}</PRE></BLOCKQUOTE><P>This modifier helps to keep program code clearly understandablebecause you don't have to use the logical <TT>not</TT>operator to change the value of a condition so you can evaluatean expression. Let's look back at the example from a moment ago.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Initialize the </I><TT><I>$firstVar</I></TT><I>and </I><TT><I>$secondVar</I></TT><I>variables to 20.<BR>INCrement </I><TT><I>$firstVar</I></TT><I>unless </I><TT><I>$secondVar</I></TT><I>is equal to 10.<BR>Print the values of </I><TT><I>$firstVar</I></TT><I>and </I><TT><I>$secondVar</I></TT><I>.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>$firstVar  = 20;$secondVar = 20;$firstVar++ unless ($secondVar == 10);print(&quot;firstVar  = $firstVar\n&quot;);print(&quot;secondVar = $secondVar\n&quot;);</PRE></BLOCKQUOTE><P>This program prints:<BLOCKQUOTE><PRE>firstVar  = 21secondVar = 20</PRE></BLOCKQUOTE><P>If you were limited to using only the <TT>if</TT>modifier, the modified statement would read<BLOCKQUOTE><PRE>$firstVar++ if ($secondVar != 10);</PRE></BLOCKQUOTE><P>The <TT>unless</TT> modifier is moredirect. All things being equal, the coNCept of <TT>$secondVar</TT>being equal to 10 is easier to grasp than the coNCept of <TT>$secondVar</TT>not being equal to 10. Of course, this is a trivial example. Let'slook at something more substantial before we move on.<P>One of the drawbacks of associative arrays is that they quietlyredefine the value of any key when that key is assigned a newvalue, thereby losing the old value. If you are reading from alist of key-value pairs, this might not be the behavior you need.The <TT>unless</TT> modifier can beused to prevent element assignment if the key has already been

⌨️ 快捷键说明

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