HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to PerlOperatorsStringsArrays (@)Hashes (%)Conditions (if)LoopsFunctions (sub)
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
✍️ Text & Number
📋 Array & Hash
🔁 Condition & Loop
🧩 Function (sub)
Perl›Operators

Operators

Arithmetic (+ - * / % **), concatenation with ., and comparison (number: ==, string: eq).

Simple enough for a 5-year-old to understand

Arithmetic and concatenation

Example code
1
print 7 % 2, "\n";
Remainder: 1
CodeRemainder: 1
2
print 2 ** 10, "\n";
Exponent: 1024
CodeExponent: 1024
3
print "Hex" . "Library", "\n";
Concatenate: HexLibrary
Output / InputConcatenate: HexLibrary
CodeOutput / Input
💡 Hint: String concatenation uses a DOT (.) not +.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Print the remainder of 23 divided by 4.

🎯 Expected output: 3

Loading editor…

About this topic

Perl's operators have a fundamental design that differs from other languages: separate operators are used for numeric and string operations. Addition and concatenation, numeric equality and string equality are written with different symbols.

The reason for this is that Perl keeps values untyped. A variable can represent both the string "10" and the number 10; the operator determines which operation you want. This makes the language flexible, but makes operator selection critical.

Not knowing this distinction is the most common mistake in Perl. When you use the numeric equality operator with strings, Perl tries to convert the strings into numbers; strings that cannot be converted are treated as zero, and all comparisons turn out true.

What you will learn

  • The distinction between numeric and string operators
  • Concatenation and repetition operators
  • Comparison operators
  • Logical operators
  • Sorting operators

Who is this for?

  • Beginners to Perl
  • Those getting unexpected results in comparisons
  • Those coming from other languages expecting a single set of operators

When to use?

It is mandatory to choose the correct operator wherever string comparison is performed; a wrong choice does not throw an error, it silently gives a wrong result.

In sorting operations, numeric and string sorting operators are separate; sorting a list with the wrong operator produces unexpected order.

Common mistakes

Wrong

my $a = "apple";
my $b = "pear";
if ($a == $b) {
  print "same\n";
}

The numeric equality operator tries to convert strings into numbers; both are counted as zero and the condition evaluates to true, printing the wrong result to the screen. If warnings are enabled, Perl reports this but does not throw an error. A separate operator must be used for string comparison.

Correct

my $a = "elma";
my $b = "armut";
if ($a eq $b) {
  print "same\n";
}

Wrong

my $ad = "Ayşe";
my $mesaj = "Merhaba " + $ad;
print $mesaj;

The plus operator is numeric addition in Perl; it tries to convert strings to numbers and the result becomes zero. The dot operator is used for string concatenation. This is the first stumbling block for those coming from JavaScript or Python.

Correct

my $ad = "Ayşe";
my $mesaj = "Merhaba " . $ad;
print $mesaj;

Wrong

my $x = 5;
if ($x = 3) {
  print "three\n";
}

A single equals sign is assignment; when used inside a condition, the value of x is set to three and since the result of the assignment is considered true, the block always runs. Perl allows this syntax, only giving a warning. Comparison must be done with a double equals sign.

Correct

my $x = 5;
if ($x == 3) {
  print "three\n";
}

Frequently asked questions

Which operator is numeric and which one is string?

The symbolic ones are numeric, and the letter-based ones are string. For equality, double equals is numeric and eq is string; for inequality, exclamation-equals is numeric and ne is string. The same rule applies to less-than and greater-than comparisons.

What does the repetition operator do?

The x letter repeats a string a specified number of times; it is useful for printing a separator line or producing alignment spacing. When used with a list, it repeats the list to generate a new list.

What do sorting operators do?

They compare two values and return minus one if smaller, zero if equal, and one if larger. The operator known as the spaceship is used for numeric comparison, and cmp is used for string comparison. They are the foundation of sorting functions.

Summary

Numeric and string operators are separate.

Concatenation is done with a dot, not a plus.

String equality is tested with eq.

Assignment in a condition works silently and gives a warning.

There are separate comparison operators for sorting.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code