Previous Page Next Page

Chapter 6. Where's the Operator?

6.1. About Perl Operators

In the real world, there are operators who operate switchboards, computers, bulldozers, tanks, etc. In Perl, operators operate on numbers and strings or a combination of them. Operators are symbols, such as +, -, =, >, <, that produce a result based on some rules. An operator manipulates data objects called operands; e.g., 5 and 4 are operands in the expression 5 + 4. Operators and operands are found in expressions. An expression combines a group of values to make a new value, n = 5 + 4. And when you terminate an expression with a semicolon, you have a complete statement; e.g., n = 5 + 4.

Figure 6.1. Evaluating an Expression.


In the numeric expression, 5 + 4 - 2, three numbers are combined. The operators are the + and - signs. The operands for the + sign are 5 and 4. After that part of the expression is evaluated to 9, the expression becomes 9 - 2. After evaluating the complete expression, the result is 7. Since the plus sign and minus operators each manipulate two operands, they are called binary operators. If there is only one operand, the operator is called a unary operator, and if there are three operands, it is called a ternary operator. We'll see examples of these operators later in the chapter.

Most of the Perl operators are borrowed from the C language, although Perl has some additional operators of its own.[1]

[1] The operators can be symbols or words. Perl 5 functions can be used as operators if the parentheses are omitted.

Previous Page Next Page