Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^2: Cloning shared hashref

by menth0l (Monk)
on Jan 20, 2011 at 14:44 UTC ( #883332=note: print w/ replies, xml ) Need Help??


in reply to Re: Cloning shared hashref
in thread Cloning shared hashref

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.


Comment on Re^2: Cloning shared hashref
Re^3: Cloning shared hashref
by BrowserUk (Pope) on Jan 20, 2011 at 15:48 UTC

    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...

        Actually, I think that a pure perl solution is both the right and probably fastest solution to this.

        The problem with the XS implementations is that they are trying to create exact duplicates of the original structures, including any attached magic. Their problem with shared structures is that they were implemented before the shared magic was, and they've not been updated to handle it.

        But for your purpose, you need to explicitly remove that shared magic, which the pure perl cloning does. But as it doesn't have to try and replicate that magic and all the structures and possibilities associated with it, it will likely be faster than an XS implementation that did.

        Which given that you don't want it, is a win-win.


        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.
      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.

        Could I ask you to explain the program requirements that led to the need for this routine and its particular feature set?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2014-09-22 04:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (178 votes), past polls