Perl Operator Types

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
Perl Operators

Perl Numeric operators

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

Perl String operators

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

String Concatenation operator

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

output:

Output
Hello this is JavaTpoint.

String Repetition operator

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

output:

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

Perl Logical operators

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:

Output
0 12

Perl Bitwise operators

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:

Output
127 96 31 15

Perl Special operators

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:

Output
10 Y

Perl Comparison operators

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:

Output
Enter your salary: 15000 You are not earning well

Perl Assignment operators

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:

Output
40 30 30 10
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents