| 1. NUMERIC PERL OPERATORS |
| 1.1. ARITHMETIC PERL OPERATORS |
In the table below you see the Perl operators used in arithmetic operations.
| Symbol | Name | Definition |
| + | addition | It's a binary operator that returns the sum of two operands. Example: |
| - | subtraction | It's a binary operator that returns the difference of two operands. Example: |
| - | negation | I's a unary operator that performs arithmetic negation. Example: |
| * | multiplication | It's a binary operator that multiplies two operands. Example: |
| / | division | It's a binary operator that divides left value by right value. Example: |
| ** | exponentiation | It's a binary operator that raises the left value to the power of the right value. Example: |
| % | modulus | It's a binary operator that returns the remainder of dividing left value by right value. Example: |
| ++ | autoincrement | If you place this unary operator before/after a variable, the variable will be incremented before/after returning the value. Example: |
| -- | autodecrement | If you place this unary operator before/after a variable, the variable will be decremented before/after returning the value. Example: |
Check my new How To Tutorial eBooks (PDF format):
- Perl Scalar and String Functions
- Perl Functions for Real Arrays
- Perl Functions for List Data
- Perl Functions for Real Hashes
- Perl Complex Data Structures
- Perl Statements
to see a lot of examples about how to use built-in functions, complex data structures and statements in Perl.
| 1.2. NUMERIC RELATIONAL PERL OPERATORS |
The numeric relational Perl operators compare two numbers and determine the validity of a relationship.
| Symbol | Name | Definition |
| < | less then | The "less then" operator indicates if the value of the left operand is less than the value of the right one. Example: |
| <= | less than or equal to | The "less than or equal to" operator indicates if the value of the left operand is less than or equal to the value of the right one. Example: |
| > | greater than | The "greater than" operator indicates if the value of the left operand is greater than the value of the right one. Example: |
| >= | greater than or equal to | The "greater than or equal to" operator indicates if the value of the left operand is greater than or equal to the value of the right one. Example: |
| == | equal | The "equal" operator returns true if the left operand is equal to the right one. Example: |
| != | not equal to | The "not equal to" operator returns true if the left operand is not equal to the right one. Example: |
| <=> | numeric comparison | This binary operator returns -1, 0, or 1 if the left operand is less than, equal to or greater than the right one. Example: |
| 1.3. NUMERIC LOGICAL PERL OPERATORS |
The numeric logical Perl operators are generally derived from boolean algebra and they are mainly used to control program flow, finding them as part of an if, a while or some other control statement. See in the table below the logical numerical Perl operators.
| Symbol | Name | Definition |
| ! | negation | This unary operator evaluates an operand and return true if the operand has the false value (0) and false otherwise. Example: |
| not | not | It has the same meaning as the "!" operator, described above. |
| and, && | and | The "and" operator returns the logical conjunction of two operands. Example: |
| or, || | or | The "or" operator returns the logical disjunction of two operands. Example: |
| xor | exclusive or | The "exclusive or" operator returns the logical exclusive-or of two operands (the result is true if either but not both of the operands is true). Example: |
| ? | conditional operator | This ternary operator is like the symbolic if ... then ... else clause from the C language. It returns the second operand if the leftmost operand is true and the third operand otherwise. Example: |
| 1.4. NUMERIC BITWISE PERL OPERATORS |
Numeric bitwise Perl operators are similar to the logical operators, but they work on the binary representation of data. They are used to change individual bits in an operand. Please note that both operands associated with bitwise operators are integers.
| Symbol | Name | Definition |
| << | shift left | The "shift left" << operator is a binary operator that shifts the bits to the left. Its first operand specifies the integer value to be shifted meanwhile the second one specifies the number of position that the bits in the value will be shifted. The rightmost bits of the integer value will be assigned with 0 and the leftmost bits will be discarded. Example: |
| >> | shift right | The "shift right" >> operator is a binary operator that shifts the bits to the right. Its first operand specifies the integer value to be shifted meanwhile the second one specifies the number of position that the bits in the value will be shifted. The leftmost bits of the integer value will be assigned with 0 and the rightmost bits will be discarded. Example: |
| & | and | The "and" operator sets a bit to 1 if both of the corresponding bits in its operands are 1, and 0 otherwise. Example: |
| | | or | The "or" operator sets a bit to 0 if both of the corresponding bits in its operands are 0, and 1 otherwise. Example: |
| ^ | exclusive or | The "exclusive or" operator sets a bit to 1 if the corresponding bits in its operands are different, and 0 otherwise. Example: |
| ~ | not | The unary "not" operator inverts each bit in the operand, changing all the ones to zeros and zeros to ones. Example: |
| 1.5. OTHER NUMERIC PERL OPERATORS |
See in the table below other numeric Perl operators:
| Symbol | Name | Definition |
| , | comma | In scalar context this binary operator evaluates its left argument, discards this value, then evaluates its right argument and returns that value. In a list context, it's just a separator and inserts both its arguments into the list. Example (in scalar context): |
| => | comma | It has the same function like the comma operator described above. |
| .. | Range operator | In scalar context, this operator returns false as long as its left operand is false. When the left operand becomes true, the range operator returns true until the right operator remains true, after which it becomes false again. In a list context, this operator will return an array with contiguous sequences of items, beginning with the left operand value and ending with the right operand value (the items can be characters or numbers). Example (in a list context): |
| 1.6. NUMERIC ASSIGNMENT PERL OPERATORS |
Numeric assignment Perl operators perform some type of numeric operation and then assign the value to the existing variable.
| Symbol | Name | Definition |
| = | assignment | This is the ordinary assignment operator. In a scalar context, it assigns the right operand's value to the left operand. In a list context, it assigns multiple values to the left array operand if the right operand is a list. Example: |
| += | addition | It adds the right operand's value to the left operand. Example: |
| -= | subtraction | It subtracts the right operand from the left operand. |
| *= | multiplication | It multiplies the left operand's value by the right operand's value. Example: |
| /= | division | It divides the left operand's value by the right operand's value. Example: |
| **= | exponentiation | It raises the left operand's value to the power of the right operand's value. Example: |
| %= | modulus | It divides the left operand value by the right operand value and assigns the remainder to the left operand. Example: |
| &&= | logical and | It's a combination between the logical "&&" and the assignment operators. Example: |
| ||= | logical or | It's a combination between the logical "||" and the assignment operators. Example: |
| <<= | bitwise shift left | It's a bitwise left shift assign. Example: |
| >>= | bitwise shift right | It's a bitwise right shift assign. Example: |
| &= | bitwise and | It's a bitwise AND assign. Example: |
| |= | bitwise or | It's a bitwise OR assign. Example: |
| ^= | bitwise exclusive or | It's a bitwise XOR assign. Example: |
| 2. STRING PERL OPERATORS |
| 2.1.STRING RELATIONAL PERL OPERATORS |
The string relational Perl operators compare two strings and determine the validity of a relationship.
| Symbol | Name | Definition |
| lt | less than | It returns true if the left operand is stringwise less then the right one. Example: |
| le | less than or equal to | It returns true if the left operand is stringwise less then or equal to the right one. Example: |
| gt | greater than | It returns true if the left operand is stringwise greater then the right one. Example: |
| ge | greater than or equal to | It returns true if the left operand is stringwise greater then or equal to the right one. Example: |
| eq | equality | It returns true if the left operand is stringwise equal to the right one. Example: |
| ne | not equal to | It returns true if the left operand is stringwise not equal to the right one. Example: |
| cmp | comparison | It returns -1, 0, or 1 if the left operand is stringwise less than, equal to or greater than the right one. Example: |
| 2.2. STRING LOGICAL PERL OPERATORS |
The string logical Perl operators are generally derived from boolean algebra and they are mainly used to control program flow, finding them as part of an if, a while or some other control statement. See in the table below the logical string Perl operators.
| Symbol | Name | Definition |
| ! | not | It returns true if the operand is a null string or an undefined value and false otherwise. Example: |
| not | not | The same meaning as above, but it is a lower-precedence version. |
| && | and | This operator is used to determine if both operands are true. Example: |
| and | and | Same as above. |
| || | or | This operator is used to determine if either of the operands is true. Example: |
| or | or | Same as above. |
| xor | exclusive or | It returns true if either but not both of the operands is true. Example: |
| ? | conditional operator | This is a ternary operator and it works like an if ... then ... else clause from the C language. If the left operand is true, it will return the central operand, otherwise the right operand. Example: |
| 2.3. OTHER STRING PERL OPERATORS |
See in the table below other string Perl operators:
| Symbol | Name | Definition |
| , | comma | In a scalar context, the comma operator evaluates each element from left to right and returns the value of the rightmost element. In a list context, the comma operator separates the elements of a literal list. Example: |
| => | comma | This operator is a special type of comma, for example ('dog', 'cat') is similar to (dog => 'cat'). Or it can be used to separate key/value pairs in a hash structure: %petColors = (dog => 'brown', cat => 'white'); |
| - | negation | If the string operand begins with a plus or minus sign, the string negation operator returns a string with the opposite sign. Example: |
| . | concatenation | This operator joins two or more strings like in the example below. Example: |
| .. | range operator | The range operator, in a list context, produces the range of values from the left value through the right value in increments of 1. Example: |
| x | repetition | The repetition operator returns the first operand (which is a string) repeated by the number of times specified by the second operand (which is an integer). Example: |
| 2.4. STRING ASSIGNMENT PERL OPERATORS |
String assignment Perl operators perform some type of string operation and then assign the value to the existing variable.
| Symbol | Name | Definition |
| = | assignment | This is the ordinary assignment operator. Example: |
| &&= | logical and | It's a combination between the logical "&&" and the assignment operators. Example: |
| ||= | logical or | It's a combination between the logical "||" and the assignment operators. Example: |
| .= | concatenation | It's a combination between the concatenation "." and the assignment operators. Example: |
| x= | repetition | It's a repetition assignment operator. Example: |
| 3. SPECIAL PERL OPERATORS |
See in the table below the special Perl operators:
| Symbol | Name | Definition |
| \ | reference | We call reference the scalar value that contains a memory address. In order to reference a variable, we use the operator. Look at the below snippet code to see how to use it. Example: |
| -> | dereference | The dereference operator was used for the first time in the Perl 5 language. This operator let us to access and manipulate the elements of an array, hash, object of a class data structure. If you use the reference operator to reference a scalar variable, before you use it you must dereference the reference variable. Example: |
| =~ | pattern binding | This binary operator binds a string expression to a pattern match. The string which is intend to bind is put on the left meanwhile the operator itself is put on the right. We use the pattern binding operator in the case we have a string which is not stored in the $_ variable and we need to perform some matches or substitutions of that string. Example: |
| !~ | pattern binding (not) | This operator is similar the operator above, but the return value is negated logically. Example: |
没有评论:
发表评论