Notifications
Clear all
Topic starter 01/08/2025 11:34 pm
Perl has a rich set of operators that let you manipulate data, perform calculations, compare values, and control logic—like giving your code superpowers 🦸♂️💻
➕ Arithmetic Operators
Used for basic math:
Operator | Description | Example | Result |
---|---|---|---|
+ |
Addition | 5 + 3 |
8 |
- |
Subtraction | 5 - 3 |
2 |
* |
Multiplication | 5 * 3 |
15 |
/ |
Division | 6 / 3 |
2 |
% |
Modulus (remainder) | 5 % 2 |
1 |
** |
Exponentiation | 2 ** 3 |
8 |
🔤 String Operators
Used to manipulate strings:
Operator | Description | Example | Result |
---|---|---|---|
. |
Concatenation | "Hello" . "World" |
"HelloWorld" |
x |
Repetition | "Hi" x 3 |
"HiHiHi" |
🔍 Comparison Operators
Used to compare values:
Type | Operator | Description | Example |
---|---|---|---|
Numeric | == |
Equal | 5 == 5 |
Numeric | != |
Not equal | 5 != 3 |
Numeric | < > |
Less/Greater than | 5 < 6 |
String | eq |
Equal | 'a' eq 'a' |
String | ne |
Not equal | 'a' ne 'b' |
String | lt gt |
Less/Greater than | 'a' lt 'b' |
🔁 Logical Operators
Used for Boolean logic:
Operator | Description | Example | Result | ||||
---|---|---|---|---|---|---|---|
&& |
AND | 1 && 0 |
false |
||||
` | ` | OR | `1 | 0` | true |
||
! |
NOT | !1 |
false |
🔧 Bitwise Operators
Operate on binary representations:
Operator | Description | Example | ||
---|---|---|---|---|
& |
AND | 5 & 3 |
||
` | ` | OR | `5 | 3` |
^ |
XOR | 5 ^ 3 |
||
~ |
NOT | ~5 |
||
<< |
Shift left | 5 << 1 |
||
>> |
Shift right | 5 >> 1 |
🧠 Operator Precedence
Just like math, some operators take priority. For example, multiplication happens before addition:
print 2 + 3 * 4; # Outputs 14, not 20
Use parentheses to control order:
print (2 + 3) * 4; # Outputs 20