in reply to Cloning shared hashref

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.

Replies are listed 'Best First'.
Re^2: Cloning shared hashref
by menth0l (Monk) on Jan 20, 2011 at 14:44 UTC
    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.
        Thanks. I wrote something similiar by myself but i hoped i would find somthing faster than pure perl cloning i.e. XS-implementation...
        I ran in exactly the same problem, I wanted to serialize a shared variable with YAML::XS. I reversed the shared_clone function and named it unshared_clone. The advantage is that references stay references (less memory consumption) and circular references don't blow up your script. I put it on github https://github.com/jwba/threads-shared-util.