in reply to scalars, references and queues

Sharing a reference to something that isn't shared is an error. As threads::share says,

my $var :shared; $var = 1; # ok $var = []; # error $var = &share([]); # ok

Maybe Thread::Queue should handle references better. Fortunately, it's easy to fix. Just share the referenced hash.

my %hash :shared = ( ... ); $DataQueue->enqueue(\%hash);

or

# Fixed as per BrowserUK's reply. # Initialization must be done after share. my $href = share({}); %$href = ( ... ); $DataQueue->enqueue($href);

or

use Storable qw( freeze thaw ); my %hash = ( ... ); $DataQueue->enqueue(freeze(\%hash));

Untested.

Update: Added Storeable snippet. Rephrased some text.

Replies are listed 'Best First'.
Re^2: scalars, references and queues
by BrowserUk (Patriarch) on Aug 03, 2007 at 15:38 UTC

    A note of caution. Using

    my $href = { ... }; $DataQueue->enqueue(share($href));

    Is dangerous, because whatever you put inside the anonymous hash, represented in your exampe by ..., will be silently discarded when you do share( $href ).

    You must share the reference first, and then populate 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.