📄 ch6.htm
字号:
used. Listing 6.1 shows the <TT>unless</TT>
modifier used in a program.
<P>
<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT><p>
<BLOCKQUOTE>
<I>Call the </I><TT><I>assignElement()</I></TT><I>
fuNCtion to create two elements in the </I><TT><I>@array</I></TT><I>
associative array.<BR>
Call the </I><TT><I>printArray()</I></TT><I>
fuNCtion.<BR>
Try to redefine the value associated with the "A" key
by calling </I><TT><I>assignElement()</I></TT><I>.
<BR>
Print the array again to verify that no elements have changed.</I>
</BLOCKQUOTE>
<HR>
<BLOCKQUOTE>
<B>Listing 6.1 06LST01.PL-Using the <FONT FACE="BI Helvetica BoldOblique">unless</FONT>
Modifier to Control Array Element Assignmen</B>t<BR>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
assignElement("A", "AAAA");
assignElement("B", "BBBB");
printArray();
assignElement("A", "ZZZZ");
printArray();
sub assignElement {
my($key, $value) = @_;
$array{$key} = $value unless defined($array{$key});
}
sub printArray {
while (($key, $value) = each(%array)) {
print("$key = $value\n");
}
print("\n");
}
</PRE>
</BLOCKQUOTE>
<HR>
<P>
This program displays:
<BLOCKQUOTE>
<PRE>
A = AAAA
B = BBBB
A = AAAA
B = BBBB
</PRE>
</BLOCKQUOTE>
<P>
These lines of code should look a little familiar to you. The
<TT>while</TT> loop in the <TT>printArray()</TT>
fuNCtion was used in a <A HREF="ch5.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch5.htm" >Chapter 5</A>example. The <TT>assignElement()</TT>
fuNCtion will make an assignment unless a key-value pair with
the same key already exists. In that case, the assignment statement
is bypassed.
<H3><A NAME="ExampleUsingtheuntilModifier">
Example: Using the until Modifier</A></H3>
<P>
The <TT>until</TT> modifier is a little
more complex than the <TT>if</TT>
or <TT>unless</TT> modifiers. It repeatedly
evaluates the expression until the condition becomes true. The
basic syntax of a modified statement with the <TT>until</TT>
modifier is
<BLOCKQUOTE>
<PRE>
EXPRESSION until (CONDITION);
</PRE>
</BLOCKQUOTE>
<P>
This is a compact way of saying
<BLOCKQUOTE>
<PRE>
until (CONDITION) {
EXPRESSION;
}
</PRE>
</BLOCKQUOTE>
<P>
The expression is evaluated only while the condition is false.
If the condition is true when the statement is eNCountered, the
expression will never be evaluated. The following example proves
this:
<P>
<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT><p>
<BLOCKQUOTE>
<I>Initialize </I><TT><I>$firstVar</I></TT><I>
to 10.<BR>
Repeatedly evaluate </I><TT><I>$firstVar++</I></TT><I>
until the condition </I><TT><I>$firstVar
> 2</I></TT><I> is true.<BR>
Print the value of </I><TT><I>$firstVar</I></TT><I>.</I>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
$firstVar = 10;
$firstVar++ until ($firstVar > 2);
print("firstVar = $firstVar\n");
</PRE>
</BLOCKQUOTE>
<P>
This program displays:
<BLOCKQUOTE>
<PRE>
firstVar = 10
</PRE>
</BLOCKQUOTE>
<P>
This shows that the expression <TT>$firstVar++</TT>
was never executed because the condition was true the first time
it was evaluated. If it had been executed, the value of <TT>$firstVar</TT>
would have been 11 when printed. In this case, the <TT>until</TT>
modifier worked exactly like the <TT>unless</TT>
modifier.
<P>
However, when the condition is false for the first evaluation,
Perl executes the expression repeatedly until the condition is
true. Here is an example:
<P>
<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT><p>
<BLOCKQUOTE>
<I>Initialize </I><TT><I>$firstVar</I></TT><I>
to 10.<BR>
Repeatedly evaluate </I><TT><I>$firstVar++</I></TT><I>
until the condition </I><TT><I>$firstVar
> 20</I></TT><I> is true.<BR>
Print the value of </I><TT><I>$firstVar</I></TT><I>.</I>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
$firstVar = 10;
$firstVar++ until ($firstVar > 20);
print("firstVar = $firstVar\n");
</PRE>
</BLOCKQUOTE>
<P>
This program displays:
<BLOCKQUOTE>
<PRE>
firstVar = 21
</PRE>
</BLOCKQUOTE>
<P>
In this case, the <TT>$firstVar++</TT>
expression is executed 11 times. Each execution of the expression
iNCrements the value of <TT>$firstVar</TT>.
When <TT>$firstVar</TT> is equal to
21, the statement ends because 21 is greater than 20, which means
that the condition is true.
<P>
You can find out about the <TT>until</TT>
statement-as opposed to the <TT>until</TT>
modifier-in <A HREF="ch7.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch7.htm" >Chapter 7</A> "Control Statements."
<H3><A NAME="ExampleUsingthewhileModifier">
Example: Using the while Modifier</A></H3>
<P>
The <TT>while</TT> modifier is the
opposite of the <TT>until</TT> modifier.
It repeatedly evaluates the expression while the condition is
true. When the condition becomes false, the statement ends. The
basic syntax of a modified statement with the <TT>while</TT>
modifier is
<BLOCKQUOTE>
<PRE>
EXPRESSION while (CONDITION);
</PRE>
</BLOCKQUOTE>
<P>
This is a compact way of saying
<BLOCKQUOTE>
<PRE>
while (CONDITION) {
EXPRESSION;
}
</PRE>
</BLOCKQUOTE>
<P>
The expression is evaluated only while the condition is true.
If the condition is false when the statement is eNCountered, the
expression will never be evaluated. Here is an example using the
<TT>while</TT> modifier.
<P>
<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT><p>
<BLOCKQUOTE>
<I>Initialize </I><TT><I>$firstVar</I></TT><I>
to 10.<BR>
Repeatedly evaluate </I><TT><I>$firstVar++</I></TT><I>
while the condition </I><TT><I>$firstVar
< 20</I></TT><I> is true.<BR>
Print the value of </I><TT><I>$firstVar</I></TT><I>.</I>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
$firstVar = 10;
$firstVar++ while ($firstVar < 20);
print("firstVar = $firstVar\n");
</PRE>
</BLOCKQUOTE>
<P>
This program displays:
<BLOCKQUOTE>
<PRE>
firstVar = 21
</PRE>
</BLOCKQUOTE>
<P>
You can compare this example directly to the last example given
for the <TT>until</TT> modifier. Because
the <TT>until</TT> modifier is the
opposite of the <TT>while</TT> modifier,
the operators in the conditions are also opposite in nature.
<P>
You can find out about the <TT>while</TT>
statement-as opposed to the <TT>while</TT>
modifier-in <A HREF="ch7.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch7.htm" >Chapter 7</A> "Control Statements."
<H2><A NAME="Summary"><FONT SIZE=5 COLOR=#FF0000>
Summary</FONT></A></H2>
<P>
This chapter discussed Perl statements and how they are built
from expressions. You read about four types of expressions: simple,
simple with side effects, simple with operators, and complex.
<P>
Next, you read about statement blocks. These program constructs
are good to logically isolate one block of statements from the
main program flow. You can also use statement blocks and the <TT>my()</TT>
fuNCtion to create local variables. This is mainly done for debugging
reasons or to make small program changes that are guaranteed not
to affect other portions of the program.
<P>
Then, seven types of statements were mentioned: no-action, action,
assignment, decision, jump, loop, and modified. This chapter described
no-action, action, and modified statements. Assignment statements
were mentioned in <A HREF="ch3.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch3.htm" >Chapter 3</A>"Variables" and again in
<A HREF="ch4.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch4.htm" >Chapter 4</A> "Operators." Decision, jump, and loop statements
are covered in <A HREF="ch7.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch7.htm" >Chapter 7</A> "Control Statements."
<P>
Modified statements use the <TT>if</TT>,
<TT>unless</TT>, <TT>until</TT>,
and <TT>while</TT> keywords to affect
the evaluation of an expression. The <TT>if</TT>
keyword evaluates an expression if a given condition is true.
The <TT>unless</TT> keyword does the
opposite: the expression is evaluated if a given condition is
false. The <TT>until</TT> keyword
repeatedly evaluates an expression until the condition is true.
The <TT>while</TT> keyword is the
opposite of until so that it repeatedly evaluates an expression
until the condition is false.
<P>
The next chapter, "Control Statements," explores the
<TT>decision</TT>, <TT>jump</TT>,
and <TT>loop</TT> statements in detail.
<H2><A NAME="ReviewQuestions"><FONT SIZE=5 COLOR=#FF0000>
Review Questions</FONT></A></H2>
<P>
Answers to Review Questions are in Appendix A.
<OL>
<LI>What is an expression?
<LI>What is a statement?
<LI>What are the four statement modifiers?
<LI>What are two uses for statement blocks?
<LI>What can non-action statements be used for?
<LI>How is the <TT>if</TT> modifier
different from the <TT>unless</TT>
modifier?
<LI>What will the following code display?
</OL>
<BLOCKQUOTE>
<PRE>
$firstVar = 10;
$secondVar = 20;
$firstVar += $secondVar++ if ($firstVar > 10);
print("firstVar = $firstVar\n");
print("firstVar = $secondVar\n");
</PRE>
</BLOCKQUOTE>
<H2><A NAME="ReviewExercises"><FONT SIZE=5 COLOR=#FF0000>
Review Exercises</FONT></A></H2>
<OL>
<LI>Write a simple expression that uses the exponentiation operator.
<LI>Write a complex expression that uses three operators and one
fuNCtion.
<LI>Write a Perl program that uses a statement block inside a
fuNCtion call.
<LI>Use the statement block from the previous exercise to create
local variables.
<LI>Write a Perl program that shows if the expression clause of
a <TT>while</TT> modified statement
will be evaluated when the condition is false.
</OL>
<HR>
<CENTER><P><A HREF="ch5.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch5.htm"><IMG SRC="pc.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A>
<A HREF="#CONTENTS"><IMG SRC="cc.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/cc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A>
<A HREF="index-1.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/index-1.htm"><IMG SRC="hb.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/hb.gif" BORDER=0 HEIGHT=88 WIDTH=140></A>
<A HREF="ch7.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch7.htm"><IMG SRC="nc.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/nc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A>
<HR WIDTH="100%"></P></CENTER>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -