Broad Network


Perl Assignment Operators

Perl Operators – Part 1

Perl Course

Foreword: Addition and subtraction symbols are examples of operators in Perl. Perl has many other operators that do not have similarities with mathematics. In this part of the series, I talk about Perl assignment operators.

By: Chrysanthus Date Published: 12 Oct 2015

Introduction

This is part 1 of my series, Perl Operators. Addition and subtraction symbols are examples of operators in Perl. Perl has many other operators that do not have similarities with mathematics. In fact you should learn Perl operators as they are given without making much similarities to mathematics.  In this part of the series, I talk about Perl assignment operators.

If you are an old programmer, you can read the series in any order or you can read just the part, which is related to a problem you currently have at your job site. If you are new to programming or you studied programming and have not been practicing, then you should read the whole series, in the order given.

I use ActivePerl in the series. If you are using traditional Perl, then begin your program with something like, #!/usr/bin/perl.

Pre-Knowledge
This series is part of my volume, Perl Course. At the bottom of this page, you have links to the different series, you should have read before coming here.

Operand/Argument
With Perl operators, an operand or argument is a variable or a literal (value) associated with an operator. Consider,

    $myVar = 30;

$myVar is a left operand and 30 is a right operand to =. = is an example of an operator called, the assignment operator, not the equal operator (details below).

Consider:

     myVar && hisVar && herVar

There are three operands in the above expression. So, you can talk of the first (myVar), second (hisVar) and third operand (herVar). &&, that is 2 ampersands, form another operator; I talk about this later.

In the Perl manual, an operand is called an argument. For the rest of this series, I use the word, argument, in place of operand.

Binary and Unary Operators
A binary operator needs two arguments to work with: one on its left and one on its right. A unary operator needs only one argument to work with, placed on its left or on its right (see later).

Assignment Operators
Assignment operators are:

=  *=  /=  %=  +=  -=

I explain each of these operators in this part of the series.

Ordinary Assignment
The ordinary assignment operator is, =. The following statement illustrates an example:

    $myInt = 35;

We say the integer 35 is assigned to the variable, $myInt. The left argument to = is $myInt. The right argument is 35.

For the ordinary assignment, the right argument may be another variable or even a function call that returns a value.

E1 op= E2 Expressions
In the following generalized statement, E1 is a variable, op is an operator and E2 is another variable; = is the ordinary assignment operator:

    E1 op= E2

This generalized statement that involves the simple assignment operator is equivalent to,

    E1 = E1 op E2

The rest of the assignment operators in the list above follow this generalized rule as explained below. The rest of the assignment operators are each made up of an operator you might already know and the ordinary assignment operator.

The *= Operator
Consider the following statement:

    $int1 = $int1 * $int2;

Here, $int1 and $int2 are variables. Note that $int1 is found in both the left argument and the right argument ($int1 * $int2), of the ordinary assignment operator. You also have the multiplication operator, *. In the above statement the result of multiplying $int1 by $int2 is assigned to $int1. The statement can be re-written as:

    $int1 *= $int2;

Also, $int2 can be replaced by a number as in,

    $int1 *=3;

The /= Operator
Consider the following statement:

    $int1 = $int1 / $int2;

Here, $int1 and $int2 are variables. Note that $int1 is found in both the left argument and the right argument, of the ordinary assignment operator. You also have the division operator, /. In the above statement the result of dividing $int1 by $int2 is assigned to $int1. The statement can be re-written as:

    $int1 /= $int2;

Also, $int2 can be replaced by a number as in,

    $int1 /=4;

The following program illustrates this:

use strict;

        my $int1 = 20;
        my $int2 = 4;

        $int1 /= $int2;
        print $int1;

The %= Operator
Consider the following statement:

    $int1 = $int1 % $int2;

Here, $int1 and $int2 are variables. Note that $int1 is found in both the left argument and right argument. You also have the modulus operator, %. In the above statement the remainder of dividing $int1 by $int2 is assigned to $int1. The statement can be re-written as:

    $int1 %= $int2;

The += Operator
Consider the following statement:

    $int1 = $int1 + $int2;

Here, $int1 and $int2 are variables. Note that $int1 is found in both the left argument and the right argument of the ordinary assignment operator. You also have the addition operator, +. In the above statement the result of adding $int1 to $int2 is assigned to $int1. The statement can be re-written as:

    $int1 += $int2;

The -= Operator
Consider the following statement:

    $int1 = $int1 - $int2;

Here, $int1 and $int2 are variables. Note that $int1 is found in both the left argument and the right argument of the ordinary assignment operator. We also have the subtraction operator, -. In the above statement the result of subtracting $int2 from $int1, is assigned to $int1. The statement can be re-written as:

    $int1 -= $int2;

Right Associativity
If you have consecutive assignment operators, the one on the right will be evaluated first, followed by the one next to it coming to the left, followed by the one next to it, leftward, and so on. Consider the following code:

use strict;

        my $int1 = 1;
        my $int2 = 2;

        my $int3 = $int2 = $int1;

        print $int3, "\n";
        print $int2, "\n";
        print $int1, "\n";

The output is:

1
1
1

The statement of interest in the code is:

        my $int3 = $int2 = $int1;

Since any assignment operation in Perl is made right-associative, $int2 = $int1 is evaluated first, assigning the value of $int1, which is 1, to $int2. Next, $int3 = $int2 is evaluated, assigning the value of $int2, which is now 1 to $int3. And all three variables end up having the value of 1.

Well, let us take a break here and continue in the next part of the series.

Chrys

Related Links

Perl Basics
Perl Data Types
Perl Syntax
Perl References Optimized
Handling Files and Directories in Perl
Perl Function
Perl Package
Perl Object Oriented Programming
Perl Regular Expressions
Perl Operators
Perl Core Number Basics and Testing
Commonly Used Perl Predefined Functions
Line Oriented Operator and Here-doc
Handling Strings in Perl
Using Perl Arrays
Using Perl Hashes
Perl Multi-Dimensional Array
Date and Time in Perl
Perl Scoping
Namespace in Perl
Perl Eval Function
Writing a Perl Command Line Tool
Perl Insecurities and Prevention
Sending Email with Perl
Advanced Course
Miscellaneous Features in Perl
Perl Two-Dimensional Structures
Advanced Perl Regular Expressions
Designing and Using a Perl Module
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course

NEXT

Comments

Become the Writer's Fan
Send the Writer a Message