2.4 Scalar Operators
An
operator produces a new value (the
result
) from one or more other values (the
operands
). For example, Perl's operators and expressions are generally a superset of those provided in most other ALGOL/Pascal-like programming languages, such as C or Java. An operator expects either numeric or string operands (or possibly a combination of both). If you provide a string operand where a number is expected, or vice versa, Perl automatically converts the operand using fairly intuitive rules, which will be detailed in Section 2.4.4, "Conversion Between Numbers and Strings ," below. 2.4.1 Operators for NumbersPerl provides the typical ordinary addition, subtraction, multiplication, and division operators, and so on. For example: 2 + 3 # 2 plus 3, or 5 5.1 - 2.4 # 5.1 minus 2.4, or approximately 2.7 3 * 12 # 3 times 12 = 36 14 / 2 # 14 divided by 2, or 7 10.2 / 0.3 # 10.2 divided by 0.3, or approximately 34 10 / 3 # always floating point divide, so approximately 3.3333333...
Additionally, Perl provides the FORTRAN-like
exponentiation
operator, which many have yearned for in Pascal and C. The operator is represented by the double asterisk, such as
Perl also supports a
modulus
operator. The value of the expression
The
logical comparison operators are You may be wondering about the word "approximately" in the code comments at the start of this section. Don't you get exactly 2.7 when subtracting 2.4 from 5.1? In math class you do, but on computers you usually don't. Instead, you get an approximation that's only accurate to a certain number of decimal places. Computers don't store numbers the same way a mathematician thinks of them. But unless you are doing something extreme, you'll usually see the results you expect to see.
Comparing the following statements, you'll see what the computer really got as the result of the subtraction (the printf("%.51f\n", 5.1 - 2.4) # 2.699999999999999733546474089962430298328399658203125 print(5.1 - 2.4, "\n"); # 2.7
Don't worry too much about this: the 2.4.2 Operators for Strings
String values can be
concatenated with the " "hello" . "world" # same as "helloworld" 'hello world' . "\n" # same as "hello world\n" "fred" . " " . "barney" # same as "fred barney"
Note that the concatenation must be explicitly called for with the "
Another set of operators for strings are the
string comparison operators. These operators are FORTRAN-like, as in
You may wonder why there are separate operators for numbers and strings, if numbers and strings are automatically converted back and forth. Consider the two values 7 and 30. If compared as numbers, 7 is obviously less than 30, but if compared as strings, the string
Note that if you come from a UNIX shell programming background, the numeric and string comparisons are roughly opposite of what they are for the UNIX
test
command, which uses
Still another string operator is the
string repetition
operator, consisting of the single lowercase letter
"fred" x 3 # is "fredfredfred" "barney" x (4+1) # is "barney" x 5, or # "barneybarneybarneybarneybarney" (3+2) x 4 # is 5 x 4, or really "5" x 4, which is "5555"
That last example is worth spelling out slowly. The parentheses on If necessary, the copy count (the right operand) is first truncated to an integer value (4.8 becomes 4) before being used. A copy count of less than one results in an empty (zero-length) string. 2.4.3 Operator Precedence and Associativity
Operator precedence defines how to resolve the ambiguous case where two operators are trying to operate on three operands. For example, in the expression
You can override the order defined by precedence using
parentheses. Anything in parentheses is completely computed before the operator outside of the parentheses is applied (just as you learned in math class). So if you really want the addition before the multiplication, you can say While precedence is intuitive for addition and multiplication,[ 6 ] we start running into problems when faced with, say, string concatenation compared with exponentiation. The proper way to resolve this is to consult the official, accept-no-substitutes Perl operator precedence chart, shown in Table 2.3 . (Note that some of the operators have not yet been described, and in fact, may not even appear anywhere in this book, but don't let that scare you from reading about them.) Operators that are also found in C have the same precedence as in C.
In the chart, any given operator has higher precedence than those listed below it, and lower precedence than all of the operators listed above it. Operators at the same precedence level resolve according to rules of associativity instead. Just like precedence, associativity resolves the order of operations when two operators of the same precedence compete for three operands: 2 ** 3 ** 4 # 2 ** (3 ** 4), or 2 ** 81, or approx 2.41e24 72 / 12 / 3 # (72 / 12) / 3, or 6/3, or 2 30 / 6 * 3 # (30/6)*3, or 15
In the first case, the 2.4.4 Conversion Between Numbers and Strings
If a string value is used as an operand for a numeric operator (say,
Likewise, if a numeric value is given when a string value is needed (for the string concatenate operator, for example), the numeric value is expanded into whatever string would have been printed for that number. For example, if you want to concatenate an "X" . (4 * 5) # same as "X" . 20, or "X20"
(Remember that the parentheses force In other words, you don't have to worry about whether you have a number or a string (most of the time). Perl performs all the conversions for you. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|