Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Cloning shared hashref

by menth0l (Monk)
on Jan 20, 2011 at 14:12 UTC ( #883319=perlquestion: print w/ replies, xml ) Need Help??
menth0l has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to clone shared hashref into non-shared one. But when i run this piece of code:
use threads; use threads::shared; use Clone; use Data::Dumper; my $x = shared_clone({ a => 'Foo', b => 'Bar' }); my $z = Clone::clone($x); print Dumper($z);
i get an error:
Can't locate object method "FETCH" via package "threads::shared::tie" +at C:/Perl/site/lib/Data/Dumper.pm line 190.
Moreover, print keys %$z; prints "ab" but print values %$z; gives previous error. Can anyone explain to me what can i do to use $z as a normal hashref and what causes this error?

Comment on Cloning shared hashref
Select or Download Code
Re: Cloning shared hashref
by BrowserUk (Pope) on Jan 20, 2011 at 14:33 UTC

    Storing a reference to a shared structure into a non-shared scalar isn't a good idea. Correct that and you get a clue as to the problem:

    use threads; use threads::shared; use Clone; use Data::Dumper; my $x :shared = shared_clone({ a => 'Foo', b => 'Bar' }); my $z = Clone::clone($x); print Dumper($z); __END__ C:\test>junk25 Don't know how to handle magic of type \156 at C:\test\junk25.pl line +11.

    Basically, Clone has never been updated to work with shared data, which is rather more than normal tied data.

    This all begs the question of why you want to clone a shared structure to a non-shared?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      This all begs the question of why you want to clone a shared structure to a non-shared?
      I don't want orignal to be changed by one of the threads. So i need o thread-local copy that can be modified at will.

        Use this:

        use threads; use threads::shared; use Data::Dumper; sub clone { my $ref = shift; my $type = ref $ref; if( $type eq 'HASH' ) { return { map clone( $_ ), %{ $ref } }; } elsif( $type eq 'ARRAY' ) { return [ map clone( $_ ),@{ $ref } ]; } elsif( $type eq 'REF' ) { return \ clone( $$ref ); } else { return $ref; } } my $x :shared = shared_clone({ a => 'Foo', b => 'Bar' }); my $z = clone( $x ); print Dumper( $z );

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Cloning shared hashref
by sundialsvc4 (Abbot) on Jan 20, 2011 at 15:14 UTC

    What you are looking for is called a deep copy.   (Or, “deep cloning.”)

    e.g. clone in Storable.

    Hashes often contain references to things, and changing any one of the references changes the (one) thing to which it refers...

    “Holy EQUIVALENCE, Captain Fortran!”

      1. If you'd bothered to look, you'd have realised that the sole purpose of Clone::clone() is to perform deep copies.
      2. If you'd looked at Storable, you'd have discovered that it doesn't export a routine called clone().
      3. And if you bothered to try dclone(), you'd have found that it produces exactly the same error.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (16)
As of 2014-10-21 15:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (105 votes), past polls