It's a good point, and a good thought, but it doesn't help (much) unfortunately.

Whilst disabling the prototype allows me to pass an anonymous hash directly to share(), it still causes any contents in that hash to be discarded.

It does mean that you can share and assign an empty anonymous hash directly into the parent shared hash, which avoids the temp var at the slight inconvenience of obscurity, but it still doesn't make up for the lack autovivification :(

#! perl -slw use strict; use Data::Dumper; use threads; use threads::shared; my %hash1 : shared; ## Disabling the prototype allows the call ## to accept an anonymous hash $hash1{A} = &share( { 1 => 'b' } ); ## But inspecting the results on a different thread ## show that the contents of the hash were discarded. async{ print Dumper \%hash1; }->detach; =output $VAR1 = { 'A' => {} }; =cut sleep 2; ## Give the async thread a timeslice my %hash2 : shared; ## This also happens if you accomodate the prototype ## using an intermediate temp variable. my $temp = { 1 => 'b' }; share $temp; print Dumper $temp; ## Anon. hash shared, but emptied. =output $VAR1 = {}; =cut ## The only way I had found to do it is: my $temp2 = {}; ## Create a temp var ref to the anon. hash share $temp2; ## share the temp var. $temp2->{1} = 'b'; ## populate the 'anon' hash print Dumper $temp2; ## Now the contents are available =output $VAR1 = { '1' => 'b' }; =cut ## Then assign the shared 'anon' hashref ## To the shared hash. $hash2{A} = $temp2; async{ print Dumper \%hash2 }->detach; ## Now we see it =output $VAR1 = { 'A' => { '1' => 'b' } }; =cut sleep 2; ## Give the async thread a timeslice ## However, by bypassing the prototype you can avoid a step my %hash3 : shared; $hash3{A} = &share( {} ); $hash3{A}{1} = 'b'; async{ print Dumper \%hash3; }->detach; =output $VAR1 = { 'A' => { '1' => 'b' } }; =cut sleep 2; ## Give the async thread a timeslice

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail        "Time is a poor substitute for thought"--theorbtwo
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

In reply to Re^3: Declaring/Assigning thread shared vars by BrowserUk
in thread Declaring/Assigning thread shared vars by Anonymous Monk

Title:
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, details, 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, summary, 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.