Perl

Perl

Perl operators provide values to their operands like numeric, Boolean or string, etc. To choose an appropriate operator, you need to know the value of operands.

We'll discuss following type of operators:

- Numeric operators
- String operators
- Logical operators
- Bitwise operators
- Special operators
- Comparison operators
- Assignment operators

Numeric operators are the standard arithmetic operators like addition (+), subtraction (-), multiplication (*), division (/) and modulo (%), etc.

String operators are positive and negative regular expression with repetition (=~ and !~) and concatenation ( .).

Example

snippet

use 5.010; use strict; use warnings; my $result = "Hello this is " . "rookienerd."; say $result;

Output

Hello this is rookienerd.

snippet

use 5.010; use strict; use warnings; my $result = "Thank You " x 3; say $result;

Output

Thank You Thank You Thank You.

Here, note that on the right of 'x' it must be an integer.

There should be space on either side of the 'x' operator.

For example,

snippet

$result = "Thank You " x 3; # This is correct $result = "Thank You "x3; # This is incorrect

Logical operators give a Boolean value to their operands. They are (&&, || and or).

**&& ->** In && operator, if $a is 0, then value of $a && $b must be false irrespective of the value of $b. So perl does not bother to check $b value. This is called short-circuit evaluation.

**|| ->** In || operator, if $a is non-zero, then value of $a && $b must be true irrespective of the value of $b. So perl does not bother to check $b value.

Example

snippet

use 5.010; use strict; use warnings; $a = 0; $b = 12; my $result1 = $a && $b; say $result1; $a = 12; $b = 14; my $result2 = $a || $b; say $result2;

Output

0
12

Bitwise operators treat their operands numerically at bit level. These are (<<, >>, &, |, ^, <<=, >>=, &=, |=, ^=).

Every number will be denoted in terms of 0s and 1s. Initially integers will be converted into binary bit and result will be evaluated. Final result will be displayed in the integer form.

Example

snippet

use 5.010; use strict; use warnings; #OR operator my $result1 = 124.3 | 99; say $result1; #AND operator my $result2 = 124.3 & 99; say $result2; #XOR operator my $result3 = 124.3 ^ 99; say $result3; #Shift operator my $result4 = 124 >> 3; say $result4;

Output

127
96
31
15

The auto-increment (++) operator is a special operator that increments the numeric character itself by 1.

Example

snippet

use 5.010; use strict; use warnings; my $num = 9; my $str = 'x'; $num++; $str++; say $num++; say $str++;

Output

10
Y

The comparison operator compares the values of its operands. These are ( ==, <, <=, >, >=, <=>, !=).

Example

snippet

use 5.010; use strict; use warnings; say "Enter your salary:"; my $salary = <>; if($salary >= 20000) { say "You are earning well"; } else { say "You are not earning well"; }

Output

Enter your salary:
15000
You are not earning well

The assignment operator assigns a value to a variable.

These are (=, +=, -=, *=, /=, |=, &=, %=)

Example

snippet

use 5.010; use strict; use warnings; $a = 20; my $result1 = $a += $a; say $result1; my $result2 = $a -= 10; say $result2; my $result3 = $a |= 10; say $result3; my $result4 = $a &= 10; say $result4;

Output

40
30
30
10

Related Tutorial