Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: Shared hash within shared hash of hash???

by muba (Priest)
on Jan 02, 2013 at 12:00 UTC ( #1011253=note: print w/replies, xml ) Need Help??

in reply to Shared hash within shared hash of hash???

I suspect that it has something to do with {} being a reference to another, newly created value. Let's break things down.

my %a : shared = (); $a{foo} = {}; $a{bar} = 42;

What I suspect to be happening here, is the following.

The first line conveniently reads from left to right: my %a declares the hash in the current lexical scope in the current thread. : shared shares the hash with all other threads. = (); assigns the empty list to this hash.

The next line has most of its action on the far right side. {}; creates an anonymous hash in the current thread. Due to being anonymous, it's not bound by any scope — it will simply live for as long as something references to it. So {} is kind enough to create and return that reference. That's right: a reference and the value it refers to, are in fact not the same entity. $a{foo} = then stores that reference under the key "foo" in the hash %a.

If you want, you can see that {} creates a new value:

print {}, "\n"; print {}, "\n";

And the last line simply stores the number 42 in $a{bar}. There is no referencing here, and no creation of a new value. 42 will be 42, so there's no need to share it here.

So my best guess, the reason you need to &share( {} ) is not because $a{foo} isn't shared, but because the anonymous hash that {} created, isn't shared.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1011253]
[LanX]: (movies)
[Your Mother]: Real ideas. Ideas grounded in reality as well.
[LanX]: still some good newer ones. Oblivion had a great design, Moon was well acted
[Your Mother]: Moon was mind bending and lovely and what's his nose Sam is *always* fun.
[LanX]: the new BS Galactica series is my all time SoapFiction favourite ...
[Your Mother]: I even liked "The Arrival" and I generally hate time travel (oriented) stuff.
[Your Mother]: Gotta go. Stay out of trouble.
[LanX]: no worries ... mainland Europe is safe ;-P
[LanX]: ??? (probably my English is to limited)
[LanX]: too

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (10)
As of 2018-03-19 15:10 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (240 votes). Check out past polls.