ProgrammingBackend Developer

Describe the differences between shallow and deep copying of complex data structures in Perl. How can you ensure no unwanted references exist between copies?

Pass interviews with Hintsage AI assistant

Answer

In Perl, when copying complex data structures (such as arrays of arrays, hashes of hashes), it's important to understand the difference between "shallow copying" and "deep copying".

Shallow copy creates a new container (like an array or hash), but its elements will reference the same objects as the original. This can lead to unexpected behavior — modifications in the copy will affect the original.

Deep copy creates a completely independent structure by recursively copying all nested elements. To achieve deep copying in Perl, the modules Storable or [Clone] are commonly used:

use Storable 'dclone'; my $original = { a => [1, 2, { b => 3 }] }; my $copy = $original; # Shallow copy my $deep = dclone($original); # Deep copy $copy->{a}[2]{b} = 42; # Changes both $copy and $original! $deep->{a}[2]{b} = 99; # Changes only $deep

Deep copying guarantees that the structures are completely isolated from each other.

Trick Question

What is the difference between copying an array by assigning it to a variable and copying an array by reference? How can you correctly copy an array so that changes in one do not affect the other?

Many respond that it is enough to assign a reference: $copy = \@arr; — however, this is incorrect, as both variables point to the same array. For independent copying, use:

my @copy = @original; # Now the arrays are independent

If nested structures need to be copied, a deep copy is necessary, for example, via Storable::dclone.

Real Error Examples Due to Ignorance of the Nuances of the Topic


Story

In a project for parsing large XML files, a hash template with template structures was used for each document, copying them by reference. Changes in one document began to affect others, leading to mysterious bugs with "missing" or "corrupted" data.

Story

In session storage during authorization, an array of user hashes was used. When recreating the array for a new list of users, only references were copied, leading to a "leak" of user data from the previous session.

Story

When implementing email templates for mass mailing, the original structures with nested objects were copied by assignment, causing changes in the template text to "bleed" between mailings, resulting in outdated messages being delivered to recipients.