WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Perl · Basic · question 19 of 100

Explain the concept of references and dereferencing in Perl.?

📕 Buy this interview preparation book: 100 Perl questions & answers — PDF + EPUB for $5

In Perl, a reference is a scalar variable that holds a reference (an address) to another variable. References are used to manipulate complex data structures like arrays, hashes or other references.

For example, let’s say we have an array:

@array = (1, 2, 3);

If we create a reference to this array, we can access the array using the reference variable instead:

$ref = @array;
print $ref->[0];   # prints 1

In this example, ‘$ref‘ is a scalar variable holding a reference to ‘@array‘. We can access elements of the array using the dereference operator ‘->‘. ‘$ref->[0]‘ is equivalent to ‘$$ref[0]‘, which means "the first element of the array referenced by ‘$ref‘".

Dereferencing is the act of accessing the value of a variable that is being referred to by a reference. There are several ways to dereference in Perl, depending on the type of reference.

For example, if we have a hash reference:

$hashref = { foo => 1, bar => 2 };

We can dereference it using the ‘%...‘ syntax:

%hash = %{$hashref};

Now ‘%hash‘ contains the same key-value pairs as the original hash referenced by ‘$hashref‘.

In summary, references provide a way to store and manipulate complex data structures in Perl. Dereferencing is used to access the values stored in the variables that are being referenced.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Perl interview — then scores it.
📞 Practice Perl — free 15 min
📕 Buy this interview preparation book: 100 Perl questions & answers — PDF + EPUB for $5

All 100 Perl questions · All topics