# Perl | Operators | Set – 1

Operators are the main building block of any programming language. Operators allow the programmer to perform different kinds of operations on operands. In Perl, operators symbols will be different for different kind of operands(like scalars and string). Operators Can be categorized based upon their different functionality:

- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Ternary Operator

These are used to perform arithmetic/mathematical operations on operands.

**Addition:**‘*+*‘ operator is used to add the values of the two operands. For Example:$a = 5; $b = 10; print $a + $b; Here Result will be 15

**Subtraction:**‘*–*‘ operator is used to subtract right hand operand from left hand operand. For Example:$a = 10; $b = 5; print $a - $b; Here Result will be 5

**Multiplication:**‘***‘ operator is used to multiplies the value on either side of the operator. For Example:$a = 5; $b = 10; print $a * $b; Here Result will be 50

**Division Operator:**‘*/*‘ operator returns the remainder when first operand is divided by the second. For Example:$a = 30; $b = 15; print $a / $b; Here Result will be 3

**Modulus Operator:**‘*%*‘ operator is used to divide left hand operand from right operand and returns remainder. For Example:$a = 10; $b = 15; print $a % $b; Here Result will be 5

**Exponent Operator:**‘****‘ operator is used to perform the exponential(power) calculation on operands. For Example:$a = 2; $b = 3; print $a**$b; Here Result will be 8

**Program:** To demonstrate the airthmetic operators

`# Perl Program to illustrate the Airthmetic Operators ` ` ` `# Operands ` `$a` `= 10; ` `$b` `= 4; ` ` ` `# using airthmetic operators ` `print` `"Addition is: "` `, ` `$a` `+ ` `$b` `, ` `"\n"` `; ` `print` `"Subtraction is: "` `, ` `$a` `- ` `$b` `, ` `"\n"` `; ` `print` `"Multiplication is: "` `, ` `$a` `* ` `$b` `, ` `"\n"` `; ` `print` `"Division is: "` `, ` `$a` `/ ` `$b` `, ` `"\n"` `; ` `print` `"Modulus is: "` `, ` `$a` `% ` `$b` `, ` `"\n"` `; ` `print` `"Exponent is: "` `, ` `$a` `** ` `$b` `, ` `"\n"` `; ` |

*chevron_right*

*filter_none*

**Output:**

Addition is: 14 Subtraction is: 6 Multiplication is: 40 Division is: 2.5 Modulus is: 2 Exponent is: 10000

Relational operators are used for comparison of two values. These operators will return either 1**(true) or nothing(i.e. 0(false))**. Sometimes these operators are also termed as the **Equality Operators**. These operators have the different symbols to operate on strings. To know about Comparison Operators operation on String you can refer to **this**.

**Equal To Operator: ‘==’**Check if two values are equal or not. If equals then return 1 otherwise return nothing.**Not equal To Operator: ‘!=’**Check if the two values are equal or not. If not equal then returns 1 otherwise returns nothing.**Comparison of equal to Operator: ‘< = >’**If left operand is less than right then returns -1, if equal returns 0 else returns 1.**Greater than Operator: ‘>’**If left operand is greater than right returns 1 else returns nothing.**Less than Operator: ‘<'**If left operand is lesser than right returns 1 else returns nothing.**Greater than equal to Operator: ‘>=’**If left operand is greater than or equal to right returns 1 else returns nothing.**Less than equal to Operator: ‘<='**If left operand is lesser than or equal to right returns 1 else returns nothing.

**Program: **To illustrate the Relational Operators in Perl

`# Perl Program to illustrate the Relational Operators ` ` ` `# Operands ` `$a` `= 10; ` `$b` `= 60; ` ` ` `# using Relational Operators ` `if` `(` `$a` `== ` `$b` `) ` `{ ` ` ` `print` `"Equal To Operator is True\n"` `; ` `} ` `else` `{ ` ` ` `print` `"Equal To Operator is False\n"` `; ` `} ` ` ` `if` `(` `$a` `!= ` `$b` `) ` `{ ` ` ` `print` `"Not Equal To Operator is True\n"` `; ` `} ` `else` `{ ` ` ` `print` `"Not Equal To Operator is False\n"` `; ` `} ` ` ` `if` `(` `$a` `> ` `$b` `) ` `{ ` ` ` `print` `"Greater Than Operator is True\n"` `; ` `} ` `else` `{ ` ` ` `print` `"Greater Than Operator is False\n"` `; ` `} ` ` ` `if` `(` `$a` `< ` `$b` `) ` `{ ` ` ` `print` `"Less Than Operator is True\n"` `; ` `} ` `else` `{ ` ` ` `print` `"Less Than Operator is False\n"` `; ` `} ` ` ` `if` `(` `$a` `>= ` `$b` `) ` `{ ` ` ` `print` `"Greater Than Equal To Operator is True\n"` `; ` `} ` `else` `{ ` ` ` `print` `"Greater Than Equal To Operator is False\n"` `; ` `} ` ` ` `if` `(` `$a` `<= ` `$b` `) ` `{ ` ` ` `print` `"Less Than Equal To Operator is True\n"` `; ` `} ` `else` `{ ` ` ` `print` `"Less Than Equal To Operator is False\n"` `; ` `} ` `if` `(` `$a` `<=> ` `$b` `) ` `{ ` ` ` `print` `"Comparison of Operator is True\n"` `; ` `} ` `else` `{ ` ` ` `print` `"Comparison of Operator is False\n"` `; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Equal To Operator is False Not Equal To Operator is True Greater Than Operator is False Less Than Operator is True Greater Than Equal To Operator is False Less Than Equal To Operator is True Comparison of Operator is True

These operators are used to combine two or more conditions/constraints or to complement the evaluation of the original condition in consideration.

**Logical AND:**The ‘**and**’ operator returns true when both the conditions in consideration are satisfied. For example, $a and $b is true when both a and b both are true (i.e. non-zero). You can use also**&&**.**Logical OR:**The ‘**or**’ operator returns true when one (or both) of the conditions in consideration is satisfied. For example, $a or $b is true if one of a or b is true (i.e. non-zero). Of course, it gives result “true” when both a and b are true. You can use also**||****Logical NOT:**The ‘not’ operator will give 1 if the condition in consideration is satisfied. For example, not($d) is true if $d is 0.

**Program:** To demonstrate the working of Logical Operators:

`# Perl Program to illustrate the Logical Operators ` ` ` `# Operands ` `$a` `= true; ` `$b` `= false; ` ` ` `# AND operator ` `$result` `= ` `$a` `&& ` `$b` `; ` `print` `"AND Operator: "` `, ` `$result` `,` `"\n"` `; ` ` ` `# OR operator ` `$result` `= ` `$a` `|| ` `$b` `; ` `print` `"OR Operator: "` `, ` `$result` `,` `"\n"` `; ` ` ` `# NOT operator ` `$d` `= 0; ` `$result` `= not(` `$d` `); ` `print` `"NOT Operator: "` `, ` `$result` `; ` |

*chevron_right*

*filter_none*

**Output:**

AND Operator: false OR Operator: true NOT Operator: 1

These operators are used to perform the bitwise operation. It will first convert the number into bits and perform the bit-level operation on the operands.

**& (bitwise AND)**Takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1. For example$a = 13; // 1101 $b = 5; // 0101 $c = $b & $a; print $c; Here Ouput will be 5 Explanation: $a = 1 1 0 1 $b = 0 1 0 1 --------------- $c = 0 1 0 1 ---------------

**| (bitwise OR)**Takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 any of the two bits is 1. For example$a = 13; // 1101 $b = 5; // 0101 $c = $b | $a; print $c; Here Output will be 13 Explanation: $a = 1 1 0 1 $b = 0 1 0 1 --------------- $c = 1 1 0 1 ---------------

**^ (bitwise XOR)**Takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different. For example$a = 13; // 1101 $b = 5; // 0101 $c = $b ^ $a; print $c; Here Output will be 9 Explanation: $a = 1 1 0 1 $b = 0 1 0 1 ------------- $c = 1 0 0 1 -------------

**~ (Complement Operator)**This is unary operator act as flipping bits. It’s work is to reverse the bits and gives reuslt using 2’s complement form due to a signed binary number.**(<<) Binary Left Shift Operator**will takes two numbers, left shifts the bits of the first operand, the second operand decides the number of places to shift. It performs multiplication of the left operand by the number of times spthe ecified by right operand. For Example:$a = 60; $c = $a << 2; print $c; Output: 240 Explanation: 60 * 2 = 120 ---(1) 120 * 2 = 240 ---(2)

**(>>)Binary Right Shift Operator**will take two numbers, right shifts the bits of the first operand, the second operand decides the number of places to shift. It performs division of the left operand by the number of times specified by right operand. For Example:$a = 60; $c = $a >> 2; print $c; Output: 15 Explanation: 60 / 2 = 30 ---(1) 30 / 2 = 15 ---(2)

**Program:** To demonstrate the working of bitwise operators:

`# Perl Program to illustrate the Bitwise operators ` `#!/usr/local/bin/perl ` `use` `integer; ` ` ` `# Operands ` `$a` `= 80; ` `$b` `= 2; ` ` ` `# Bitwise AND Operator ` `$result` `= ` `$a` `& ` `$b` `; ` `print` `"Bitwise AND: "` `, ` `$result` `, ` `"\n"` `; ` ` ` `# Bitwise OR Operator ` `$result` `= ` `$a` `| ` `$b` `; ` `print` `"Bitwise OR: "` `, ` `$result` `, ` `"\n"` `; ` ` ` `# Bitwise XOR Operator ` `$result` `= ` `$a` `^ ` `$b` `; ` `print` `"Bitwise XOR: "` `, ` `$result` `, ` `"\n"` `; ` ` ` `# Bitwise Complement Operator ` `$result` `= ~` `$a` `; ` `print` `"Bitwise Complement: "` `, ` `$result` `, ` `"\n"` `; ` ` ` `# Bitwise Left Shift Operator ` `$result` `= ` `$a` `<< ` `$b` `; ` `print` `"Bitwise Left Shift: "` `, ` `$result` `, ` `"\n"` `; ` ` ` `# Bitwise Right Shift Operator ` `$result` `= ` `$a` `>> ` `$b` `; ` `print` `"Bitwise Right Shift: "` `, ` `$result` `, ` `"\n"` `; ` |

*chevron_right*

*filter_none*

**Output:**

Bitwise AND: 0 Bitwise OR: 82 Bitwise XOR: 82 Bitwise Complement: -81 Bitwise Left Shift: 320 Bitwise Right Shift: 20

Assignment operators are used to assigning a value to a variable. The left side operand of the assignment operator is a variable and right side operand of the assignment operator is a value.

Different types of assignment operators are shown below:

**“=”(Simple Assignment)**: This is the simplest assignment operator. This operator is used to assign the value on the right to the variable on the left.

Example :$a = 10; $b = 20;

**“+=”(Add Assignment)**: This operator is combination of ‘+’ and ‘=’ operators. This operator first adds the current value of the variable on left to the value on the right and then assigns the result to the variable on the left.

Example :($a += $b) can be written as ($a = $a + $b)

If initially value stored in a is 5. Then ($a += 6) = 11.

**“-=”(Subtract Assignment)**: This operator is combination of ‘-‘ and ‘=’ operators. This operator first subtracts the current value of the variable on left from the value on the right and then assigns the result to the variable on the left.

Example :($a -= $b) can be written as ($a = $a - $b)

If initially value stored in a is 8. Then ($a -= 6) = 2.

**“*=”(Multiply Assignment)**: This operator is combination of ‘*’ and ‘=’ operators. This operator first multiplies the current value of the variable on left to the value on the right and then assigns the result to the variable on the left.

Example :

($a *= $b) can be written as ($a = $a * $b)

If initially value stored in a is 5. Then ($a *= 6) = 30.

**“/=”(Division Assignment)**: This operator is combination of ‘/’ and ‘=’ operators. This operator first divides the current value of the variable on left by the value on the right and then assigns the result to the variable on the left.

Example :($a /= $b) can be written as ($a = $a / $b)

If initially value stored in a is 6. Then ($a /= 2) = 3.

**“%=”(Modulus Assignment)**: This operator is combination of ‘%’ and ‘=’ operators. This operator first modulo the current value of the variable on left by the value on the right and then assigns the result to the variable on the left.

Example :($a %= $b) can be written as ($a = $a % $b)

If initially value stored in a is 6. Then ($a %= 2) = 0.

**“**=”(Exponent Assignment)**: This operator is combination of ‘**’ and ‘=’ operators. This operator first exponent the current value of the variable on left by the value on the right and then assigns the result to the variable on the left.

Example :($a **= $b) can be written as ($a = $a ** $b)

If initially value stored in a is 6. Then ($a **= 2) = 36.

**Program:** To demonstrate the working of Assignment Operators

`# Perl program to demonstrate the working ` `# of Assignment Operators ` `#!/usr/local/bin/perl ` ` ` `# taking two variables & using ` `# simple assignments operation ` `$a` `= 8; ` `$b` `= 5; ` ` ` `# using Assignment Operators ` `print` `"Addition Assignment Operator: "` `, ` `$a` `+= ` `$b` `, ` `"\n"` `; ` ` ` `$a` `= 8; ` `$b` `= 4; ` `print` `"Subtractation Assignment Operator: "` `, ` `$a` `-= ` `$b` `, ` `"\n"` `; ` ` ` `$a` `= 8; ` `$b` `= 4; ` `print` `"Multiplication Assignment Operator: "` `, ` `$a` `*=` `$b` `, ` `"\n"` `; ` ` ` `$a` `= 8; ` `$b` `= 4; ` `print` `"Division Assignment Operator: "` `,` `$a` `/=` `$b` `, ` `"\n"` `; ` ` ` `$a` `= 8; ` `$b` `= 5; ` `print` `"Modulo Assignment Operator: "` `, ` `$a` `%=` `$b` `,` `"\n"` `; ` ` ` `$a` `= 8; ` `$b` `= 4; ` `print` `"Exponent Assignment Operator: "` `, ` `$a` `**=` `$b` `, ` `"\n"` `; ` ` ` |

*chevron_right*

*filter_none*

**Output:**

Addition Assignment Operator: 13 Subtractation Assignment Operator: 4 Multiplication Assignment Operator: 32 Division Assignment Operator: 2 Modulo Assignment Operator: 3 Exponent Assignment Operator: 4096

It is a conditional operator which is a shorthand version of if-else statement. It has three operands and hence the name ternary. It will return one of two values depending on the value of a Boolean expression.

**Syntax:**

condition ? first_expression : second_expression;

**Explanation:**

condition:It must be evaluated to true or false. If the condition is true first_expression is evaluated and becomes the result. If the condition is false, second_expression is evaluated and becomes the result.

**Example:**

`# Perl program to demonstrate the working ` `# of Ternary Operator ` ` ` `$x` `= 5; ` `$y` `= 10; ` ` ` `# To find which value is greater ` `# Using Ternary Operator ` `$result` `= ` `$x` `> ` `$y` `? ` `$x` `: ` `$y` `; ` ` ` `# displaying the output ` `print` `"The Larger Number is: $result"` |

*chevron_right*

*filter_none*

**Output:**

The Larger Number is: 10

**Note: **In Ternary operator the condition can be any expression also which can make by using the different operators like relational operators, logical operators etc.

**Example:**

`# Perl program to demonstrate the working ` `# of Ternary Operator by using expression ` `# as the condition ` ` ` `# here maximum value can be 100 ` `$MAX_VALUE` `= 100; ` ` ` `# suppose user provide value ` `$user_value` `= 444; ` ` ` `# To find which whether user provided ` `# value is staisfying the maximum value ` `# or not by using Ternary Operator ` `$result` `= ` `$user_value` `<= ` `$MAX_VALUE` `? ` `$user_value` `: ` `$MAX_VALUE` `; ` ` ` `# displaying the output ` `# Here it will be MAX_VALUE ` `print` `"$result"` |

*chevron_right*

*filter_none*

**Output:**

100

## Recommended Posts:

- Perl | Operators | Set - 2
- Perl | Useful String Operators
- Perl | String Operators
- Perl | Operators in Regular Expression
- Perl | File Test Operators
- Perl | Basic Syntax of a Perl Program
- Perl Tutorial - Learn Perl With Examples
- Perl vs C/C++
- Perl Hash
- Perl | each() Function
- Perl | int() function
- Perl | oct() Function
- Perl | log() Function
- Perl vs Java
- Perl | last in loop
- Perl | lt operator
- Introduction to Perl
- Perl | qw Operator
- Packages in Perl
- Perl | Hashes

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.