100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Perl Operators

A tour of Perl's arithmetic, string, comparison, logical, and assignment operators, and the pitfalls of mixing numeric and string comparisons.

FoundationsBeginner9 min readJul 10, 2026
Analogies

Perl Operators

Perl provides a large, expressive set of operators, and one of its most distinctive design decisions is keeping numeric and string comparisons completely separate: ==, !=, <, >, <=, and >= compare numbers, while eq, ne, lt, gt, le, and ge compare strings. This separation matters because Perl's automatic context conversion means the two operator families can disagree: '10' == '10.0' is true, since both sides convert to the same number, while '10' eq '10.0' is false, since the two strings are spelled differently, so picking the wrong operator for the data you actually have is one of the most common sources of subtle bugs for programmers new to Perl.

🏏

Cricket analogy: Similar to how a match can be decided by runs scored (a numeric comparison) or by which team's name comes first alphabetically in a schedule (a string comparison), Perl keeps == for numbers and eq for strings as separate tools.

Arithmetic and String Operators

The standard arithmetic operators +, -, *, /, % (modulus), and ** (exponentiation) work as expected on numbers, and Perl adds two especially useful shortcuts: . concatenates two strings, so 'foo' . 'bar' yields 'foobar', and x repeats a string or list, so 'ab' x 3 yields 'ababab' while (1, 2) x 3 yields the list (1, 2, 1, 2, 1, 2). Compound assignment operators like +=, -=, .=, and **= combine an operation with assignment in one step, and the increment/decrement operators ++ and -- are especially notable in Perl because ++ works correctly on strings too, so my $s = 'az'; $s++; produces 'ba', a feature Perl calls 'magic string increment' that is useful for generating sequences like spreadsheet-style column labels.

🏏

Cricket analogy: Similar to how a scorer appends each new run to a running tally, .= in Perl appends new text to an existing string in place, like building up a live commentary line ball by ball.

Logical and Assignment Operators

Perl's logical operators come in two flavors with different precedence: the C-style &&, ||, and ! bind tightly and are preferred inside expressions like if ($a && $b), while the word forms and, or, and not bind very loosely and are preferred for control flow, most famously in the idiom open(my $fh, '<', $file) or die "Cannot open: $!";, where low precedence ensures the whole assignment happens before the or is even considered. The defined-or operator // (added in Perl 5.10) returns its left operand if it is defined and the right operand otherwise, which is safer than || for numeric defaults because || would incorrectly treat a legitimate value of 0 as false; for example, my $count = $input // 0; correctly keeps an explicit 0 from $input, whereas my $count = $input || 0; would wrongly overwrite it.

🏏

Cricket analogy: Similar to how a coach's decision to bowl or bat depends on winning the toss AND the pitch conditions, if ($won_toss && $pitch_favors_bowling) in Perl combines two conditions that both must be true.

perl
use strict;
use warnings;

my $a = '10';
my $b = '9';

print "Numeric: ", ($a == $b ? 'equal' : 'not equal'), "\n";  # compares 10 vs 9
print "String:  ", ($a eq $b ? 'equal' : 'not equal'), "\n";  # compares '10' vs '9'

my $label = 'Item ' x 3;          # 'Item Item Item '
my $joined = 'foo' . 'bar';       # 'foobar'

my $input = 0;
my $count = $input // 5;          # 0, because // only falls back on undef
print "count = $count\n";

open(my $fh, '<', 'data.txt') or die "Cannot open data.txt: $!";

Never use == to compare two strings, and never use eq to compare two numbers. 'abc' == 'xyz' silently evaluates as 0 == 0 (true!) because Perl converts both non-numeric strings to 0 in numeric context, which is a classic and hard-to-spot bug.

  • Perl keeps numeric comparisons (==, !=, <, >, <=, >=) strictly separate from string comparisons (eq, ne, lt, gt, le, ge).
  • The . operator concatenates strings and x repeats a string or list a given number of times.
  • ++ performs 'magic string increment' on alphanumeric strings, e.g. incrementing 'az' produces 'ba'.
  • C-style && || ! bind tightly for use in expressions; word-form and/or/not bind loosely, ideal for open(...) or die.
  • The defined-or operator // only falls back to its right operand when the left is undef, unlike ||, which also falls back on 0 or ''.
  • Compound assignment operators like +=, .=, and **= combine an operation with assignment in a single step.
  • Comparing a string with a numeric operator (or vice versa) is a classic Perl bug since both sides get silently coerced.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#PerlStudyNotes#PerlOperators#Perl#Operators#Arithmetic#String#StudyNotes#SkillVeris#ExamPrep