Perl supports a number of operators to perform various operations. There are comparison operators (used to compare values, as the name implies), compound operators (used to combine operations or multiple comparisons), arithmetic operators (to perform math), and special string constants. Comparison Operators The comparison operators used by Perl are similar to those used by C, awk, and the csh shells, and are used to specify and compare values (including strings). Most frequently, a comparison operator is used within an if statement or loop. Perl has comparison operators for numbers and strings. Table 30.1 shows the numeric comparison operators and their behavior. Table 30.1. Numeric Comparison Operators in PerlOperator | Meaning |
---|
== | Is equal to | < | Less than | > | Greater than | <= | Less than or equal to | >= | Greater than or equal to | != | Not equal to | .. | Range of >= first operand to <= second operand | <=> | Returns -1 if less than, 0 if equal, and 1 if greater than |
Table 30.2 shows the string comparison operators and their behaviors. Table 30.2. String Comparison Operators in PerlOperator | Meaning |
---|
eq | Is equal to | lt | Less than | gt | Greater than | le | Less than or equal to | ge | Greater than or equal to | ne | Not equal to | cmp | Returns -1 if less than, 0 if equal, and 1 if greater than | =~ | Matched by regular expression | !~ | Not matched by regular expression |
Compound Operators Perl uses compound operators, similar to those used by C or awk, which can be used to combine other operations (such as comparisons or arithmetic) into more complex forms of logic. Table 30.3 shows the compound pattern operators and their behavior. Table 30.3. Compound Pattern Operators in PerlOperator | Meaning |
---|
&& | Logical AND | || | Logical OR | ! | Logical NOT | () | Parentheses; used to group compound statements |
Arithmetic Operators Perl supports a wide variety of math operations. Table 30.4 summarizes these operators. Table 30.4. Perl Arithmetic OperatorsOperator | Purpose |
---|
x**y | Raises x to the y power (same as x^y) | x%y | Calculates the remainder of x/y | x+y | Adds x to y | x-y | Subtracts y from x | x*y | Multiplies x times y | x/y | Divides x by y | -y | Negates y (switches the sign of y); also known as the unary minus | ++y | Increments y by 1 and uses value (prefix increment) | y++ | Uses value of y and then increments by 1 (postfix increment) | --y | Decrements y by 1 and uses value (prefix decrement) | y-- | Uses value of y and then decrements by 1 (postfix decrement) | x=y | Assigns value of y to x. Perl also supports operator-assignment operators (+=, - =, *=, /=, %=, **=, and others) |
You can also use comparison operators (such as == or <) and compound pattern operators (&&, ||, and !) in arithmetic statements. They evaluate to the value 0 for false and 1 for true. Other Operators Perl supports a number of operators that don't fit any of the prior categories. Table 30.5 summarizes these operators. Table 30.5. Other Perl OperatorsOperator | Purpose |
---|
~x | Bitwise not (changes 0 bits to 1 and 1 bits to 0) | x & y | Bitwise and | x | y | Bitwise or | x ^ y | Bitwise exclusive or (XOR) | x << y | Bitwise shift left (shifts x by y bits) | x >> y | Bitwise shift right (shifts x by y bits) | x . y | Concatenate y onto x | a x b | Repeats string a for b number of times | x , y | Comma operatorevaluates x and then y | x ? y : z | Conditional expressionif x is true, y is evaluated; otherwise, z is evaluated |
Except for the comma operator and conditional expression, these operators can also be used with the assignment operator, similar to the way addition (+) can be combined with assignment (=), giving +=. Special String Constants Perl supports string constants that have special meaning or cannot be entered from the keyboard. Table 30.6 shows most of the constants supported by Perl. Table 30.6. Perl Special String ConstantsExpression | Meaning |
---|
\\ | The means of including a backslash | \a | The alert or bell character | \b | Backspace | \cC | Control character (like holding the Ctrl key down and pressing the C character) | \e | Escape | \f | Formfeed | \n | Newline | \r | Carriage return | \t | Tab | \xNN | Indicates that NN is a hexadecimal number | \0NNN | Indicates that NNN is an octal (base 8) number |
|