Perl supports almost the same operators as other computer languages. Operators allow a computer language to perform actions on operands. As in every computer language, perl operators also have a certain precedence.
As we have already seen, there is one operator which we have already encountred, the assignment operator (=). This operator assigns a value from one variable to another:
string1 = string2;
The above code assigns the value of string2 to string1. We will now continue our operator discussion with the binary and unary aritmatic operators.
Operator | Description |
op1 + op2 | The addtion operator will add two numbers. |
op1 - op2 | The subtraction operator will subtract two numbers |
op1 * op2 | The multiplication operator will multiply two numbers |
op1 / op2 | The division operator will divide two numbers |
op1 %op2 | The modulus operator will return the remainder of the division of two integer operands.. |
op1 xx op2 | The exponentiation operator will raise op1 to the power of op2. |
++op1 | The pre-increpment operator will increase the value of op1 first then assign it. |
op1++ | The post-increment operator will increase the value of op1 after it is assigned. |
--op1 | The pre-decrement operator will decrease the value of op1 before it is assigned. |
op1-- | The post-decrement operator will decrease the value of op1 after it is assigned. |
The logical operators are used mainly to control the flow of the program. Below are some of the logical operators that perl supports:
Operator | Description |
&& | The AND operator compares two values, it will return true only if both values are true |
|| | The OR operator compares two values, it will return true only if at least one value is true |
! | The NOT operator will negate a value. |
op1 == op2 | The equals operator will compare the equality of two numerical values. |
op1 != op2 | This numerical operator will true if both values are not equal |
op1 < op2 | This numerical operator will return true if op1 is less than op2 |
op1 > op2 | This numerical operator will return true if op1 is greater than op2 |
op1 <= op2 | This numerical operator will return true if op1 is less than or equal to op2 |
op1 >= op2 | This numerical operator will return true if op1 is greather than or equal to op2 |
op1 <=> op2 | This numerical operator will return -1 if op1 is less than op2, it
will return 0 if
they are equal, and it will return 1 if op2 is greater than op1. |
op1 eq op2 | This string operator will return true if the two strings are equal |
op1 ne op2 | This string operator will return true if both strings are not equal |
op1 lt op2 | This string operator will return true if op1 is less than op2 |
op1 le op2 | This string operator will return true if op1 is less than or equal to op2 |
op1 gt op2 | This string operator will return true if op1 is greater than op2 |
op1 ge op2 | This string operator will return true if op1 is greather than or equal to op2 |
op1 cmp op2 | This string operator functions in the same manner as the numerical <=> operator above. |
There are other operators which can also be used in perl. We have the concatenation operator for strings (.):
string1 . string2
The above code will concatenate string one with string 2.
We also have the repetition operator (x), this operator will repeat a string a certain number of times specified:
string1 x 2;
The above code will repeate string1 two times.
The range operator will allow us to use ranges, in arrays or patters.:
@array = (1..50);
The above code will assign 50 elements to the array.
As we have already seen, we have a wide variety of operators to work with scalar values, but if we wanted to work with arrays we could do what is called array slicing. Let's say that we have an array with 10 values. If we want to assign values 5 and 6 to two scalar values we could do the following:
($one,$two) = @array[4,5]; #remember that arrays start at subscript 0.
In the above code we just spliced two values from the array. When we do array splicing we use the @ sign followed by the array name followed by brackets and the subscripts that you want seperated by commas, so instead of having two lines for the above code we have one. We need to enclose the two scalar values in parenthesis in order to group them.
![]() |
![]() |
![]() |