Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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.

In reply to Re: Shared hash within shared hash of hash??? by muba
in thread Shared hash within shared hash of hash??? by ISAI student

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (4)
    As of 2018-05-28 02:12 GMT
    Find Nodes?
      Voting Booth?