Operators in Perl
Learn arithmetic, string, comparison, logical, and assignment operators in Perl, including the distinct numeric and string comparison families.
Operators are the building blocks of every expression you’ll write in Perl. Because Perl is dynamically typed with weak conversions between numbers and strings, it takes a route no other mainstream language matches: it gives you two parallel families of comparison operators — one for numbers (==, <, >, <=>) and one for strings (eq, lt, gt, cmp). The operator itself decides which type the operands should be coerced to, not the values.
This design eliminates a whole class of bugs that plague languages like JavaScript, where "10" < "9" evaluates differently depending on operand types. In Perl, you state your intent through the operator you pick. Combined with sigil-tagged variables and a rich set of string operators (., x, =~), Perl’s operator vocabulary reflects its origins as a text-processing language.
This tutorial covers arithmetic, string, comparison, logical, assignment, and a handful of distinctively Perl operators like the repetition operator x, the defined-or operator //, and the binding operator =~.
Arithmetic and String Operators
Perl’s arithmetic operators look familiar, but it also ships dedicated string operators: . for concatenation and x for repetition. Mixing them with weak typing produces some interesting results.
Create a file named operators_arith.pl:
| |
Note: under
use warnings;, coercing"abc"to a number raises a warning. The line is included to show the behavior, not to encourage it.
Comparison Operators: Numeric vs. String
This is the section that trips up newcomers from other languages. Perl has two complete sets of comparison operators. Pick the one that matches what you’re comparing.
| Comparison | Numeric | String |
|---|---|---|
| Equal | == | eq |
| Not equal | != | ne |
| Less than | < | lt |
| Greater than | > | gt |
| Less or equal | <= | le |
| Greater or equal | >= | ge |
| Three-way (sort) | <=> | cmp |
The three-way operators (<=> and cmp) return -1, 0, or 1 — perfect for sort.
Create a file named operators_cmp.pl:
| |
The string sort places 10 before 2 because "1" comes before "2" lexicographically — exactly the bug a naive sort produces in many languages.
Logical, Assignment, and Distinctly Perl Operators
Perl’s logical operators come in two flavors: the C-style symbols (&&, ||, !) and lower-precedence word forms (and, or, not). The word forms are typically used for control flow at statement level (open(FH, $f) or die "..."), while the symbol forms are used inside expressions.
Perl also offers the defined-or operator //, which only falls back when the left side is undef (not merely false), and the binding operator =~ that ties a value to a regex.
Create a file named operators_logic.pl:
| |
Note that Perl uses # for comments — there are no // line comments. The // token always means the defined-or operator (or an empty regex), depending on context.
Running with Docker
| |
Expected Output
Output of operators_arith.pl:
a + b = 22
a - b = 12
a * b = 85
a / b = 3.4
a % b = 2
a ** b = 1419857
int(a/b) = 3
Hello, World!
--------------------
hahaha
'42' + 8 = 50
'42' . 8 = 428
'abc' + 1 = 1
Output of operators_cmp.pl:
10 == 9? no
10 eq 9? no
10 < 9? no
10 lt 9? yes
5 <=> 10 = -1
5 <=> 5 = 0
5 <=> 1 = 1
Numeric sort: 2 4 10 25 33
String sort: 10 2 25 33 4
Output of operators_logic.pl:
a || b = fallback
a && b = 0
0 || 'x' = x
0 // 'x' = 0
u // 'x' = x
after += 5 : 15
after -= 3 : 12
after *= 2 : 24
after **= 2 : 576
after .= : log-entry
after x= 2: log-entrylog-entry
Found year: 2024
PERL 5.40 was released in 2024
Range 1..5: 1 2 3 4 5
Key Concepts
- Two comparison families:
==/</>work on numbers;eq/lt/gtwork on strings. The operator decides the coercion, not the operands. - String operators are first-class:
.concatenates,xrepeats — they aren’t overloaded+and*as in many languages. - Three-way operators (
<=>for numbers,cmpfor strings) return-1,0, or1and are the idiomatic way to build customsortcomparators. - Defined-or
//is the safe fallback: it only triggers onundef, unlike||which fires for any falsy value (including0and""). - Compound assignment works for every binary operator, including the string-specific ones —
.=appends,x=repeats in place. - The binding operator
=~is what makes regex feel built-in: it attaches a string to am//match ors///substitution. - Word-form logicals (
and,or,not) have very low precedence — ideal for statement-level flow control likeopen($fh, $f) or die, but use the symbol forms inside expressions. - Weak typing means silent coercion: a non-numeric string used in arithmetic becomes
0(with a warning underuse warnings), so always pick the operator family that matches your intent.
Comments
Loading comments...
Leave a Comment