📄 519-521.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=519-521//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="516-518.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="521-522.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading17"></A><FONT COLOR="#000077">until Statements</FONT></H4>
<P>The <TT>until</TT> statement is very similar to the <TT>while</TT> statement. The difference is that the <TT>until</TT> statement executes its statement block only until its conditional expression becomes True, and the <TT>while</TT> statement executes its statement block while its conditional expression is True. The following program contains an example of the <TT>until</TT> statement:</P>
<!-- CODE SNIP //-->
<PRE>
$i = 0;
until ($i > 10) {
print “$i “;
$i = $i + 1;
}
</PRE>
<!-- END CODE SNIP //-->
<P>This statement continues to execute and print the value of <TT>$i</TT> until <TT>$i</TT> is larger than 10. The output from this command is</P>
<!-- CODE SNIP //-->
<PRE>
0 1 2 3 4 5 6 7 8 9 10
</PRE>
<!-- END CODE SNIP //-->
<P>Any <TT>until</TT> statement can also be written as a <TT>while</TT> statement. For this reason, the <TT>until</TT> statement is not used very often. The example shown for the <TT>until</TT> statement can be rewritten as a <TT>while</TT> statement, as shown by the following code:</P>
<!-- CODE SNIP //-->
<PRE>
$i = 0;
while ($i <= 10 ) {
print “$i “;
$i = $i + 1;
}
</PRE>
<!-- END CODE SNIP //-->
<H3><A NAME="Heading18"></A><FONT COLOR="#000077">Functions</FONT></H3>
<P>You have already seen some of the built-in Perl functions, such as <TT>print</TT>, but you have not seen how to create your own functions. Perl refers to functions that you create as subroutines. The syntax for creating a subroutine is</P>
<!-- CODE SNIP //-->
<PRE>
sub subroutine_name {
statements;
}
</PRE>
<!-- END CODE SNIP //-->
<P>The <TT>subroutine_name</TT> is a name you provide and is the name you use when calling the subroutine from your Perl code. The statements in the statement block are the statements that are executed every time you make a call to your subroutine.</P>
<P>To familiarize yourself with the syntax for creating a function, take a look at the following function which multiplies the values that are stored in the variables <TT>$num1</TT> and <TT>$num2</TT> each time it is called.</P>
<!-- CODE SNIP //-->
<PRE>
sub mult {
print “The result of $num1 * $num2 is “,$num1 * $num2, “\n”;
}
</PRE>
<!-- END CODE SNIP //-->
<P>To invoke this function, you must precede the name of the function with an ampersand (&). For example, the following program assigns values to the <TT>$num1</TT> and <TT>$num2</TT> variables and invokes the <TT>mult</TT> function.</P>
<!-- CODE SNIP //-->
<PRE>
$num1 = 5;
$num2 = 10;
&mult;
</PRE>
<!-- END CODE SNIP //-->
<P>Running this program results in the following output being displayed on the screen:
</P>
<!-- CODE SNIP //-->
<PRE>
The result of 5 * 10 is 50
</PRE>
<!-- END CODE SNIP //-->
<P>If this were all functions could do, they would not be incredibly useful. The real power of functions doesn’t come into play until you start defining them so that they can accept arguments and return values.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading19"></A><FONT COLOR="#000077">Passing Arguments to Functions</FONT></H4>
<P>Passing arguments to functions increases their usefulness a great deal. This is because you do not have to know the names of the variables used by the function if they are being passed in as arguments. If you rewrite the <TT>mult</TT> function so that it accepts arguments, you can use it to multiply any two numbers together instead of just the numbers stored in the <TT>$num1</TT> and <TT>$num2</TT> variables. The following is the <TT>mult</TT> function after it has been rewritten to use arguments.</P>
<!-- CODE SNIP //-->
<PRE>
sub mult2 {
print “The result of $_[0] * $_[1] is “, $_[0] * $_[1], “\n”;
}
</PRE>
<!-- END CODE SNIP //-->
<P>The variables <TT>$_[0]</TT> and <TT>$_[1]</TT>, which are the first and second elements of the <TT>@_</TT> array, are used to store the values of the first two arguments passed to the function. The following line of code uses the new <TT>mult2</TT> function to multiply two numbers together:</P>
<!-- CODE SNIP //-->
<PRE>
&mult2(14,20);
</PRE>
<!-- END CODE SNIP //-->
<P>When this command is executed, the first argument passes to <TT>mult2</TT> (14) and is put into the first element of the <TT>@_</TT> array; the second argument (20) is put into the second element of the <TT>@_</TT> array. The <TT>mult2</TT> function prints the following message on the screen:</P>
<!-- CODE SNIP //-->
<PRE>
The result of 14 * 20 is 280
</PRE>
<!-- END CODE SNIP //-->
<H4 ALIGN="LEFT"><A NAME="Heading20"></A><FONT COLOR="#000077">Using Return Values</FONT></H4>
<P>What happens if you do not want to print the results of the <TT>mult2</TT> function to the screen but instead want to use the result in your program? Perl handles this problem by using a return value. Every function you write in Perl has a return value. By default, the return value is equal to the result of the last statement evaluated in the function. The <TT>mult2</TT> function returns the result of the Perl <TT>print</TT> function, which, if everything works properly, is equal to 1. You can rewrite the <TT>mult</TT> function once again so that it returns the result of the multiplication.</P>
<!-- CODE SNIP //-->
<PRE>
sub mult3 {
$_[0] * $_[1];
}
</PRE>
<!-- END CODE SNIP //-->
<P>The following code uses the <TT>mult3</TT> function to find the results of a multiplication:</P>
<!-- CODE SNIP //-->
<PRE>
$num1 = 5;
$num2 = 10;
$result = &mult3($num1 $num2);
print “The answer is $result\n”
</PRE>
<!-- END CODE SNIP //-->
<P>When this program is executed, the result of the multiplication performed by the <TT>mult3</TT> function is returned and stored in the <TT>result</TT> variable. The program then prints the result to the screen.</P>
<H3><A NAME="Heading21"></A><FONT COLOR="#000077">Perl Operators</FONT></H3>
<P>You have seen quite a few Perl operators in the examples in this chapter. The <TT>print</TT> command that’s been used throughout this chapter is one example of a Perl operator. You also saw the Perl operators <TT>keys</TT>, <TT>values</TT>, <TT>each</TT>, and <TT>delete</TT> which are used to perform operations on associative arrays. Perl actually provides an unbelievable number of operators. To get a good idea of what operators are available or to get a complete listing of the Perl operators, refer to the Perl manual page.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="516-518.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="521-522.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 + -