Domain Name Registration

www.
Local: 1-910-321-1300 | Sales: 1-800-878-4084 | Support: 1-877-209-5184

Products & Services


 
hosting award
Viewing: Help Desk > Tutorials > PERL Tutorial > Chapter 4

PERL Tutorial [chapter 4]


What are the PERL Operators?
Before we can go much further, we will need to look at how operators are used in Perl, and what they are. These will be important in the upcoming sections where comparison, logic, and some mathematics will be used in conditional blocks. So, let's take a look at the Perl Operators.

Some Important Notes

In Perl, there are different versions of the operators for numbers and strings. For instance, if you want to compare a number, you would use a traditional symbol such as <, >, and so on. However, when you compare two strings, the less-than and greater-than signs are not used. Instead, a special version is used to compare strings. Less-than would be the two letters lt and greater-than would be gt. In the lists that follow, there will be separate listings for numerical and string operators where this is necessary.

Arithmetic Operators

These operators are used to perform mathematical calculations on numbers. Keep in mind though, they are not used to combine strings. There are some special string operators for this. Note that the assignment operator does work both ways, we used it to assign values to variables in the last section. Here is the list:

Operator Description
+ Addition - finds the sum of 2 values
- Subtraction, Negative Numbers, Unary Negation - finds the difference
* Multiplication - Multiplies 2 values
/ Division - Divides one value by
% Modulus - Divides one value by another value and returns only the remainder ion the result.
** Exponent

To use these, you will place them in your statements like a mathematical expression. So, if you want to store the sum of two variables in a third variable, you would write something like this:

$adrevenue=20;
$sales=10;
$total_revenue=$adrevenue+$sales;

You can use the other arithmetic operators in the same way, it is quite similar to other programming languages.

Assignment Operators

We have already used the = sign as an assignment operator to assign values to a variable. You can also use the = sign with another arithmetic operator to perform a special type of assignment. You can precede the = sign with the + operator, for example:

$revenue+=10;

What this does is create a shorthand for writing out the following statement:
$revenue=$revenue+10;

It takes the variable $revenue and assigns it the value of $revenue (itself) plus 10. So, if you had an initial value for $revenue set at 5:

$revenue=5;
$revenue=$revenue+10;

After these statements, $revenue is 15. It added 10 to the value it had before the new assignment.

The others work the same way, but perform the various different operations. Here a list of the arithmetic operators we used above when we place them with the assignment operator:

Operator Description
= Normal Assignment
+= Add and Assign
-= Subtract and Assign
*= Multiply and Assign
/= Divide and Assign
%= Modulus and Assign
**= Exponent and Assign

Remember, these are used for the sake of typing less. You can write the statements out the long way if it makes it more understandable when you read your code.

Increment/Decrement

Another shorthand method is to use the increment and decrement operators, rather than writing out something like this:

$revenue=$revenue+1;

You can simply write something like this:
$revenue++;

However, using these operators you must remember that you could also write something like this:

++$revenue;

If you place the ++ before the variable name, it the variable adds one to itself before it is used or evaluated. For example, if you write:

$revenue=5;
$total= ++$revenue + 10;

The $revenue variable is incremented before it is used in the calculation, so it is changed to 6 before 10 is added to it. Thus, $total turns out to be 16 here.

If you want to increment the variable after it is used, you use the ++ after the variable name:

$revenue=5;
$total= $revenue++ + 10;

This way $total is only 15 because $revenue is used before being incremented, so it stays at 5 for this expression. If you use $revenue again after this, it will have a value of 6.

With that in mind, here is the short list of the two operators:

Operator Description
++ Increment (Add 1)
-- Decrement (Subtract 1)

The -- operator works the same way as ++, but it subtracts one from the value of the variable (decrements it).

Adding Strings

Like I mentioned at the beginning of this section, there are different operators for strings under certain conditions. If you want to put two strings together (also called concatenate), you will want to use the dot operator. Unlike C and JavaScript (where it is used with objects), the dot operator in Perl concatenates two strings.

For example, if you want to place two strings together, you could do this:

$full_string="light" . "house";

This would make $full_string have the value of lighthouse. This is more useful if you are using variables for this:

$word1="light";
$word2="house";
$full_string=$word1 . $word2;
print "If I had a $word1 and a $word2, would I be able to make a $full_string?";

It prints out the following sentence:

If I had a light and a house, would I be able to make a lighthouse?

This can also be used with the assignment operator to do what we did with numbers earlier. In this case, it gives the string the value of itself put together with another string:

$word1="light";
$full_string=$word1 . "house";

Of course, we again get the value of lighthouse for the $full_string variable. Here is the list for these two string operators:

Operator Description
. Concatenate Strings
.= Concatenate and Assign

After all of that information in the last section, you might think there couldn't be any more. Well, there is more-- but these are a little more interesting.

Numeric Comparison

These operators are used to compare two numbers, but not to compare strings. We'll get to those next. These operators are typically used in some type of conditional statement that executes a block of code or initiates a loop. We'll get to the conditional statements in the next section, but to introduce this we will use the beginning of an if () condition. Before that, let's look at the list:

Operator Description
== Equal to
!= Not Equal to
> Greater than
< Less than
>= Greater than or Equal to
<= Less than or Equal to

So, suppose you want to execute some code only if one number is equal to another. You would use the if () condition with the == operator above:

$money=5;
if ($money==5)
{
....more code....
}

Since money is equal to 5, it would execute the code you place between the curly brackets. It works the same way if you use one of the other operators:

$money=5;
if ($money<3)
{
....more code....
}

This time it would not go through, as 5 is not less than 3-- the value of $money.

String Comparison

These are similar to the numerical comparisons, but they work with strings. We will note a few differences in the way these work after the list below:

Operator Description
eq Equal to
ne Not Equal to
gt Greater than
lt Less than
ge Greater than or Equal to
le Less than or Equal to

Strings are equal if they are exactly the same. So, "cool" and "cool" are equal, but "cool" and "coolz" are not. Here is a string equality example:

$i_am="cool";
if ($i_am eq "cool")
{
....more cool code....
}

The greater-than and less-than operators compare strings using alphabetical order. Something that starts with "a" is greater than something that starts with "c". Also, small letters are greater than capital letters. Thus, "hello" is greater than "Hello". So, that is how it will compare it. Here is a sample:

$i_am="delightful";
if ($i_am lt "cool")
{
print "You are not very cool, dude.";
}

Logical Operators

These are often used when you need to check more than one condition. Here they are:

Operator Description
&& AND
|| OR
! NOT

So, if you want to see if a number is less than or equal to 10, and also greater than zero:

$number=5;
if (($number <= 10) && ($number > 0))
{
...code....
}

Notice the nested parentheses. Since we are checking for two conditions, we want to be sure the comparison is done first. Thus, they have their own sets of parentheses within the parentheses for the if () condition. Afterward, it checks to see if both conditions are true. Again, more on the conditionals in the next section.