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

📄 505-507.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=505-507//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="503-505.html">Previous</A></TD>

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

<TD><A HREF="507-510.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<H3><A NAME="Heading4"></A><FONT COLOR="#000077">Handling Data in Perl</FONT></H3>

<P>At the simplest level, Perl deals with two different kinds of data: numbers and strings. This section describes how Perl handles these two forms of data and also how each can be used in Perl programs. Let&#146;s start things off with a description of how variables are used in Perl programs.

</P>

<H4 ALIGN="LEFT"><A NAME="Heading5"></A><FONT COLOR="#000077">Variables</FONT></H4>

<P>Variables in Perl are similar in function and syntax to variables found in the shell programming languages. The biggest difference is that in the shell languages you get the value of a variable by preceding it with a dollar sign, and you assign a value to a variable by using the variable name without a dollar sign in front of it.

</P>

<P>In Perl, you <I>always</I> use a dollar sign in front of the variable name no matter whether you are assigning a value to the variable or getting the value of the variable. The following command assigns the value <TT>hello</TT> to the variable named <TT>greeting</TT>.</P>

<!-- CODE SNIP //-->

<PRE>

&#36;greeting=&#148;hello&#148;;

</PRE>

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

<P>In the shell languages, this command is written as follows:

</P>

<!-- CODE SNIP //-->

<PRE>

greeting=&#148;hello&#148;

</PRE>

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

<P>or

</P>

<!-- CODE SNIP //-->

<PRE>

set greeting = &#147;hello&#148; (when using tcsh)

</PRE>

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

<P>Spaces on either side of the equal sign don&#146;t matter to Perl (unlike the shell programming language) so you can use whichever method is most comfortable and familiar to you. Another difference is that you can perform integer operations directly on a variable without using another command like <TT>expr</TT>. For example, the command</P>

<!-- CODE SNIP //-->

<PRE>

&#36;a = 1 &#43; 2;

</PRE>

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

<P>assigns the value of <TT>3</TT> to the variable <TT>&#36;a</TT>. If you enter the following two commands</P>

<!-- CODE SNIP //-->

<PRE>

&#36;a = 1 &#43; 2;

&#36;b = 3 * &#36;a;

</PRE>

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

<P>the <TT>&#36;b</TT> variable is assigned a value of <TT>9</TT>.</P>

<P>This is the expected result, but how does Perl know that <TT>&#36;a</TT> is a numeric value and not a string? The answer is that it does not know. Whenever a variable or literal is used as an argument to a numeric operator, it is converted to a number. In this case, there is no problem because the value stored in <TT>&#36;a</TT> is a number. There would be a problem, however, if the value stored in <TT>&#36;a</TT> was a string that contained a character such as <TT>b</TT> because this type of string cannot be converted to a numeric value. Perl handles this situation in a not so elegant way. If the value is not convertible to a number, it simply makes it equal to 0. By default, Perl does not even warn you about this conversion. If you invoke Perl with a <TT>-w</TT> command-line option, though, it will warn you when this type of conversion takes place.</P>

<H4 ALIGN="LEFT"><A NAME="Heading6"></A><FONT COLOR="#000077">Numbers</FONT></H4>

<P>Perl stores all numeric data as floating-point values. You can use integers in your Perl programs, but Perl treats them all as floating-point numbers.

</P>

<P>Perl provides a set of operators that can be used to perform comparisons between two numeric values and for performing the standard mathematical operations on numbers. Table 28.1 lists some of the operators that Perl provides for numeric data.</P>

<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 28.1.</B> Numeric operators.

<TR>

<TH COLSPAN="2"><HR>

<TR>

<TH WIDTH="35%" ALIGN="LEFT">Operator

<TH WIDTH="65%" ALIGN="LEFT">Description

<TR>

<TH COLSPAN="2"><HR>

<TR>

<TD><TT>&#36;a = op1 &#43; op2</TT>

<TD>Stores the value of <TT>op1</TT> plus <TT>op2</TT> into <TT>&#36;a</TT>

<TR>

<TD><TT>&#36;a = op1 - op2</TT>

<TD>Stores the value of <TT>op1</TT> minus <TT>op2</TT> into <TT>&#36;a</TT>

<TR>

<TD><TT>&#36;a = op1 * op2</TT>

<TD>Stores the value of <TT>op1</TT> times <TT>op2</TT> into <TT>&#36;a</TT>

<TR>

<TD><TT>&#36;a = op1 / op2</TT>

<TD>Stores the value of <TT>op1</TT> divided by <TT>op2</TT> into <TT>&#36;a</TT>

<TR>

<TD><TT>&#36;a = op1 ** op2</TT>

<TD>Stores the value of <TT>op1</TT> to the power of <TT>op2</TT> into <TT>&#36;a</TT>

<TR>

<TD><TT>&#36;a = op1 % op2</TT>

<TD>Stores the value of <TT>op1</TT> modulus <TT>op2</TT> into <TT>&#36;a</TT>

<TR>

<TD><TT>op1 == op2</TT>

<TD>Returns True if <TT>op1</TT> is equal to <TT>op2</TT>

<TR>

<TD><TT>op1 != op2</TT>

<TD>Returns True if <TT>op1</TT> is not equal to <TT>op2</TT>

<TR>

<TD><TT>op1 &lt; op2</TT>

<TD>Returns True if <TT>op1</TT> is less than <TT>op2</TT>

<TR>

<TD><TT>op1 &gt; op2</TT>

<TD>Returns True if <TT>op1</TT> is greater than <TT>op2</TT>

<TR>

<TD><TT>op1 &lt;= op2</TT>

<TD>Returns True if <TT>op1</TT> is less than or equal to <TT>op2</TT>

<TR>

<TD><TT>op1 &gt;= op2</TT>

<TD>Returns True if <TT>op1</TT> is greater than or equal to <TT>op2</TT>

<TR>

<TD COLSPAN="2"><HR>

</TABLE>

<P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="503-505.html">Previous</A></TD>

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

<TD><A HREF="507-510.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 + -