In Perl, reference structures (such as arrays of arrays or hashes of hashes) are by default copied superficially: only the references themselves are copied, not the nested contents. Historically, this often led to unexpected effects — changing the internal structure in one copy reflects in others. Solution: use specialized methods and modules for deep cloning to create an independent structure with independent nested elements.
Example code (using Storable):
use Storable 'dclone'; my $original = { a => [1, 2, { x => 10 }] }; my $copy = dclone($original); $copy->{a}[2]{x} = 20; print $original->{a}[2]{x}; # 10
Key points:
Does the operator “=” ($copy = $ref) work for deep copying?
No, the operator “=” copies only the reference itself. After such an assignment, any changes to $copy also reflect in $ref.
Can Data::Dumper be used for deep copying the structure?
Data::Dumper is a tool for debugging and serializing to a string, not intended for restoring data structures in memory. For reverse conversion, eval is needed, which is dangerous and not recommended for security and performance reasons.
Does dclone always work correctly with objects (blessed references)?
Storable::dclone clones objects, but only if the class does not overload serialization methods or contain non-standard objects (e.g., file descriptors or strong references to external resources). For complex objects, STORABLE_freeze and STORABLE_thaw methods need to be implemented.
An array of arrays is duplicated by the operator =, changes are made in one of the nested structures — the same changes are visible in all copies.
Pros:
Cons:
Storable::dclone or Clone::PP is used, all nested structures are independent.
Pros:
Cons: