2. References
• A reference is a scalar value that points to a memory location that holds some type of
data. Everything in your Perl program is stored inside your computer's memory.
Therefore, all of your variables and functions are located at some memory location.
References are used to hold the memory addresses.
• When a reference is dereferenced, you retrieve the information referred to by the reference.
• Create References
• It is easy to create a reference for any variable, subroutine or value by prefixing it with a
backslash as follows −
• $scalarref = $foo;
• $arrayref = @ARGV;
• $hashref = %ENV;
• $coderef = &handler;
• $globref = *foo;
• You cannot create a reference on an I/O handle (filehandle or dirhandle) using the backslash
operator but a reference to an anonymous array can be created using the square brackets as
follows −
• $arrayref = [1, 2, ['a', 'b', 'c']];
3. • Similar way you can create a reference to an anonymous hash using the curly brackets as
follows −
• $hashref = { 'Adam' => 'Eve', 'Clyde' => 'Bonnie', };
• A reference to an anonymous subroutine can be created by using sub without a
subname as follows −
• $coderef = sub { print "Boink!n" };
• Following are some of the advantages of passing the reference to a subroutine, instead of
passing the whole array.
1. If we passed the array to a subroutine, Perl copies the entire array into the @_
variable. When the array is big, this is not an effective method.
2. When we want the original array to be modified by the subroutine, we need to
pass the reference of the array.
3. References plays essential role in constructing complex data structures.
4. We can take the reference of an Anonymous Array into the scalar variable as
shown below.
5. $array_ref = [ 11,2,3,45];
4. Dereferencing
• Dereferencing returns the value from a
reference point to the location. To
dereference a reference simply use
$, @ or % as prefix of the reference
variable depending on whether the
reference is pointing to a scalar,
array, or hash.
5. Circular References
• A circular reference occurs when two references contain a reference to each
other. You have to be careful while creating references otherwise a circular
reference can lead to memory leaks. Following is an example −
6. Arrays of Arrays
• In c a two-dimensional array is
constructed as an array of arrays.
a[0][1];
• But in perl we create an array of
references to anonymous arrays.
@colours=([42,128,244],[24,255,0],[0,1
27,127]);
• The array composer converts each
comma separated list to an anonymous
array in memory and returns a reference
to it.
$colours[0][1]=64;
• Second subscript helps to find from
where the element is
selected(dereferencing).
• A 2d array can be created dynamically
by repeatedly using the push operator
to add a reference to an anonymous
array to the top-level array.
7. Arrays of Hashes
An array of hashes is useful when you have a bunch of records that you'd like to access
sequentially, and each record itself contains key/value pairs.
Composition of an Array of Hashes
You can create an array of anonymous hashes as follows:
@AoH = ( {
husband => "barney",
wife => "betty",
son => "bamm bamm",
},
{ husband => "george",
wife => "jane",
son => "elroy",
},
{ husband => "homer",
wife => "marge",
son => "bart", }, );
To add another hash to the array, you can simply say:
push @AoH, { husband => "fred", wife => "wilma", daughter => "pebbles" };
8. Generation of an Array of Hashes
•Here are some techniques for populating an array of hashes. To read from a file with the
following format:
•husband=fred friend=barney
•you could use either of the following two loops:
while ( <> ) {
$rec = {};
for $field ( split ) {
($key, $value) = split /=/, $field;
$rec->{$key} = $value;
}
push @AoH, $rec;
}
while ( <> ) {
push @AoH, { split /[s=]+/ };
}
•If you have a subroutine get_next_pair that returns key/value pairs, you can use it to
stuff @AoH with either of these two loops:
while (@fields = get_next_pair() ) {
push @AoH, { @fields }; } while (<>) { push @AoH, { get_next_pair($_) }; }
You can append new members to an existing hash like so:
$AoH[0]{pet} = "dino"; $AoH[2]{pet} = "santa's little helper";