microBlog

Home / 2026-06-17

Data Operations in Perl

Perl, just like any other programming language, uses operators to perform certain tasks. To make sure everything is performed as expected, it is crucial to understand how these operators work.

If understood correctly, usage of such operators are quite easy.

The more we go in depth, the operations that we will work are provided below.

  • Arithmetic Operators
  • Assignment Operators
  • Bit Operators
  • Increment and Decrement
  • Compound Assigment
  • Comparison Operators
  • Logical Operators

Arithmetic Operators

Perl can perform the very basis of Mathematics that they have been teaching us for years. The language itself is actually quite strong when it comes to such operations if used correctly.

Symbol Explanation Program Example
+ Addition, 2 + 2 = 4 $x=2; $y=2; $z=$x+$y; print "$z\n"
- Substraction, 3 - 2 = 1 $x=3; $y=2; $z=$x-$y; print "$z\n"
* Multiplication, 2 * 2 = 4 $x=2; $y=2; $z=$x*$y; print "$z\n"
** Power, 2**4 = 16 $x=2; $y=4; $z=$x**$y; print "$z\n"
/ Division, 4 / 2 = 2 $x=4; $y=2; $z=$x/$y; print "$z\n"
% Modulus, 2 % 4 = 2 $x=2; $y=4; $z=$x%$y; print "$z\n"

The rule of PEMDAS applies here as well. Perl will always perform the operations following this method. To illustrate an example, $opr = 4 * ( 5 + 3); will first add the numbers inside the parentheses and multiply them with 4.

A more complex example would be $opr = 4 ** (5 % (8-6));, Perl will go from inside out with the nested parentheses.

Example Program 1

#!/usr/bin/perl

print "Content-type: text/html\n \n";
$numval = 6;
$powval = $numval ** 3;
print ("$powval\n");

Example Program 2

#!/usr/bin/perl

print "Content-type: text/html\n \n";
$valx = -3 ** 3;
$valy = -(3 ** 3);
$valz = (-3) ** 3;
print ("$valx\n");
print ("$valy\n");
print ("$valz\n")

Example Program 3

#!/usr/bin/perl

print "Content-type: text/html\n \n";
$val = 1234;
print("First value ", $val, "\n");
$val = 12345;
print("Second value ", $val, "\n");
$val = 123456;
print("Third value ", $val, "\n");

Assignment Operators

Assignment operators in Perl are used to assign values to variables. The most basic one is =, but Perl also provides operators that combine assignment with another operation.

Symbol Explanation Program Example
= Assigns the value on the right to the left $x = 5;
+= Adds and assigns, $x = $x + $y $x += $y;
-= Subtracts and assigns, $x = $x - $y $x -= $y;
*= Multiplies and assigns, $x = $x * $y $x *= $y;
/= Divides and assigns, $x = $x / $y $x /= $y;
%= Modulus and assigns, $x = $x % $y $x %= $y;
**= Raises to power and assigns $x **= $y;
.= Concatenates strings and assigns $x .= $y;

These operators exist mostly for convenience, so instead of writing $x = $x + 5;, we can shorten it to $x += 5;. The result is identical, but the code is easier to read once you're used to the shorthand.

Example Program 1

#!/usr/bin/perl
print "Content-type: text/html\n \n";
$total = 10;
$total += 5;
print ("$total\n");
$total -= 3;
print ("$total\n");
$total *= 2;
print ("$total\n");

Example Program 2

#!/usr/bin/perl
print "Content-type: text/html\n \n";
$name = "Perl";
$name .= " Language";
print ("$name\n");

Bit Operators

Bit operators work directly on the binary representation of numbers. They are less commonly used in everyday scripting, but understanding them is useful when working with flags, masks, or low-level data manipulation.

Symbol Explanation Program Example
& Bitwise AND $x=6; $y=3; $z=$x & $y; print "$z\n"
<code>&#124;</code> Bitwise OR `$x=6; $y=3; $z=$x \
^ Bitwise XOR $x=6; $y=3; $z=$x ^ $y; print "$z\n"
~ Bitwise NOT (complement) $x=6; $z=~$x; print "$z\n"
<< Bitwise left shift $x=2; $z=$x<<2; print "$z\n"
>> Bitwise right shift $x=8; $z=$x>>2; print "$z\n"

To understand these, it helps to think in binary. For example, 6 is 110 and 3 is 011 in binary. Applying & compares each bit position and only keeps a 1 where both numbers have a 1, giving 010, which is 2.

Example Program 1

#!/usr/bin/perl
print "Content-type: text/html\n \n";
$x = 6;
$y = 3;
print ("AND: ", $x & $y, "\n");
print ("OR: ", $x | $y, "\n");
print ("XOR: ", $x ^ $y, "\n");

Example Program 2

#!/usr/bin/perl
print "Content-type: text/html\n \n";
$x = 2;
$shifted_left = $x << 3;
$shifted_right = $shifted_left >> 1;
print ("$shifted_left\n");
print ("$shifted_right\n");

Increment and Decrement

Perl provides shorthand operators to increase or decrease a variable's value by one. These are especially useful in loops.

Symbol Explanation Program Example
++ Increases the value by 1 $x++; or ++$x;
-- Decreases the value by 1 $x--; or --$x;

The placement of ++ and -- matters. When placed before the variable (++$x), the value is incremented before it is used in the expression. When placed after ($x++), the value is used first, then incremented.

Example Program 1

#!/usr/bin/perl
print "Content-type: text/html\n \n";
$x = 5;
print ($x++, "\n");
print ($x, "\n");

Example Program 2

#!/usr/bin/perl
print "Content-type: text/html\n \n";
$y = 5;
print (++$y, "\n");
print ($y, "\n");

Compound Assignment

Compound assignment is closely tied to the assignment operators covered earlier, combining an arithmetic or bitwise operation with assignment in a single step. Perl supports this for nearly all its operators.

Symbol Explanation Program Example
&= Bitwise AND and assign $x &= $y;
<code>&#124;=</code> Bitwise OR and assign <code>$x
^= Bitwise XOR and assign $x ^= $y;
<<= Left shift and assign $x <<= $y;
>>= Right shift and assign $x >>= $y;

Example Program

#!/usr/bin/perl
print "Content-type: text/html\n \n";
$flags = 6;
$mask = 3;
$flags &= $mask;
print ("$flags\n");

Comparison Operators

Comparison operators are used to compare two values. Perl has two separate sets: one for numbers and one for strings, since == will not work correctly on string values.

Symbol Explanation Program Example
== Equal to (numeric) $x==$y
!= Not equal to (numeric) $x!=$y
> Greater than $x>$y
< Less than $x<$y
>= Greater than or equal to $x>=$y
<= Less than or equal to $x<=$y
eq Equal to (string) $x eq $y
ne Not equal to (string) $x ne $y

Example Program

#!/usr/bin/perl
print "Content-type: text/html\n \n";
$x = "apple";
$y = "banana";
if ($x eq $y) {
    print ("Equal\n");
} else {
    print ("Not Equal\n");
}

Logical Operators

Logical operators combine multiple conditions into a single expression, commonly used inside if statements or loops.

Symbol Explanation Program Example
&& Logical AND $x && $y
`\ \ `
! Logical NOT !$x
and Logical AND (low precedence) $x and $y
or Logical OR (low precedence) $x or $y

Example Program

#!/usr/bin/perl
print "Content-type: text/html\n \n";
$age = 20;
$has_id = 1;
if ($age >= 18 && $has_id) {
    print ("Access granted\n");
} else {
    print ("Access denied\n");
}