Open In App

Perl | Operators | Set – 2

Last Updated : 14 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Perl program to demonstrate the Quote
# Like Operators
  
#!/usr/local/bin/perl
  
# taking a string variable
$g = "Geek";
  
# using single quote Operators
# this operator will not 
# interpolate the string variable
$r = q{$g};
  
print "Value of g is = $r\n";
  
# using double quote Operators
# this operator will interpolate
# the string variable
$r = qq{$g};
  
print "Value of g is = $r\n";
  
# Executing unix date command
$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




# Perl program to demonstrate the
# string operators
  
#!/usr/bin/perl
  
# Input first string 
$first_string = "Geeks";
  
# Input second string 
$second_string = "forGeeks";
  
# Implement Concatenation operator(.) 
$concat_string = $first_string.$second_string;
  
# displaying concatenation string result
print "Result of Concatenation Operator = $concat_string\n";
  
# Input a string 
$string = "GeeksforGeeks "
   
# Using Repetition operator(x)
$str_result = $string x 4; 
   
# Display output
# print string 4 times 
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




# Perl program to demonstrate the
# Range operators
  
#!/usr/bin/perl
  
# using range operator  
@res = (1..4);
  
# Display output
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




# Perl program to demonstrate the Auto 
# Increment and Decrement Operator
  
#!/usr/local/bin/perl
  
# taking a  variable
$g = 10;
  
# using pre Increment
$res = ++$g;
  
print "Value of res is = $res\n";
print "Value of g is = $g\n";
  
# taking a  variable
$g1 = 15;
  
# using post Increment
$res1 = $g1++;
  
print "Value of res1 is = $res1\n";
print "Value of g1 is = $g1\n";
  
# taking a  variable
$g2 = 20;
  
# using pre Decrement
$res2 = --$g2;
  
print "Value of res2 is = $res2\n";
print "Value of g2 is = $g2\n";
  
# taking a  variable
$g3 = 25;
  
# using post Decrement
$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




# Perl program to demonstrate the Arrow Operator
  
#!/usr/local/bin/perl
  
use strict;
use warnings;
  
# reference to array
my $arr1 = [4,5,6];
  
# array inside array
my $arr2 = [4,5,[6,7]];
  
# reference to hash
my $has1 = {'a'=>1,'b'=>2};
  
# hash inside hash
my $has2 = {'a'=>1,'b'=>2,'c'=>[1,2],'d'=>{'x'=>3,'y'=>4}};
  
# using arrow operator
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:

  1. 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.
  2. 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.
  3. 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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads