TExcelTAD
Home › Perl › Perl Operators

Perl Operators

Perl has a rich set of operators. The key idea to remember: numbers and strings have separate comparison operators, because a scalar can be either one.

Comparison operators

Meaning Numeric String
equal == eq
not equal != ne
less than < lt
greater than > gt
less or equal <= le
greater or equal >= ge
three-way compare (-1, 0, 1) <=> cmp
perl · try it yourself
use strict;
use warnings;

print "10 < 9 numerically?  ", (10 < 9 ? "true" : "false"), "\n";
print "10 lt 9 as strings?  ", ("10" lt "9" ? "true" : "false"), "\n";
print "3 <=> 7   = ", 3 <=> 7, "\n";
print "7 <=> 7   = ", 7 <=> 7, "\n";
print "'b' cmp 'a' = ", "b" cmp "a", "\n";
10 < 9 numerically?  false
10 lt 9 as strings?  true
3 <=> 7   = -1
7 <=> 7   = 0
'b' cmp 'a' = 1

As strings, "10" sorts before "9" because the character 1 comes before 9. The three-way operators <=> (the "spaceship") and cmp are what sort uses.

Logical operators

Operator Low-precedence form Meaning
&& and true if both sides are true
|| or true if either side is true
! not negation
// defined-or: left side if it is defined, else right side

Logical operators short-circuit and return the last value they evaluated, not just 1 or "". That makes them handy for defaults:

perl · try it yourself
use strict;
use warnings;

my $nickname = "";
my $port;

my $display = $nickname || "anonymous";   # "" is false, so use the default
my $chosen  = $port // 8080;               # undef, so use the default
my $zero    = 0;
my $kept    = $zero // 99;                 # 0 is defined, so keep it

print "$display $chosen $kept\n";
anonymous 8080 0

Use // when 0 or "" are valid values that should be kept, and || when any false value should be replaced.

The word forms and, or and not have very low precedence, which makes them ideal for control flow, as in the classic idiom open(...) or die "...";.

Assignment operators

Most binary operators have an assignment form: +=, -=, *=, /=, **=, %=, .=, x=, ||=, //=, &&=.

perl · try it yourself
use strict;
use warnings;

my $balance = 100;
$balance -= 30;
$balance *= 2;
print "Balance: $balance\n";

my $bar = "#";
$bar x= 8;
print "$bar\n";

my %config;
$config{color} //= "blue";   # set only if not yet defined
$config{color} //= "red";    # already defined: unchanged
print "Color: $config{color}\n";
Balance: 140
########
Color: blue

Increment on strings

++ has a special "magic" behaviour on strings that look like letters followed by digits: it increments them like an odometer.

perl · try it yourself
use strict;
use warnings;

my $id = "aa9";
$id++;
print "$id\n";

my $col = "Az";
$col++;
print "$col\n";

my $ticket = "zz";
$ticket++;
print "$ticket\n";
ab0
Ba
aaa

The ternary and range operators

COND ? A : B picks one of two values. The range operator .. builds a list of consecutive values and works for letters too:

perl · try it yourself
use strict;
use warnings;

my $score = 72;
my $result = $score >= 60 ? "pass" : "fail";
print "Result: $result\n";

my @digits  = (1 .. 5);
my @letters = ('a' .. 'e');
print "@digits | @letters\n";
Result: pass
1 2 3 4 5 | a b c d e

Precedence in brief

From high to low, the most common ones: **, then ! and unary minus, then * / % x, then + - ., then comparisons, then &&, ||, //, then ?:, then assignment, then not, and, or. When in doubt, add parentheses.

Test yourself

What does "10" lt "9" return?

  1. false, because 10 > 9
  2. true, because the strings are compared character by character
  3. an error
  4. undef

Given my $n = 0;, what is $n // 5?

  1. 5
  2. 0
  3. undef
  4. an error

What does the spaceship operator 3 <=> 8 return?

  1. 1
  2. 0
  3. -1
  4. 5