Perl Tutorial
Fundamentals
Input and Output
Control Flow
Arrays and Lists
Hash
Scalars
Strings
Object Oriented Programming in Perl
Subroutines
Regular Expressions
File Handling
Context Sensitivity
CGI Programming
Misc
Perl offers a rich set of operators for various operations, including arithmetic, comparison, logical operations, and more. This tutorial will walk you through the major operators in Perl and how to use them.
These are used for mathematical operations:
+
: Addition-
: Subtraction*
: Multiplication/
: Division%
: Modulus (remainder after division)**
: ExponentiationUsed for numeric comparison:
==
: Equal to!=
: Not equal to<
: Less than>
: Greater than<=
: Less than or equal to>=
: Greater than or equal toFor string comparison:
eq
: Equal tone
: Not equal tolt
: Less thangt
: Greater thanle
: Less than or equal toge
: Greater than or equal to&&
: Logical AND||
: Logical OR!
: Logical NOTand
: Low precedence logical ANDor
: Low precedence logical ORnot
: Low precedence logical NOT=
: Assign+=
: Add and assign-=
: Subtract and assign*=
: Multiply and assign/=
: Divide and assign%=
: Modulus and assign&
: Bitwise AND|
: Bitwise OR^
: Bitwise XOR~
: Bitwise NOT<<
: Left shift>>
: Right shift.
: Concatenate stringsx
: Repeat stringExample:
print "hello" . "world"; # helloworld print "hello" x 3; # hellohellohello
..
: Creates a range of values.Example:
@numbers = (1..5); # (1, 2, 3, 4, 5) @letters = ('a'..'e'); # (a, b, c, d, e)
? :
: Ternary conditional operatorExample:
$age < 18 ? "minor" : "adult";
These are unary operators that test attributes of a file:
-e
: File exists-z
: File is empty-s
: File size (returns size or undef if doesn't exist)-r
: File is readable-w
: File is writable-x
: File is executable-f
: Regular file (not a directory or device file)-d
: Directory
... and many more.Example:
if (-e $filename) { print "$filename exists.\n"; }
..
: Flip-flop operator in scalar context.<>
: Diamond operator to read from file or standard input.Perl's operator set is diverse and flexible, providing tools for a variety of operations, from basic arithmetic to file tests. Familiarity with these operators is key to writing efficient and readable Perl code.
Arithmetic operators in Perl:
my $sum = 5 + 3; # Addition my $difference = 7 - 2; # Subtraction my $product = 4 * 6; # Multiplication my $quotient = 10 / 2; # Division my $remainder = 17 % 4; # Modulus
Comparison operators in Perl:
my $result1 = (10 == 5); # Equal to my $result2 = (7 != 3); # Not equal to my $result3 = (8 > 6); # Greater than my $result4 = (4 < 9); # Less than my $result5 = (12 >= 10); # Greater than or equal to my $result6 = (2 <= 5); # Less than or equal to
Logical operators in Perl:
my $and_result = ($condition1 && $condition2); # Logical AND my $or_result = ($condition1 || $condition2); # Logical OR my $not_result = !($condition); # Logical NOT
Bitwise operators in Perl:
my $bitwise_and = $num1 & $num2; # Bitwise AND my $bitwise_or = $num1 | $num2; # Bitwise OR my $bitwise_xor = $num1 ^ $num2; # Bitwise XOR my $bitwise_not = ~$num; # Bitwise NOT
String operators in Perl:
my $concatenation = $str1 . $str2; # String concatenation my $repetition = $str x 3; # String repetition
Assignment operators in Perl:
my $num = 10; $num += 5; # Addition assignment (equivalent to $num = $num + 5) $num -= 3; # Subtraction assignment $num *= 2; # Multiplication assignment $num /= 4; # Division assignment
Ternary operator in Perl:
my $result = ($condition) ? "True" : "False";
Precedence of operators in Perl:
my $result = 2 + 3 * 4; # Precedence: Multiplication has higher precedence than addition
Overloading operators in Perl:
package MyNumber; use overload '+' => \&add_numbers; sub new { my ($class, $value) = @_; return bless { value => $value }, $class; } sub add_numbers { my ($self, $other, $swap) = @_; return $swap ? $other + $self->{value} : $self->{value} + $other; } my $num1 = MyNumber->new(5); my $num2 = MyNumber->new(3); my $result = $num1 + $num2;