You are in: > Home > Developers Guides
Perl - Operators

Numeric and String Operators
There are many operators in Perl, here are a few that are commonly used:
| Operator | Description | Example | Result |
|---|---|---|---|
| . | String concatenate | 'ACTGA' . 'GCG' | ACTGAGCG |
| = | Assignment | $a = 'ATP' | $a variable is storing 'ATP' |
| + | Addition | 4 + 2 | 6 |
| - | Subtraction | 7 - 2 | 5 |
| - | Negation | -3 | -3 |
| ! | Not | !1 | 0 |
| * | Multiplication | 5 * 2 | 10 |
| / | Division | 7 / 2 | 3.5 |
| % | Modulus | 3%2 | 1 |
| ** | Exponentiation | 6**2 | 36 |
| Repetition | 'A' x 7 | AAAAAAA |
Logical Operators
The logical operators evaluate from left to right testing the truth of the statement.
Comparison Operators
These operators compare strings or numbers, returning TRUE (1) or FALSE (0):
| Numeric Comparison | String Comparison | ||||
|---|---|---|---|---|---|
| 3 == 2 | 0 | equal to | 'Santa' eq 'Claus' | 0 | equal to |
| 3 != 2 | 1 | not equal to | 'Santa' ne 'Claus' | 1 | not equal to |
| 3 < 2 | 0 | less than | 'Santa' lt 'Claus' | 0 | less than |
| 3 > 2 | 1 | greater than | 'Santa' gt 'Claus' | 1 | greater than |
| 3 <= 2 | 0 | less or equal | 'Santa' le 'Claus' | 0 | less than or equal |
| 3 >= 2 | 1 | greater than or equal | 'Santa' ge 'Claus' | 1 | greater than or equal |
| 3 <=> 2 | 1 | compare | 'Santa' cmp 'Claus' | 1 | compare |
The string comparison operators compare the alphabetical order.
The numeric comparison operators are quite self explanatory except probably the last numeric one, which may need an example:
3 <=> 2
if left side of the operator is greater than the right side, it returns 1
if both sides are equal, 0
if right side of the operator is greater than the left side, it returns -1
Related articles
- Perl - Downloading a URL with a script - [22nd Mar 2006]
- Perl - Pattern Matching (Regular Expressions) - [12th Jan 2006]
- Shell Scripting - Reading Variables From STDIN - [11th Jan 2006]
- Perl - Configuring and Starting the mod_perl Server - [06th Jan 2006]
- Perl - Installing mod_perl in Three Steps - [06th Jan 2006]
- Perl - mod_perl - [06th Jan 2006]
- Perl - Associative Arrays - [06th Jan 2006]
- Perl - Manipulating Arrays - [06th Jan 2006]
- Perl - Arrays - [06th Jan 2006]
- Perl - Operators - [06th Jan 2006]
- Perl - Scalar Variables - [06th Jan 2006]
- Shell Scripting - Introduction - [08th Dec 2005]
- Shell Scripting - Invoking Shell Scripts - [08th Dec 2005]
- Shell Scripting - Variables - [08th Dec 2005]
- Shell Scripting - Referencing Variables - [08th Dec 2005]
- Shell Scripting - Command Arguments - [08th Dec 2005]
- Shell Scripting - Language Constructs - [08th Dec 2005]
- Perl - Introduction - [06th Dec 2005]
- MySQL - Connecting to the server - [06th Dec 2005]
- MySQL - Introduction - [06th Dec 2005]







