Perl | References

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 which 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 which 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 which 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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;    

chevron_right


Output:

123

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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;  

chevron_right


Output:

3c2b1a

Example 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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;  

chevron_right


Output:

1234


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.