Dereferencing in perl

Dereferencing in perl

References are commonly used when you are returning a large object or data structure (for example an array or hash) from a subroutine. Instead of returning a copy of the entire data structure, you return a pointer to the structure. This makes your programs more efficient. You can also use references to subroutines and scalars.

When you want to actually use the values in these variables, you need to dereference the pointer. This page shows you how this works in Perl.

Creating references

You can create a reference to a variable or subroutine by using the backslash (\) operator. For example, the following subroutine returns a reference to the array @fruit.

The code to call this subroutine would look like:

The fruit_i_like() subroutine returns a scalar variable so that $fruit will hold a reference to the @fruit array.

This works the same with scalars, hashes and subroutines:

You can create a reference to an anonymous array or hash by using square brackets for an array and braces for a hash:

Similarly, you can create a reference to an anonymous subroutine:

Filehandles can also be referenced. You do this by using a reference to a typeglob (see perldoc perldata):

Dereferencing – part 1

A simple way of dereferencing is to put the appropriate identifier (sigil) in front of the reference. For example, the following code creates a reference to a scalar, and prints both the reference and the dereferenced value so that you can see that the reference is actually a reference:

This produces something like:

In the same way, to dereference an array reference, hash reference or a subroutine reference, you put the appropriate identifier (sigil) before the reference. Here is a array reference example:

The output is:

And a hash reference example:

The output of this program is:

And a subroutine reference example:

This calls the subroutine and produces:

Dereferencing – part 2

Another similar way of dereferencing is to use a block (in curly braces). For example, the example above that uses a scalar reference would be re-written as:

While this looks like only two extra characters, the block is helpful when your reference is stored in a hash or other data structure:

Dereferencing – part 3

The arrow operator also allows you to dereference references to arrays or hashes. The arrow operator makes more complex structures easier to read. The first example shows accessing an element of an array reference:

This would produce the following output:

The next example shows accessing elements of an hash refernce:

This produces the following output:

Subroutine references work the same way, using parenthesis:

The ref operator

The handly perl ‘ref’ operator tells you what type of reference your variable is. This means that you can write a subroutine that takes a different action based on the type of reference it received. In the following example, the subroutine write_to_file writes the passed in $message to the given filehandle, however it writes the message differently depending on whethere the variable is a scalar value, an array reference or a hash reference.

The above example would produce a file with the following contents:

See also

Scroll to Top