Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: threads::shared hash

by BrowserUk (Patriarch)
on Mar 27, 2004 at 01:01 UTC ( #340179=note: print w/replies, xml ) Need Help??


in reply to threads::shared hash

Seems to be a problem with your build of perl rather than perl itself, in that I don't get any errors (other than the missing semicolons) running your snippet under 5.8.2/AS 808

This is perl, v5.8.2 built for MSWin32-x86-multi-thread Binary build 808 provided by ActiveState Corp. http://www.ActiveState. +com

That said, your snippet obviously isn't a direct c&p of your failing code (given the syntax errors), and the partial error message you give "Invalid value for shared scalar at..." is usually a compile or runtime error, not a crash. So maybe you should post a more complete example?

P:\test>perl -Mthreads -mthreads::shared my( %h ) : shared; my $var = 'hello'; $h{ $var } = 123; print $h{hello}; ^Z 123

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

Replies are listed 'Best First'.
Re: Re: threads::shared hash
by fxmakers (Friar) on Mar 27, 2004 at 01:14 UTC
    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.

      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!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2023-02-04 02:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (30 votes). Check out past polls.

    Notices?