This is part 7 of my series, Basics of ActivePerl. In this part of the series, we talk about some common ActivePerl Operators. We have seen the logical operators. We have also seen the assignment and equal operators. In this part we look at comparison and arithmetic operators.
Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.
Operand
An Operand 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. = is the assignment operator, not the equal operator. The equal operator is, == when dealing with numbers and eq when dealing with strings; and is used only in conditions.
Consider:
$myVar && $hisVar && $herVar
There are three operands in the above expression. So, you can talk of the first, second and third operand.
Comparison Operators
A comparison operator compares the operands on its sides and returns a logical value (true or false) depending on whether the comparison is correct or wrong. If the comparison is correct a logical value of true is returned. If it is wrong, a logical value of false is returned. Another name for Boolean Value is Logical Value, which is either true or false.
The Equal Operator for Numbers
It is ==, typed as a double assignment operator. The equal operator returns true if operands (numbers) are equal, otherwise it returns false.
The Not Equal Operator for Numbers
The Not Equal operator is the opposite of the Equal Operator. The Not Equal operator is, != . It returns true if the operands are not equal, otherwise it returns false. Let us look at some examples:
Read and try the following code:
use strict;
print "Content-Type: text/html\n\n";
my $myVar = 25;
my $hisVar = 30;
if ($myVar != $hisVar)
{
print 'The values of the two variables are not equal.';
}
$myVar is 25, $hisVar is 30. The condition is read like this: If $myVar is not equal to $hisVar, then the if-block will be executed. Since the values of the variables are not equal, ($myVar != $hisVar) returns true.
In the following code, the values of the two variables are equal, so the condition returns false and the if-block is not executed.
use strict;
print "Content-Type: text/html\n\n";
my $myVar = 50;
my $hisVar = 50;
if ($myVar != $hisVar)
{
print 'The values of the two variables are not equal.';
}
Note: The letter O and the digit zero are not the same things. If you type the letter O in place of zero (0) you will not have the right results. The digit zero is found in the number keypad of your keyboard. The letter O is found in the main keyboard area.
The Equal Operator for Strings
It is eq, meaning, equal. The equal operator returns true if operands (strings) are equal, otherwise it returns false. We have seen examples of this.
The Not Equal Operator for Strings
The Not Equal operator is the opposite of the Equal Operator. The Not Equal operator for strings is, ne . It returns true if the operands are not equal, otherwise it returns false. Read and try the following code to illustrate this:
use strict;
print "Content-Type: text/html\n\n";
if ("one" ne "two")
{
print "The strings are not equal.";
}
The Greater Than Operator
The Greater Than operator is, > . It returns true if the left operand is greater than the right operand. In the following example, the left operand is greater than the right operand. So the if-block is executed:
use strict;
print "Content-Type: text/html\n\n";
my $variab1 = 60;
my $variab2 = 70;
if ($variab2 > $variab1)
{
print 'The value of variab2 is greater than the value of variab1.';
}
Read and try the above code.
Greater Than Or Equal - Operator
The Greater Than or Equal operator is, >= (it is the math greater than sign followed by the math equal sign). It returns true if the left operand is greater than or equal to the right operand.
The Less Than Operator
The Less Than Operator is < .It returns true if the left operand is less than the right operand.
The Less Than or Equal - Operator
The Less than or Equal operator is,
Arithmetic Operators
An Arithmetic operator takes one or two numbers as operands (either literals or variables) and returns the answer, similar to what happens in arithmetic.
The standard arithmetic operators are addition (+), subtraction (-), multiplication (*), and division (/). To save time explaining these four operators, just read and try the following examples:
Addition Operator
Code example:
use strict;
print "Content-Type: text/html\n\n";
my $var1 = 20;
my $var2 = 30;
my $var3 = $var2 + $var1;
print $var3;
Subtraction Operator
Code example:
use strict;
print "Content-Type: text/html\n\n";
my $var1 = 20;
my $var2 = 30;
my $var3 = $var2 - $var1;
print $var3;
Multiplication Operator
Code example:
use strict;
print "Content-Type: text/html\n\n";
my $var1 = 20;
my $var2 = 30;
my $var3 = $var2 * $var1;
print $var3;
Note that the multiplication operator is * and not X.
Division Operator
Code example:
use strict;
print "Content-Type: text/html\n\n";
my $var1 = 20;
my $var2 = 30;
my $var3 = $var2 / $var1;
print $var3;
Note that the division operator is, / .
Other operators are the Modulus (%), Increment (++), Decrement (--), and the Negation operators. You have to learn the particular way in which each of these operators behaves (see below).
Modulus Operator
The modulus operator divides the first operand by the second operand and returns the remainder. Read and try the following code:
use strict;
print "Content-Type: text/html\n\n";
my $var1 = 17;
my $var2 = 12;
my $var3 = $var1 % $var2;
print $var3;
The Modulus operator is the percentage sign.
Increment Operator
The Increment Operator is, ++. It works with one operand, not two as the others. The operand has to be a number. When it is placed in front (prefix) of the operand, it behaves in one way. When it is placed after (postfix) the operand it behaves in another way.
Prefix: When it is prefix, it adds 1 to the operand and returns the incremented operand. Read and try the following code:
use strict;
print "Content-Type: text/html\n\n";
my $var1 = 10.5;
my $var2 = ++$var1;
print $var2;
In the code, initially, 10.5 is assigned to var1. Then we have a statement. In the statement you have a new variable, $var2, the assignment operator and then "++$var1". What interest us here is "++$var1", where the increment operator is in front of the variable. The value the increment operator returns is assigned to $var2. If you have tried the code, you would have noticed that the value of $var2 is 11.5. This means, if used prefix, it increments the operand and then returns the incremented operand. Note: in the above code, the final value for $var1 is 11.5 and not 10.5.
Postfix: When it is postfix, it returns the operand before adding 1 to it. The returned value is the original value of the operand. The increased value is the new value of the operand, which is not returned. Read and try the following code.
use strict;
print "Content-Type: text/html\n\n";
my $var1 = 10.5;
my $var2 = $var1++;
print $var2; print '
';
print $var1;
If you have tried the above code, you would have noticed that the value for $var2 is 10.5 and the final value for $var1 is 11.5, confirming that the incrementing took place after the value was returned. The "print '
';" sends a line break HTML element to the browser so that the next result should be displayed one line below the previous one. In ActivePerl, you can have more than one statement in one line, such as in, "print $var2; print '
';".
Decrement Operator
The Decrement operator, -- , behaves like the increment operator with the difference that it subtracts 1 instead of adding.
Negation Operator
This operator is the negative sign, - . It works with one operand (on its right); it negates the operand just like in math. Read and try the following:
use strict;
print "Content-Type: text/html\n\n";
my $var1 = 25;
my $var2 = -$var1;
print $var2;
We have come to the end of this part of the series. Do not forget that the all what we learn in this series is applicable to traditional Perl. You can try the code with traditional Perl (in their operating systems). If you want to try a code sample with traditional Perl, just precede it with something like, #!/usr/bin/perl (see the corresponding Perl documentation).
Let us stop here. Rendezvous in the next part of the series.
Chrys
Published by Chrys Forcha
I have more than 10 years experience in computer programming, software, electronics and telecommunications. I have a First Degree in Electronics and a Master's Degree in Technical Education. As well a... View profile
- True Blood Season 1 ConcludesTrue Blood Season 1 has concluded, with certain story threads tied up, but others set up for True Blood Season 2, to come the HBO next summer. True Blood proves, along with the hit movie Twilight, that blood suckers a...
- True Blood, Season 2, Episode 4 - Shake and FingerpopIn episode 4 of True Blood, season 2, entitled "Shake and Fingerpop," Sookie travels with Bill to Dallas to carry out an assignment from Eric.
- True Blood Season 2, Episode 8 - Timebomb: Air Date 08/09/09Things just keep getting weirder and weirder on True Blood. What happened in True Blood, episode 8? Read and find out!
- More Regular Expressions in PerlThis is an eight part series. This last part gives you extra features in Perl Regular Expressions.
- How to Write an Effective Comparison/Contrast EssayWhen it comes to comparison/contrast writing, a writer is really only limited by his or her imagination. However, following certain tried-and-true steps can help guarantee that the resulting essay or article is effect...
- PHP Comparison and Arithmetic Operators
- Comparison and Arithmetic Operators
- Regular Expressions in Perl for the Novice
- How to Become an MRI Operator
- Avoiding New Operator Failure
- PHP Reference
- True Blood, Season 2, Episode 5 - Never Let Me Go



