http://qs321.pair.com?node_id=340183


in reply to Re: threads::shared hash
in thread threads::shared hash

You're right, I did some mistakes, I just modified my post :P
A shared hash works fine, but
the creation of a shared hash of hashes is the problem:

use threads;
use threads::shared;
use strict;

my %hash : shared;
my $var = "hello";

$hash{'foo'}{'bleh'} = "test";
$hash{$var}{'foo'} = "test";

Both 'foo' or $var keys returns the error: Invalid value for shared scalar.

Replies are listed 'Best First'.
Re: Re: Re: threads::shared hash
by BrowserUk (Patriarch) on Mar 27, 2004 at 01:30 UTC

    Unfortunately, autovivification doesn't auto-share, so you have to manually create shared elements.

    #! perl -slw use strict; use threads; use threads::shared; use Data::Dumper; my %hash : shared; my $var = 'hello'; $hash{ foo } = &share( {} ); $hash{ foo }{ bleh } = 'test'; $hash{ $var } = &share( {} ); $hash{ $var }{ foo } = 'test'; print Dumper \%hash; __END__ P:\test>test $VAR1 = { 'foo' => { 'bleh' => 'test' }, 'hello' => { 'foo' => 'test' } };

    Note: The &shared( {} ); syntax is required. See threads::shared for details.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
      Thanks a lot!