Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Copying an array or hash

by bageler (Hermit)
on Dec 05, 2005 at 19:13 UTC ( [id://514208]=note: print w/replies, xml ) Need Help??


in reply to Copying an array or hash

Here's the deep copy sub that I use in some of my objects:
sub deep_copy { my $self = shift; my $this = shift; unless (ref $this) { $this; } elsif (ref $this eq 'ARRAY') { [map $self->deep_copy($_), @$this]; } elsif (ref $this eq 'HASH') { +{map{$_ => $self->deep_copy($this->{$_})} keys %$this +}; } }
Here's the sub that calls it:
sub copy { my $self = shift; return $self->deep_copy($self); }
of course, this works because I assume my object will only either have hashrefs, arrayrefs or scalars.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://514208]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-25 14:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found