Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Dump, Dup or Copy an object

by turo (Friar)
on Feb 22, 2006 at 13:02 UTC ( [id://531943]=perlquestion: print w/replies, xml ) Need Help??

turo has asked for the wisdom of the Perl Monks concerning the following question:

Hi, monks!

I'm wondering if some of you know if exists an effective trick for copying a hash. I mean, does exists some method that allows to duplicate a hash or an array, entirely, creating new references when necessary (not copying the references)? ... (difficult to explain ...)

For example, if i have this array:

@a = ([1,2,3],[a,b,c]);
Which is compossed by two references to other arrays (ARRAY(0x814cc20), ARRAY(0x819eae0)) p.e.
I want to make a new copy of that array, and a new copy of each sub-element referenced, with the same content.
(ARRAY(0x814aaaa), ARRAY(0x819bbbb))
Dumping the element to a file, and re-reading is the only way? ... or does it copy the references to a file, so when you load it, perl can allocate the elements on the same memory position? ...
In c++, for make a copy of an object programmers must implement the copy constructor ... I think, i can do the same in perl (with little problems like, how can I allocate new memory, or how can I make the same as memcpy ... bad habits ^_^>), but after, i've saw the power of the Dumpvalue module ... i cant think in other way to do it ...

Thanks, :-)

turo

perl -Te 'print map { chr((ord)-((10,20,2,7)[$i++])) } split //,"turo"'

Replies are listed 'Best First'.
Re: Dump, Dup or Copy an object
by holli (Abbot) on Feb 22, 2006 at 13:13 UTC
    What you want is called a "deep copy" or "clone". See my node here for some modules that apply here.

    Update:
    You might also want to have a look at Class::Cloneable if your structure is actually an object.


    holli, /regexed monk/
Re: Dump, Dup or Copy an object
by borisz (Canon) on Feb 22, 2006 at 13:33 UTC
    My choice would be Storable's dclone function.
    Boris

      map {thanks} qw(arkturuz borisz holli);

      The Storable module is what i need (i think so, after reading the pod):

      # Deep (recursive) cloning $cloneref = dclone($ref);


      ummm, The Class::Cloneable, is cool, but remembers me c++ and the copy constructor, maybe i'll consider to use it soon ... :-) .
      And, at last the powerfull Data::Dumper is too complex, i suppose, for what i wantted to do (indeed, i don't know what to do with it ^_^> ... by the momment), ...

      Cheers

      perl -Te 'print map { chr((ord)-((10,20,2,7)[$i++])) } split //,"turo"'
        Out of curiosity i benchmarked it and got the following results:
        use Benchmark qw(timethese); use Clone; use Storable; #use Scalar::Util::Clone; my $struct = { 'foo' => 'bar', 'move' => 'zig', bar => [1, 2, 3] }; timethese ( 500000, { 'Clone' => sub { my $s = Clone::clone($struct) + }, #'Scalar::Util::Clone' => sub { my $s = Scalar::Util::Clone::c +lone ($struct) }, 'Storable' => sub { my $s = Storable::dclone ($stru +ct) } } ); #Benchmark: timing 500000 iterations of Clone, Storable... # Clone: 3 wallclock secs ( 4.28 usr + 0.00 sys = 4.28 CPU) @ 1169 +04.37/s (n=500000) # Storable: 74 wallclock secs (69.62 usr + 0.03 sys = 69.65 CPU) @ 7 +178.65/s (n=500000)
        The reason because Scalar::Util::Clone is commented out is that I couldn't make it compile nor did I find a ppd for it. Maybe someone else can step in?


        holli, /regexed monk/
      Dumping the element to a file, and re-reading is the only way?
      The OP is already aware of Data::Dumper.

      Update: Sorry Boris, my bad. This was of course meant to be a reply to arkturuz' node above.


      holli, /regexed monk/
        ???
        Boris
Re: Dump, Dup or Copy an object
by arkturuz (Curate) on Feb 22, 2006 at 13:23 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-25 23:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found