In Perl, we use variables to access data stored in a memory location(all data and functions are stored in memory). Variables are assigned with data values that are used in various operations. Perl Reference is a way to access the same data but with a different variable. A reference in Perl is a scalar data type that holds the location of another variable. Another variable can be scalar, hashes, arrays, function name, etc. Nested data structure can be created easily as a user can create a list that contains the references to another list that can further contain the references to arrays, scalar or hashes etc.
Reference Creation
You can create references for scalar value, hash, array, function etc. In order to create a reference, define a new scalar variable and assign it the name of the variable(whose reference you want to create) by prefixing it with a backslash.
Examples: Making References of different Data Types:
# Array Reference
# defining array
@array = ('1', '2', '3');
# making reference of array variable
$reference_array = \@array;
# Hash Reference
# defining hash
%hash = ('1'=>'a', '2'=>'b', '3'=>'c');
# make reference of the hash variable
$reference_hash = \%hash;
# Scalar Value Reference
# defining scalar
$scalar_val = 1234;
# making reference of scalar variable
$reference_scalar = \$scalar_val;
Note:
- A reference to an anonymous hash can be created using the curly brackets {} around the key and value pairs.
- Example:
# creating reference to anonymous hash
$ref_to_anonymous_hash = {'GFG' => '1', 'Geeks' => '2'};
- A reference to an anonymous array can be created using the square brackets [].
- Example:
# creating reference to an anonymous array
$ref_to_anonymous_array = [20, 30, ['G', 'F', 'G']];
- A reference to an anonymous subroutine can also be created with the help of sub. Here there will be no name for the sub.
Example:
# creating reference to an anonymous subroutine
$ref_to_anonymous_subroutine = sub { print "GeeksforGeeks\n"};
- A reference to an Input/output handle i.e. dirhandle and filehandle cannot be created.
Dereferencing
Now, after we have made the reference, we need to use it to access the value. Dereferencing is the way of accessing the value in the memory pointed by the reference. In order to dereference, we use the prefix $, @, % or & depending on the type of the variable(a reference can point to a array, scalar, or hash etc).
Example 1:
Perl
# Perl program to illustrate the
# Dereferencing of an Array
# defining an array
@array = ('1', '2', '3');
# making an reference to an array variable
$reference_array = \@array;
# Dereferencing
# printing the value stored
# at $reference_array by prefixing
# @ as it is a array reference
print @$reference_array;
Example 2: We can use the below way to dereference the array and get the same output as mentioned above:
Perl
#!/usr/bin/perl
# your code here
# Perl program to illustrate the
# Dereferencing of an Array
# defining an array
@array = ('1', '2', '3');
# making an reference to an array variable
$reference_array = \@array;
# Dereferencing
# printing the value stored
# at $reference_array by prefixing
# @ as it is a array reference
print @ { $reference_array };
Example 3:
Perl
# Perl program to illustrate the
# Dereferencing of a Hash
# defining hash
%hash = ('1'=>'a', '2'=>'b', '3'=>'c');
# creating an reference to hash variable
$reference_hash = \%hash;
# Dereferencing
# printing the value stored
# at $reference_hash by prefixing
# % as it is a hash reference
print %$reference_hash;
Example 4:
Perl
# Perl program to illustrate the
# Dereferencing of a Scalar
# defining a scalar
$scalar = 1234;
# creating an reference to scalar variable
$reference_scalar = \$scalar;
# Dereferencing
# printing the value stored
# at $reference_scalar by prefixing
# $ as it is a Scalar reference
print $$reference_scalar;
Dereference of any particular array element:
We can also deference any particular array element if we want by passing the index of that element.
Perl
#!/usr/bin/perl
# your code here
# defining an array
@array = ('1', '2', '3');
# making an reference to an array variable
$reference_array = \@array;
# Dereferencing
# printing the value stored
# at $reference_array by prefixing
# @ as it is a array reference
print @ { $reference_array }[2]; # This will print the element at 2nd index i.e 3 for this case
Similar Reads
Perl | Pass By Reference
When a variable is passed by reference function operates on original data in the function. Passing by reference allows the function to change the original value of a variable. When the values of the elements in the argument arrays @_ are changed, the values of the corresponding arguments will also c
2 min read
Perl | References to a Subroutine
Prerequisite: Perl references Declaring References to a Subroutine In Perl, a reference is, exactly as the name suggests, a reference or pointer to another object. References actually provide all sorts of abilities and facilities that would not otherwise be available and can be used to create sophis
6 min read
Perl Reverse Sort Method
The article focuses on discussing how to sort an array/ list which consists of integers or strings in reverse order (descending to ascending). Prerequisite: Sorting in Perl. Syntax: reverse sort @array_name; reverse sort (@array_name); This method is a combination of the reverse and sort method of P
3 min read
Perl | Variables
Variables in Perl are used to store and manipulate data throughout the program. When a variable is created it occupies memory space. The data type of a variable helps the interpreter to allocate memory and decide what to be stored in the reserved memory. Therefore, variables can store integers, deci
4 min read
Perl | Regex Cheat Sheet
Regex or Regular Expressions are an important part of Perl Programming. It is used for searching the specified text pattern. In this, set of characters together form the search pattern. It is also known as regexp. When user learns regular expression then there might be a need for quick look of those
6 min read
Perl | LDAP Server
Lightweight Directory Access Protocol (LDAP) is an internet protocol that works on TCP/IP and is used to access information from directories. The LDAP protocol is usually used to access an active directory. It allows us to keep a directory of items and information about them. LDAP stores the data in
4 min read
Perl | Scalars
A scalar is a variable that stores a single unit of data at a time. The data that will be stored by the scalar variable can be of the different type like string, character, floating point, a large group of strings or it can be a webpage and so on.Example : Perl # Perl program to demonstrate # scalar
2 min read
Perl | return() Function
return() function in Perl returns Value at the end of a subroutine, block, or do function. Returned value might be scalar, array, or a hash according to the selected context. Syntax: return Value Returns: a List in Scalar Context Note: If no value is passed to the return function then it returns an
2 min read
Perl | Array Slices
In Perl, array is a special type of variable. The array is used to store the list of values and each object of the list is termed as an element. Elements can either be a number, string, or any type of scalar data including another variable. Arrays can store any type of data and that data can be acce
3 min read
Perl vs Python
.Difference-table { border-collapse: collapse; width: 100%; } .Difference-table td { text-color: black !important; border: 1px solid #5fb962; text-align: left !important; padding: 8px; } .Difference-table th { border: 1px solid #5fb962; padding: 8px; } .Difference-table tr>th{ background-color: #c6e
3 min read