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). Some of the operators already discussed in Perl Operators Set – 1. Remaining operators will be discussed in this article:
Quote-like Operators
In these operators, {} will represent a pair of delimiters that user choose. In this category there are three operators as follows:
- q{ } : It will encloses a string in single quotes(”) and cannot interpolate the string variable. For Example: q{geek} gives ‘geek’.
- qq{ }: It will encloses a string in double quotes(“”) and can interpolate the string variable. For Example: qq{geek} gives “geek”.
- qx{ } : It will encloses a string in invert quotes(“). For Example: qq{geek} gives `geek`.
Example:
Perl
$g = "Geek" ;
$r = q{$g} ;
print "Value of g is = $r\n" ;
$r = qq{$g} ;
print "Value of g is = $r\n" ;
$d = qx{date} ;
print "Value of qx{date} = $d" ;
|
Output:
Value of g is = $g
Value of g is = Geek
Value of qx{date} = Sun Aug 12 14:19:43 UTC 2018
String Manipulation Operators
String are scalar variables and start with ($) sign in Perl. The String is defined by the user within a single quote(”) or double quote(“”). There are different types of string operators in Perl, as follows:
- Concatenation Operator (.) : Perl strings are concatenated with a Dot(.) symbol. The Dot(.) sign is used instead of (+) sign in Perl.
- Repetition Operator (x): The x operator accepts a string on its left-hand side and a number on its right-hand side. It will return the string on the left-hand side repeated as many times as the value on the right-hand side.
Example:
Perl
$first_string = "Geeks" ;
$second_string = "forGeeks" ;
$concat_string = $first_string . $second_string ;
print "Result of Concatenation Operator = $concat_string\n" ;
$string = "GeeksforGeeks " ;
$str_result = $string x 4;
print "Result of Repetition Operator = $str_result" ;
|
Output:
Result of Concatenation Operator = GeeksforGeeks
Result of Repetition Operator = GeeksforGeeks GeeksforGeeks GeeksforGeeks GeeksforGeeks
Note: To know more about string operators in Perl, you can refer to Perl String Operators article.
Range Operator(..)
In Perl, range operator is used for creating the specified sequence range of specified elements. So this operator is used to create a specified sequence range in which both the starting and ending element will be inclusive. For example, 7 .. 10 will create a sequence like 7, 8, 9, 10.
Example:
Perl
@res = (1..4);
print "Result of Range Operator = @res" ;
|
Output:
Result of Range Operator = 1 2 3 4
Auto Increment & Auto Decrement Operator
Auto Increment(++): Increment the value of an integer. When placed before the variable name (also called pre-increment operator), its value is incremented instantly. For example, ++$x. And when it is placed after the variable name (also called post-increment operator), its value is preserved temporarily until the execution of this statement and it gets updated before the execution of the next statement. For example, $x++.
Auto Decrement Operator(–): Decrement the value of an integer. When placed before the variable name (also called pre-decrement operator), its value is decremented instantly. For example, –$x. And when it is placed after the variable name (also called post-decrement operator), its value is preserved temporarily until the execution of this statement and it gets updated before the execution of the next statement. For example, $x–.
Note: The increment and decrement operators are called unary operators as they work with a single operand.
Example:
Perl
$g = 10;
$res = ++ $g ;
print "Value of res is = $res\n" ;
print "Value of g is = $g\n" ;
$g1 = 15;
$res1 = $g1 ++;
print "Value of res1 is = $res1\n" ;
print "Value of g1 is = $g1\n" ;
$g2 = 20;
$res2 = -- $g2 ;
print "Value of res2 is = $res2\n" ;
print "Value of g2 is = $g2\n" ;
$g3 = 25;
$res3 = $g3 --;
print "Value of res3 is = $res3\n" ;
print "Value of g3 is = $g3\n" ;
|
Output:
Value of res is = 11
Value of g is = 11
Value of res1 is = 15
Value of g1 is = 16
Value of res2 is = 19
Value of g2 is = 19
Value of res3 is = 25
Value of g3 is = 24
Arrow Operator(->)
This operator is used for dereferencing a Variable or a Method from a class or an object. Example: $ob->$x is an example to access variable $x from object $ob. Mostly this operator is used as a reference to a hash or an array to access the elements of the hash or the array.
Example:
Perl
use strict;
use warnings;
my $arr1 = [4,5,6];
my $arr2 = [4,5,[6,7]];
my $has1 = { 'a' =>1, 'b' =>2};
my $has2 = { 'a' =>1, 'b' =>2, 'c' =>[1,2], 'd' =>{ 'x' =>3, 'y' =>4}};
print "$arr1->[0]\n" ;
print "$arr2->[1]\n" ;
print "$arr2->[2][0]\n" ;
print "$has2->{'a'}\n" ;
print $has2 ->{ 'd' }->{ 'x' }, "\n" ;
print $has2 ->{ 'c' }->[0], "\n" ;
|
Output:
4
5
6
1
3
1
Points to Remember:
- Operator Associativity: It is used to decide whether equation or expression will evaluate from left to right or right to left. The order of evaluation is very important. Sometimes it may look same from both the sides but it can produce a lot of difference.
- Perl Arity: The arity can be defined as the number of operands on which an operator operates.
- Nullary operator: These operator operates on zero operand.
- Unary operator: Theses operators operates on one operand.
- Binary operator: These operators operates on two operands.
- Listary operator: These operators operates on a list of operands.
- Perl Fixity: It can be defined as operator’s position relative to its operands.
- Infix operator: These operators comes between its operands. For Example, 5 + 8, Here, + operator comes between the operands 5 and 8
- Prefix operator: These operators comes before its operands. For Example, ! $g, – 7, Here, ! and – operator comes before the operands $a and 3.
- Postfix operator: These operators appears after its operands. For Example, $y ++, Here, ++ operator appears after the operands $x.
- Circumfix operators: These operators enclosed its operands like as hash constructor and quoting operators. For Example, (qq[..])
- Postcircumfix operators: These operators follow some certain operands and surround some operands. For Example, $hash{$y}
Operator Precedence & Associativity Table
Operator |
Precedence |
Associativity |
-> |
Arrow Operator |
Left to Right |
++, — |
Increment, decrement |
Non Associative |
** |
Exponent |
Right to Left |
!, +, -, ~ |
Not, unary plus, unary minus, complement |
Right to Left |
!=, ~= |
Not equal to, complement equal to |
Left to Right |
*, /, % |
Multiply, Divide, Modulus |
Left to Right |
<<, >> |
Shifting Operators |
Left to Right |
<>, <=, >= |
Comparison, Less than equal to, right than equal to |
Non Associative |
& |
Bitwise AND |
Left to Right |
|, ^ |
Bitwise OR, EX-OR |
Left to Right |
&& |
AND |
Left to Right |
|| |
OR |
Left to Right |
.. |
Range Operator |
Non Associative |
*=, /=, +=, -= |
Multiply equal to, Divide equal to, plus equal to |
Right to Left |
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
14 Jan, 2022
Like Article
Save Article