This tutorial explains about Perl assignment Operators

Perl assignments operators

Assignment operators perform expression and assign the result to a variable.

  • Simple assignment
  • Assignment Addition
  • Assignment Subtraction
  • Assignment Multiplication
  • Assignment Division
  • Assignment Modulus
  • Assignment Exponent

Perl Assignment Operator

For example, We have operand1(op1)=6 and operand2(op2)=2 values.

NameSymbolDescriptionResult
Simple Assign=assign right side value to left side operand$op1=2 and $op1 is 2
Assign Add+=It adds left and right values, results assigned to the left operand$op1+=$op2 and $op1 is6+2=8
Assign Multiply*=Multiple left with right operand, assign the result to left operand$op1*=$op2 and $op1 is 6*2=12
Assign subtraction-=subtraction left with right operand, assign the result to left operand$op1-=$op2 and $op1 is 6-2=4
AssignDivide/=Divide left with right operand, assign the result(quotient) to left operand
Assign Modulus%=Divide left with right operand, assign the remainder to left operand$op1%=$op2 and $op1 is 6%2=0
Assign Exponent**=exponent(power) of left with a right operand, assign the result to left operand$op1**=$op2 and $op1 is 6**2=36

Here is a Perl Assignment Operator Example

my $op1= 14;
my $op2 = 2;
$op1= $op2;
print "Simple Assignment example: $op1 \n";


my $op1= 14;
my $op2 = 2;
$op1 += $op2;
print "Assignment Add Example: $op1 \n";

$op1= 14;
$op2 = 2;
$op1-= $op2;
print "Assignment Subtraction example: $op1 \n";

my $op1= 14;
my $op2 = 2;
$op1*= $op2;
print "Assignment Multiplication example: $op1 \n";


my $op1= 14;
my $op2 = 2;
$op1/= $op2;
print "Assignment Division example: $op1 \n";

my $op1= 14;
my $op2 = 2;
$op1%= $op2;
print "Assignment Modulus example: $op1 \n";

my $op1= 14;
my $op2 = 2;
$op1**= $op2;
print "Assignment Exponent example: $op1 \n";

Output:

Simple Assignment example: 2
Assignment Add Example: 16
Assignment Subtraction example: 12
Assignment Multiplication example: 28
Assignment Division example: 7
Assignment Modulus example : 0
Assignment Exponent example: 196

What Assignment operators are used in Perl scripts?

Arithmetic operators with assignments in Perl allow you to do arithmetic calculations on operands, and assign the result to a left operand. Addition(+=),Multiplication(*=),Subtraction(-=), Division(/=), Modulus(%=), Exponent(power (**=)) operators exists for this.

What are Assignment operators in Perl

there are 6 operators in Per for Assignment operators. Addition(+=),Multiplication(*=),Subtraction(-=), Division(/=), Modulus(%=), Exponent(power (**=)) operators exists for this.