in reply to Re: forked::shared with hashes?
in thread forked::shared with hashes?

It turns out the problem has to do with the restriction that you should share scalar variables, not complex structures like hashes. So the parent can see the child's hash modifications if you do something like this:

use forks; use forks::shared; my %GLOBALHASH : shared; my %hashref : shared; $hashref = \%GLOBALHASH; $child = fork (); if (!$child) { # child $hashref->{OILPRICE} = 100; exit 0; } elsif ($child) { # daddy sleep (2); print "Global Oils Price is now: " . $hashref->{OILPRICE} . " doll +ars\n"; }

However, I've not been able to get this to work with hashes of hashes.

Replies are listed 'Best First'.
Re^3: forked::shared with hashes?
by choroba (Cardinal) on Apr 02, 2011 at 08:31 UTC
    No, for me, it worked with a hash as well. The restriction applies to threads::shared, not forks::shared.
      The restriction applies to threads::shared,

      Could you:

      1. explain "the restriction"
      2. either show where that restriction is documented; or code to demonstrate 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.
        I just cited "the restriction" from the previous post. I thought the author might mean this:
        In the standard Perl threads implementation, arrays and hashes are re-initialized when they become shared (with the share()) function. The share() function of forks::shared does not initialize arrays and hashes when they become shared with the share() function.
        Quoted from the documentation of forks::shared.