Perl Operators
Arithmetic Operators
Basic Arithmetic Operators
perl
my $a = 10;
my $b = 3;
print $a + $b; # Addition: 13
print $a - $b; # Subtraction: 7
print $a * $b; # Multiplication: 30
print $a / $b; # Division: 3.33333333333333
print $a % $b; # Modulus: 1
print $a ** $b; # Exponentiation: 1000Arithmetic Assignment Operators
perl
my $num = 10;
$num += 5; # $num = $num + 5 Result: 15
$num -= 3; # $num = $num - 3 Result: 12
$num *= 2; # $num = $num * 2 Result: 24
$num /= 4; # $num = $num / 4 Result: 6
$num %= 4; # $num = $num % 4 Result: 2
$num **= 3; # $num = $num ** 3 Result: 8Increment and Decrement
perl
my $num = 5;
$num++; # Increment: 6
$num--; # Decrement: 5
# Difference between prefix and postfix
my $a = 5;
my $b = ++$a; # Increment first, then assign: $a=6, $b=6
my $c = 5;
my $d = $c++; # Assign first, then increment: $c=6, $d=5String Operators
String Concatenation
perl
my $str1 = "Hello";
my $str2 = "World";
my $combined = $str1 . " " . $str2; # "Hello World"String Repetition
perl
my $line = "-" x 20; # "--------------------"
my $beep = "a" x 5; # "aaaaa"String Comparison Operators
perl
my $str1 = "apple";
my $str2 = "banana";
print $str1 eq $str2; # Equal: 0 (false)
print $str1 ne $str2; # Not equal: 1 (true)
print $str1 lt $str2; # Less than: 1 (true)
print $str1 gt $str2; # Greater than: 0 (false)
print $str1 le $str2; # Less or equal: 1 (true)
print $str1 ge $str2; # Greater or equal: 0 (false)
# Using cmp returns -1, 0, or 1
print $str1 cmp $str2; # -1 (less than)Comparison Operators
Numeric Comparison
perl
my $a = 10;
my $b = 20;
print $a == $b; # Equal: 0 (false)
print $a != $b; # Not equal: 1 (true)
print $a < $b; # Less than: 1 (true)
print $a > $b; # Greater than: 0 (false)
print $a <= $b; # Less or equal: 1 (true)
print $a >= $b; # Greater or equal: 0 (false)
# Using <=> returns -1, 0, or 1
print $a <=> $b; # -1 (less than)Comparison Operators Summary
| Numeric | String | Description |
|---|---|---|
== | eq | Equal |
!= | ne | Not equal |
< | lt | Less than |
> | gt | Greater than |
<= | le | Less or equal |
>= | ge | Greater or equal |
<=> | cmp | Compare returns -1/0/1 |
Logical Operators
Basic Logical Operators
perl
my $a = 1;
my $b = 0;
print $a && $b; # Logical AND: 0
print $a || $b; # Logical OR: 1
print !$a; # Logical NOT: 0Low-precedence Logical Operators
perl
my $a = 1;
my $b = 0;
print $a and $b; # Logical AND: 0
print $a or $b; # Logical OR: 1
print not $a; # Logical NOT: 0Ternary Operator
perl
my $age = 18;
my $status = $age >= 18 ? "adult" : "minor";
print $status; # "adult"Short-circuit Evaluation
perl
# Logical AND: if first is false, second not evaluated
my $x = 0;
my $y = 10;
my $result = $x && ($y = 20); # $y won't be modified
# Logical OR: if first is true, second not evaluated
my $a = 1;
my $b = 10;
my $result2 = $a || ($b = 20); # $b won't be modified
# Defined-or operator
my $value = $input // "default";Bitwise Operators
perl
my $a = 5; # Binary: 101
my $b = 3; # Binary: 011
print $a & $b; # Bitwise AND: 1 (001)
print $a | $b; # Bitwise OR: 7 (111)
print $a ^ $b; # Bitwise XOR: 6 (110)
print ~$a; # Bitwise NOT: -6
print $a << 1; # Left shift: 10 (1010)
print $a >> 1; # Right shift: 2 (010)Assignment Operators
perl
my $a = 10; # Assignment
my $b = $a; # Copy
$a = $b = 5; # Chained assignment
# Multiple value assignment
my ($x, $y, $z) = (1, 2, 3);Comma Operator
perl
# In scalar context returns last value
my $result = (1, 2, 3); # $result = 3
# In list context returns entire list
my @array = (1, 2, 3); # @array = (1, 2, 3)
# Using in for loops
for (my $i = 0, my $j = 10; $i < 10; $i++, $j--) {
print "$i, $j\n";
}Range Operator
perl
# Numeric range
my @nums = 1..10; # (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
# Character range
my @chars = 'a'..'z'; # ('a', 'b', ..., 'z')
# Conditional range
while (<>) {
print if /start/../end/; # Print lines between /start/ and /end/
}Advanced String Operators
String Concatenation and Assignment
perl
my $str = "Hello";
$str .= " World"; # $str = "Hello World"Smart Matching (String)
perl
use v5.10;
my $name = "Alice";
if ($name ~~ qr/^A/) {
print "Starts with A\n";
}File Test Operators
perl
my $file = "test.txt";
print -e $file; # File exists
print -f $file; # Is regular file
print -d $file; # Is directory
print -r $file; # Is readable
print -w $file; # Is writable
print -x $file; # Is executable
print -s $file; # File size (bytes)
print -z $file; # File is emptyRegex Operators
perl
my $text = "Hello World";
# Match
if ($text =~ /World/) {
print "Found!\n";
}
# No match
if ($text !~ /Python/) {
print "Not found!\n";
}
# Substitution
$text =~ s/World/Perl/; # "Hello Perl"
# Global substitution
$text =~ s/l/x/g; # "Hexxo Perx"Reference Operators
perl
# Backslash creates reference
my $scalar = 42;
my $scalar_ref = \$scalar;
my @array = (1, 2, 3);
my $array_ref = \@array;
my %hash = (key => "value");
my $hash_ref = \%hash;
# Square brackets create anonymous array
my $anon_array = [1, 2, 3];
# Curly braces create anonymous hash
my $anon_hash = {name => "Alice"};Dereference Operators
perl
my $array_ref = [1, 2, 3];
my @array = @$array_ref; # Dereference to array
my $first = $array_ref->[0]; # Arrow operator
my $hash_ref = {name => "Alice"};
my %hash = %$hash_ref; # Dereference to hash
my $name = $hash_ref->{name}; # Arrow operatorOperator Precedence
Precedence from High to Low
- Terms and list operators (left)
->++--(increment/decrement)**(exponentiation)!~\+-(logical not, bitwise not, reference, unary plus/minus)=~!~(matching)*/%x(multiply, divide, modulus, repetition)+-.(add, subtract, concatenation)<<>>(shift)- Named unary operators
<<=>>=ltlegtge(comparison)==!=<=>eqnecmp(equality comparison)&(bitwise AND)|^(bitwise OR, XOR)&&(logical AND)||//(logical OR, defined-or).....(range)?:(ternary operator)=+=-=*=etc. (assignment),=>(comma, fat arrow)- Logical operators (right)
andorxor(low-precedence logical)
Using Parentheses for Clarity
perl
# Use parentheses when precedence is unclear
my $result = ($a + $b) * $c;
my $condition = ($x > 0) and ($y < 10);Practice Examples
Example 1: Calculator
perl
#!/usr/bin/perl
use strict;
use warnings;
print "Enter first number: ";
chomp(my $num1 = <STDIN>);
print "Enter second number: ";
chomp(my $num2 = <STDIN>);
printf "Addition: %d + %d = %d\n", $num1, $num2, $num1 + $num2;
printf "Subtraction: %d - %d = %d\n", $num1, $num2, $num1 - $num2;
printf "Multiplication: %d * %d = %d\n", $num1, $num2, $num1 * $num2;
printf "Division: %d / %d = %.2f\n", $num1, $num2, $num1 / $num2;
printf "Modulus: %d %% %d = %d\n", $num1, $num2, $num1 % $num2;
printf "Power: %d ** %d = %d\n", $num1, $num2, $num1 ** $num2;Example 2: String Operations
perl
#!/usr/bin/perl
use strict;
use warnings;
my $str1 = "Hello";
my $str2 = "World";
my $combined = $str1 . " " . $str2;
print "Concatenation: $combined\n";
my $repeated = $str1 x 3;
print "Repetition: $repeated\n";
print "Length: " . length($combined) . "\n";
if ($str1 lt $str2) {
print "'$str1' comes before '$str2' in dictionary order\n";
}Example 3: Logical Decisions
perl
#!/usr/bin/perl
use strict;
use warnings;
my $age = 25;
my $has_license = 1;
my $has_car = 0;
my $can_drive = $age >= 18 && $has_license;
print "Can drive: " . ($can_drive ? "Yes" : "No") . "\n";
my $can_travel = $has_license || $has_car;
print "Can travel: " . ($can_travel ? "Yes" : "No") . "\n";
my $status = $age >= 18 ? "Adult" : "Minor";
print "Status: $status\n";Summary
In this chapter, we learned Perl operators:
- ✅ Arithmetic operators
- ✅ String operators
- ✅ Comparison operators
- ✅ Logical operators
- ✅ Bitwise operators
- ✅ Assignment operators
- ✅ File test operators
- ✅ Regex operators
- ✅ Operator precedence
Next, we will learn Perl Conditional Statements.