Operators
Learn Perl's core operators: arithmetic, string concatenation with ., string repetition with x, and the distinction between numeric and string comparison operators.
Introduction
Operators are the small symbols and keywords that let you combine, compare, and transform values. Perl has a particularly rich operator set, and understanding it well — especially the numeric-versus-string split — will save you from some of the most common beginner bugs in the language.
- Perl's arithmetic operators.
- How to concatenate strings with the . operator.
- How to repeat strings with the x operator.
- The difference between numeric comparisons (==) and string comparisons (eq).
Arithmetic Operators
Perl supports the standard set of arithmetic operators you would expect from most programming languages, plus a couple of convenient shortcuts.
| Operator | Meaning | Example |
|---|---|---|
| + | Addition | 5 + 2 is 7 |
| - | Subtraction | 5 - 2 is 3 |
| * | Multiplication | 5 * 2 is 10 |
| / | Division | 5 / 2 is 2.5 |
| % | Modulus (remainder) | 5 % 2 is 1 |
| ** | Exponentiation | 5 ** 2 is 25 |
use strict;use warnings;
my $a = 17;my $b = 5;
print "Sum: ", $a + $b, "\n";print "Difference: ", $a - $b, "\n";print "Product: ", $a * $b, "\n";print "Quotient: ", $a / $b, "\n";print "Remainder: ", $a % $b, "\n";print "Power: ", $a ** 2, "\n";
my $counter = 10;$counter++; # increment shorthand$counter += 5; # add-and-assign shorthandprint "Counter: $counter\n";Click Run to see what this code prints.
String Concatenation with .
To join two strings together explicitly, Perl uses the . (dot) operator. This is distinct from the + operator, which is reserved strictly for numeric addition — a deliberate design choice that avoids the ambiguity some other languages have with "+" meaning both addition and concatenation.
use strict;use warnings;
my $first_name = "Ada";my $last_name = "Lovelace";
my $full_name = $first_name . " " . $last_name;print "Full name: $full_name\n";
my $message = "Hello, " . $full_name . "!";$message .= " Welcome to Perl."; # append-and-assign shorthandprint "$message\n";Click Run to see what this code prints.
String Repetition with x
The x operator repeats a string a given number of times, which is a distinctly Perl-flavored convenience for building separators, padding, or simple ASCII formatting.
use strict;use warnings;
my $separator = "-" x 20;print "$separator\n";print "Report Summary\n";print "$separator\n";
my @repeated_list = ("ha") x 3; # x also works on lists in list contextprint "@repeated_list\n";Click Run to see what this code prints.
Numeric Comparison Operators
Numeric comparisons compare two values as numbers, converting strings to numbers if needed.
| Operator | Meaning |
|---|---|
| == | Equal to |
| != | Not equal to |
| < | Less than |
| > | Greater than |
| <= | Less than or equal to |
| >= | Greater than or equal to |
| <=> | Numeric comparison (returns -1, 0, or 1); commonly used with sort |
use strict;use warnings;
my $x = 10;my $y = "10.0"; # a string, but numerically equal to 10
if ($x == $y) { print "Numerically equal\n";}
my @numbers = (40, 5, 100, 25);my @sorted_numbers = sort { $a <=> $b } @numbers;print "Sorted: @sorted_numbers\n";Click Run to see what this code prints.
String Comparison Operators
String comparisons compare two values as text, character by character, using alphabetical (lexicographic) ordering rather than numeric value.
| Operator | Meaning |
|---|---|
| eq | Equal to |
| ne | Not equal to |
| lt | Less than (alphabetically before) |
| gt | Greater than (alphabetically after) |
| le | Less than or equal to |
| ge | Greater than or equal to |
| cmp | String comparison (returns -1, 0, or 1); commonly used with sort |
use strict;use warnings;
my $status1 = "active";my $status2 = "Active";
if ($status1 eq $status2) { print "Statuses match exactly.\n";} else { print "Statuses differ (case-sensitive comparison).\n";}
my @names = ("Charlie", "alice", "Bob");my @sorted_names = sort { $a cmp $b } @names;print "Sorted names: @sorted_names\n";Click Run to see what this code prints.
The last example also demonstrates that cmp comparisons are case-sensitive by default, sorting uppercase letters before lowercase ones based on their underlying character codes.
Why Two Sets of Comparisons Exist
This is one of the single most important things to internalize as a Perl beginner: == and eq are not interchangeable, even though many other languages only offer one comparison operator that handles both. Using the wrong one produces subtle, hard-to-spot bugs.
use strict;use warnings;
my $a = "10";my $b = "9";
if ($a > $b) { print "Numerically, 10 is greater than 9\n";}
if ($a gt $b) { print "Sorted as text, this branch runs instead\n";} else { print "String-wise, \"10\" is NOT greater than \"9\", because '1' sorts before '9'\n";}Click Run to see what this code prints.
Numerically, 10 is bigger than 9. But as text, "10" is compared character by character: the first character '1' comes before '9' alphabetically, so "10" is considered "less than" "9" in string comparison. This exact mismatch is one of the most common sources of real Perl bugs, especially when comparing user input or data read from files, which always starts out as text.
Common Mistakes
- Using == to compare strings, or eq to compare numbers — always match the operator to the data's intended meaning.
- Using + to concatenate strings — Perl reserves + strictly for numeric addition; use . instead.
- Forgetting that sort defaults to string comparison, so sort @numbers on numeric data gives wrong-looking results unless you use sort { $a <=> $b } @numbers.
- Assuming string comparisons are case-insensitive — eq and cmp are case-sensitive by default.
Best Practices
- Always choose == family operators for numbers and eq family operators for strings, even when values look interchangeable.
- Use <=> inside sort blocks for numeric sorting, and cmp for string sorting.
- Use .= and += (and similar compound assignment operators) to make intent clear when updating a variable in place.
- When comparing values from user input or files, remember they usually start as strings, so decide deliberately whether a numeric or string comparison is correct.
Frequently Asked Questions
Perl's designers wanted the language itself to make your intent explicit: whether you mean "these are numerically equal" or "these are the same text" are genuinely different questions, and separate operators prevent accidental confusion between them.
By default, sort compares elements as strings. To sort numbers correctly, supply an explicit block like sort { $a <=> $b } @array.
Yes, by default. If you need case-insensitive comparison, convert both sides with lc() (lowercase) or uc() (uppercase) before comparing.
Key Takeaways
- Perl supports the standard arithmetic operators (+, -, *, /, %, **) plus handy shorthand like += and ++.
- The . operator concatenates strings; + is reserved for numeric addition.
- The x operator repeats a string (or list) a given number of times.
- Numeric comparisons (==, !=, <, >, <=>, etc.) and string comparisons (eq, ne, lt, gt, cmp, etc.) are separate operator families and are not interchangeable.
- sort compares as strings by default; use <=> for correct numeric sorting.
Summary
You now have a solid grasp of Perl's arithmetic, string, and comparison operators, including the crucial numeric-versus-string distinction. In the final lesson of this batch, we will cover input and output in more depth: print versus say, reading from STDIN, and cleaning up input with chomp.