in reply to Bad nstore of a shared hash

threads::shared::tie does not play nice with Storable. The solution is to make a copy of the hash before writing it to disk:

#! perl -slw use strict; use threads; use threads::shared; use Storable qw[ nstore retrieve ]; use Data::Dump qw[ pp ]; my %hash :shared = ( a=>1, b=>1, ); async { nstore { %hash }, 'fred.bin'; }->join; my $hash2 = retrieve 'fred.bin'; pp $hash2; __END__ C:\test>junk42 { a => 1, b => 1 }

That does mean you'll have to share_clone() it if you need it shared once you retrieve() it.


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.
RIP an inspiration; A true Folk's Guy

Replies are listed 'Best First'.
Re^2: Bad nstore of a shared hash
by daverave (Scribe) on Sep 21, 2010 at 23:24 UTC
    Let me see if I got it right. You use async just to get a copy of the hash? You don't really need that fork, right?

    Also, how is that the copy created is not shared also?

      You use async just to get a copy of the hash?

      No. I used the async to demonstrate Storable work fine within threads; that the problem you encountered is with threads::shared::tie.

      So yes, the threading was unnecessary to demonstrate that Storable works, but we know that already ;)

      Also, how is that the copy created is not shared also?

      Because nothing is shared unless you explicitly share it.

      The reference to the shared hash %hash, simply results in a list of values. Placing them inside an anonymous hash constructor, constructs a thread-local (ie.non-shared), anonymous hash. Which Storable can nstore() in the usual way.

      It is effectively the same as just: nstore { 1, 'a', 2, 'b }, 'fred.bin';. Except that the values come from a shared hash rather than constants.


      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!