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

This may be an obvious question, but I'm having trouble wrapping my head around it...I have a reference to a data structure consisting of nested hashes and/or arrays, and I have to pass it (by reference) to a subroutine that does some destructive operations on it.

Before I pass it, however, I'd like to make a copy of the entire data structure, one that will not get modified. None of the following examples do what I need, but they illustrate my train of thought...

# this is an arrayref or hashref containing # other arrayrefs and/or hashrefs $orig_ref; # this makes a copy of the *reference* to the same data # DOESNT WORK $copy_ref = $orig_reg; # this dereferences, then references the original data # DOESNT WORK $copy_ref = ref($orig_ref) eq 'ARRAY' ? \@{$orig_ref} : ref($orig_ref) eq 'HASH' ? \%{$orig_ref} : undef; # this derefs, copies, then refs the copy # SHOULD WORK BUT DOESNT? if(ref($orig_ref) eq 'ARRAY') { @copy = @{$orig_ref}; # deref the original @copy2 = @copy; # COPY the original data? $copy_ref = \@copy2; # ref the copied data? } elsif(ref($orig_ref) eq 'HASH') { %copy = %{$orig_ref}; # deref the original %copy2 = %copy; # COPY the original data? $copy_ref = \%copy2; # ref the copied data? }
I was certain the last example would work...What am I misunderstanding so fundamentally?

Replies are listed 'Best First'.
Re: How to copy a referenced data structure?
by jettero (Monsignor) on May 18, 2007 at 17:18 UTC

    I don't think there's a way to avoid recursing to make the copy. Assuming the referenced data structure has refs within refs anyway.

    Might I suggest Storable's freeze/thaw combo: dclone()? It handle's most of the surprises automatically.

    Say you have $ar = [ 1,2,3 ];. You can in fact copy it with $b = [ @$ar ]. It doesn't work quite right with $ar = [ [ 1,2,3 ] ] however, as you'll end up with a copy of the ref in a new array ref, if you follow my meaning.

    -Paul

      Wow, I feel like a fool. I was so focused on the top reference, i forgot the nested structures were also refs...

      I used Storable as you suggested, and it works great. Thanks!

Re: How to copy a referenced data structure?
by shmem (Chancellor) on May 18, 2007 at 18:09 UTC
    Check out Clone. Faster than Storable, but not so flexible.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      I am trying to see what the issue is that this question is trying to address. If I make a copy of a reference to a reference, and then undef that reference, the copy to original references data still exists. I know I am missing the point here, I would just like to see how.
      #!/usr/bin/perl use Data::Dumper; $ar = [ [ 1,2,3 ] ]; $copy = [ @$ar ]; undef $ar; print Dumper($copy);
        $copy = [ @$ar ];

        $copy is a reference to an array. The first (and only) element of that array is the array that $ar refers to. When you undef $ar; that has no effect on the array that $ar referred to - and hence no effect on $copy.

        Cheers,
        Rob
Re: How to copy a referenced data structure?
by kyle (Abbot) on May 18, 2007 at 17:23 UTC
Re: How to copy a referenced data structure?
by scorpio17 (Canon) on May 18, 2007 at 17:55 UTC
    Check out the Storable module, on CPAN:
    use Storable; $serialized = freeze \%table; %table_clone = %{ thaw($serialized) };