unx16.htm
来自「Unix Unleashed, Third Edition is written」· HTM 代码 · 共 2,814 行 · 第 1/5 页
HTM
2,814 行
versa. For example, consider the program in file LIST 16_2 on the CD-ROM, which converts distances from miles to kilometers and vice versa. In this example, the scalar variable $originaldist contains the character string read in from the standard input
file. The contents of this string are then treated as a number, multiplied by the miles-to-kilometers and kilometers-to-miles conversion factors, and stored in $miles and $kilometers.
<BR></P>
<P>This program also contains a call to the function chop(). This function throws away the last character in the specified string. In this case, chop() gets rid of the newline character at the end of the input line.
<BR></P>
<P>If a string contains characters that are not digits, it is converted to 0:
<BR></P>
<PRE># this assigns 0 to $a, since "hello" becomes 0
$a = "hello" * 5;</PRE>
<P>In cases like this, Perl does not tell you that anything has gone wrong, and your results may not be what you expect.
<BR></P>
<P>Also, strings containing misprints yield unexpected results:
<BR></P>
<PRE>$a = "12O34"+1 # the letter O, not the number 0</PRE>
<P>When Perl sees a string in the middle of an expression, it converts the string to an integer. To do this, it starts at the left of the string and continues until it sees a letter that is not a digit. In this case, "12O34" is converted to the
integer 12, not 12034.
<BR></P>
<H4 ALIGN="CENTER">
<CENTER><A ID="I21" NAME="I21">
<FONT SIZE=3><B>Using Scalar Variable Operators</B>
<BR></FONT></A></CENTER></H4>
<P>The statement $miles = $originaldist * 0.6214; uses two scalar variable operators: =, the assignment operator, which assigns a value to a variable, and *, the multiplication operator, which multiplies two values.
<BR></P>
<P>Perl provides the complete set of operators found in C, plus a few others. These operators are described in the following sections.
<BR></P>
<H5 ALIGN="CENTER">
<CENTER><A ID="I22" NAME="I22">
<FONT SIZE=3><B>Performing Arithmetic</B>
<BR></FONT></A></CENTER></H5>
<P>To do arithmetic in Perl, use the arithmetic operators.
<BR></P>
<P>Perl supports the following arithmetic operators:
<BR></P>
<PRE>$a = 15; # assignment: $a now has the value 15
$a = 4 + 5.1; # addition: $a is now 9.1
$a = 17 - 6.2; # subtraction: $a is now 10.8
$a = 2.1 * 6; # multiplication: $a is now 12.6
$a = 48 / 1.5; # division: $a is now 32
$a = 2 ** 3; # exponentiation: $a is now 8
$a = 21 % 5; # remainder (modulo): $a is now 1
$a = - $b; # arithmetic negation: $a is now $b * -1</PRE>
<P>Non-integral values are converted to integers before a remainder operation is performed:
<BR></P>
<PRE>$a = 21.4 % 5.1; # identical to 21 % 5</PRE>
<H5 ALIGN="CENTER">
<CENTER><A ID="I23" NAME="I23">
<FONT SIZE=3><B>Performing Comparisons</B>
<BR></FONT></A></CENTER></H5>
<P>To compare two scalar values in Perl, use the logical operators.
<BR></P>
<P>Logical operators are divided into two classes: numeric and string. The following numeric logical operators are defined:
<BR></P>
<PRE>11.0 < 16 # less than
16 > 11 # greater than
15 == 15 # equals
11.0 <= 16 # less than or equal to
16 >= 11 # greater than or equal to
15 != 14 # not equal to
$a || $b # logical OR: true if either is non-zero
$a && $b # logical AND: true only if both are non-zero
! $a # logical NOT: true if $a is zero</PRE>
<P>In each case, the result of the operation performed by a logical operator is non-zero if true and zero if false, just like in C.
<BR></P>
<P>The expression on the left side of a || (logical or) operator is always tested before the expression on the right side, and the expression on the right side is only used when necessary. For example, consider the following expression:
<BR></P>
<PRE>$x == 0 || $y / $x > 5</PRE>
<P>Here, the expression on the left side of the ||, $x == 0, is tested first. If $x is zero, the result is true regardless of the value of $y / $x > 5, so Perl doesn't bother to compute this value. $y / $x > 5 is only evaluated if s is not zero. This
ensures that division by zero can never occur.
<BR></P>
<P>Similarly, the expression on the right side of a && operator is only tested if the expression on the left side is true:
<BR></P>
<PRE>$x != 0 && $y / $x > 5</PRE>
<P>Once again, a division by zero error is impossible, because $y / $x > 5 is only evaluated if $x is non-zero.
<BR></P>
<P>Perl also defines the <=> operator, which returns 0 if the two values are equal, -1 if the left value is larger, and 1 if the right value is larger:
<BR></P>
<PRE>4 <=> 1 # returns -1
3 <=> 3.0 # returns 0
1 <=> 4.0 # returns 1</PRE>
<HR ALIGN=CENTER>
<NOTE>
<IMG SRC="caution.gif" WIDTH = 37 HEIGHT = 35><B>CAUTION: </B>Be careful when you use floating point numbers in comparison operations, because the result may not be what you expect. Consider the following code fragment:
<BR>
<BR>$val1 = 14.3;
<BR>$val2 = 100 + 14.3 - 100;
<BR>print "val1 is $val1, val2 is $val2\n";
<BR>
<BR>On first examination, $val1 and $val2 appear to contain the same value, 14.3. However, the print statement produces the following:
<BR>
<BR>val1 is 14.300000000000001, val2 is 14.299999999999997
<BR>
<BR>Adding and subtracting 100 affects the value stored in $val2 because of the way floating point values are calculated and stored on the machine. As a result, $val1 and $val2 are not the same, and $val1 == $val2 is not true.
<BR>
<BR>This problem occurs in most programming languages (including C).
<BR></NOTE>
<HR ALIGN=CENTER>
<P>Besides the preceding numeric logical operators, Perl also provides logical operators that work with strings:
<BR></P>
<PRE>"aaa" lt "bbb" # less than
"bbb" gt "aaa" # greater than
"aaa" eq "aaa" # equals
"aaa" le "bbb" # less than or equal to
"bbb" ge "aaa" # greater than or equal to
"aaa" ne "bbb" # not equal to</PRE>
<P>Perl also defines the cmp operator, which, like the numeric operator <=>, returns -1, 0 or 1:
<BR></P>
<PRE>"aaa" cmp "bbb" # returns -1
"aaa" cmp "aaa" # returns 0
"bbb" cmp "aaa" # returns 1</PRE>
<P>This behavior is identical to that of the C function strcmp().
<BR></P>
<P>Note that the logical string operators perform string comparisons, not numeric comparisons. For example, "40" lt "8" is true—if the two strings are sorted in ascending order, "40" appears before "8".
<BR></P>
<H5 ALIGN="CENTER">
<CENTER><A ID="I24" NAME="I24">
<FONT SIZE=3><B>Manipulating Bits</B>
<BR></FONT></A></CENTER></H5>
<P>Any integer can always be represented in binary or base-2 notation, of course. For example, the number 38 is equivalent to the binary value 100110: 32 plus 4 plus 2. Each 0 or 1 in this binary value is called a bit.
<BR></P>
<P>If a Perl scalar value happens to be an integer, Perl allows you to manipulate the bits that make up that integer. To do this, use the Perl bitwise operators.
<BR></P>
<P>The following bitwise operators are supported in Perl:
<BR></P>
<UL>
<LI>The & (bitwise AND) operator
<BR>
<BR></LI>
<LI>The | (bitwise OR) operator
<BR>
<BR></LI>
<LI>The ^ (bitwise EXOR, or exclusive OR) operator
<BR>
<BR></LI>
<LI>The ~ (bitwise NOT) operator
<BR>
<BR></LI>
<LI>The << (left shift) and >> (right shift) operators
<BR>
<BR></LI></UL>
<P>If a scalar value is not an integer, it is converted to an integer before a bitwise operation is performed:
<BR></P>
<PRE>$a = 24.5 & 11.2 # identical to $a = 24 & 11</PRE>
<P>The & operator works as follows: first, it examines the values on either side of the &. (These values are also known as the operands of the & operator.) These values are examined in their binary representations. For example, consider the
following bitwise operation:
<BR></P>
<PRE>$a = 29 & 11;</PRE>
<P>In this case, the 29 is converted to 11101, and the 11 is converted to 01011. (A binary representation can have as many leading zeroes as you like.)
<BR></P>
<P>Next, Perl compares each bit of the first operand with the corresponding bit in the second operand:
<BR></P>
<PRE>11101
01011</PRE>
<P>In this case, only the second and fifth bits (from the left) of the two operands are both 1; therefore, the binary representation of the result is 01001, or 9.
<BR></P>
<P>The | operator works in much the same way. The bits of the two operands are compared one at a time; if a bit in the first operand is 1 or its corresponding bit in the second operand is 1, the bit in the result is set to 1. Consider this example:
<BR></P>
<PRE>$a = 25 | 11;</PRE>
<P>Here, the binary representations are 11001 and 01011. In this case, only the third bits are both 0, and the result is 11011 or 27.
<BR></P>
<P>The ^ operator sets a result bit to 1 if exactly one of the corresponding bits in an operand is 1. If both bits are 1 or both are 0, the result bit is set to 0. In the example $a = 25 ^ 11; the binary representations of the operands are 11001 and 01011,
and the result is 10010, or 18.
<BR></P>
<P>The ~ operator works on one operand. Every 0 bit in the operand is changed to a 1 and vice versa. For example, consider the following:
<BR></P>
<PRE>$a = ~ 25;</PRE>
<P>Here, the binary representation of 25 is 11001. The result, therefore, is 00110, or 6.
<BR></P>
<P>The << operator shifts the bits of the left operand the number of places specified by the right operand, and fills the vacated places with zeroes:
<BR></P>
<PRE>$a = 29 << 2;</PRE>
<P>Here the value 29, whose binary representation is 11101, is shifted left two positions. This produces the result 1110100, or 116.
<BR></P>
<P>Similarly, the >> operator shifts the bits rightward, with the rightmost bits being lost:
<BR></P>
<PRE>$a = 29 >> 2;</PRE>
<P>In this case, 29, or 11101, is shifted right two places. The 01 on the end is thrown away, and the result is 111, or 7.
<BR></P>
<P>Shifting left one bit is equivalent to multiplying by 2:
<BR></P>
<PRE>$a = 54 << 1; # this result is 108
$a = 54 * 2; # this result is also 108</PRE>
<P>Shifting right one bit is equivalent to dividing by 2:
<BR></P>
<PRE>$a = 54 >> 1; # this result is 27
$a = 54 / 2; # this result is also 27</PRE>
<P>Similarly, shifting left or right n bits is equivalent to multiplying or dividing by 2**n.
<BR></P>
<H5 ALIGN="CENTER">
<CENTER><A ID="I25" NAME="I25">
<FONT SIZE=3><B>Using the Assignment Operators</B>
<BR></FONT></A></CENTER></H5>
<P>The most common assignment operator is the = operator, which you've already seen:
<BR></P>
<PRE>$a = 9;</PRE>
<P>Here, the value 9 is assigned to the scalar variable $a.
<BR></P>
<P>Another common assignment operator is the += operator, which combines the operations of addition and assignment:
<BR></P>
<PRE>$a = $a + 1; # this adds 1 to $a
$a += 1; # this also adds 1 to $a</PRE>
<P>Other assignment operators exist that correspond to the other arithmetic and bitwise operators:
<BR></P>
<PRE>$a -= 1; # same as $a = $a - 1
$a *= 2; # same as $a = $a * 2
$a /= 2; # same as $a = $a / 2
$a %= 2; # same as $a = $a % 2
$a **= 2; # same as $a = $a ** 2
$a &= 2; # same as $a = $a & 2
$a |= 2; # same as $a = $a | 2
$a ^= 2; # same as $a = $a ^ 2</PRE>
<H5 ALIGN="CENTER">
<CENTER><A ID="I26" NAME="I26">
<FONT SIZE=3><B>Using Autoincrement and Autodecrement</B>
<BR></FONT></A></CENTER></H5>
<P>Another way to add 1 to a scalar variable is with the ++, or autoincrement, operator:
<BR></P>
<PRE>++$a; # same as $a += 1 or $a = $a + 1</PRE>
<P>This operator can appear either before or after its operand:
<BR></P>
<PRE>$a++; # also equivalent to $a += 1 and $a = $a + 1</PRE>
<P>The ++ operator can also be part of a more complicated sequence of operations. (A code fragment consisting of a sequence of operations and their values is known as an expression.) Consider the following statements:
<BR></P>
<PRE>$b = ++$a;
$b = $a++;</PRE>
<P>In the first statement, the ++ operator appears before its operand. This tells Perl to add 1 to $a before assigning its value to $b:
<BR></P>
<PRE>$a = 7;
$b = ++$a; # $a and $b are both 8</PRE>
<P>If the ++ operator appears after the operand, Perl adds 1 to $a after assigning its value to $b:
<BR></P>
<PRE>$a = 7;
$b = $a++; # $a is now 8, and $b is now 7</PRE>
<P>Similarly, the —, or autodecrement, operator subtracts 1 from the value of a scalar variable either before or after assigning the value:
<BR></P>
<PRE>$a = 7;
$b = —$a; # $a and $b are both 6
$a = 7;
$b = $a—; # $a is now 6, and $b is now 7</PRE>
<P>The ++ and — operators provide a great deal of flexibility, and are often used in loops and other control structures.
<BR></P>
<P>Do not use the ++ and — operators on the same variable more than once in the same expression:
<BR></P>
<PRE>$b = ++$a + $a++;</PRE>
<P>The value assigned to $b depends on which of the operands of the + operator is evaluated first. On some systems, the first operand (++$a) is evaluated first. On others, the second operand ($a++) is evaluated first.
<BR></P>
<P>You can ensure that you get the result you want by using multiple statements and the appropriate assignment operator:
<BR></P>
<PRE>$b = ++$a;
$b += $a++;</PRE>
<H5 ALIGN="CENTER">
<CENTER><A ID="I27" NAME="I27">
<FONT SIZE=3><B>Concatenating and Repeating Strings</B>
<BR></FONT></A></CENTER></H5>
<P>Perl provides three operators that operate on strings: The . operator, which joins two strings together; the x operator, which repeats a string; and the .= operator, which joins and then assigns.
<BR></P>
<P>The . operator joins the second operand to the first operand:
<BR></P>
<PRE>$a = "be" . "witched"; # $a is now "bewitched"</PRE>
<P>This join operation is also known as string concatenation.
<BR></P>
<P>The x operator (the letter x) makes <I>n</I> copies of a string, where <I>n</I> is the value of the right operand:
<BR></P>
<PRE>$a = "t" x 5; # $a is now "ttttt"</PRE>
<P>The .= operator combines the operations of string concatenation and assignment:
<BR></P>
<PRE>$a = "be";
$a .= "witched"; # $a is now "bewitched"</PRE>
<H5 ALIGN="CENTER">
<CENTER><A ID="I28" NAME="I28">
<FONT SIZE=3><B>Using Other C Operators</B>
<BR></FONT></A></CENTER></H5>
<P>Perl also supports the following operators, found in the C programming language: The , (comma) operator, and the ? and : (conditional) operator combination.
<BR></P>
<P>The , operator ensures that one portion of an expression is evaluated first:
<BR></P>
<PRE>$x += 1, $y = $x;</PRE>
<P>The , operator breaks this expression into two parts:
<BR></P>
<PRE>$x += 1
$y = $x</PRE>
<P>The part before the comma is performed first. Thus, 1 is added to $x and then $x is assigned to $y.
<BR></P>
<P>The ? and : combination allows you to test the value of a variable and then perform one of two operations based on the result of the test. For example, in the expression $y = $x == 0 ? 15 : 8 the variable $x is compared with zero. If $x equals zero, $y
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?