uhm, I don't agree on the necessary behaviour part, and I think you are mixing things up.

first of all, I'm returning the value of a variable, and this should work with any Perl variable, and it has nothing to do with scope. example:

sub foo { my $scoped_var = 42; return $scoped_var; } say foo();

of course the value "42" does not go out of scope. it would be very unkind if it did :-)

second, the sub that defines the variable is running in the same thread as main::. the anonymous sub I pass to threads->create() does run in another thread, and that's exactly why the variable is shared. but I'm not switching threads between the declaration and the usage outside of the sub.

I know that our works, as defining the variable outside of the sub does, but why? there is no mention in the threads::shared documentation about the fact that shared variables must be declared globally. furthermore, if you do the same thing with a scalar variable (eg. define it shared in a sub and returns it to the caller) everything works.

also, if you read my first post carefully, you will see that the keys of the hash (which are created in another thread as well) are effectively returned. just the values not. so it seems to me that there is (or better said there was, according to dave_the_m's reply below) some bug with container variables, threads::shared and scoping.

cheers,
Aldo

King of Laziness, Wizard of Impatience, Lord of Hubris


In reply to Re^2: bug in threads::shared or is it just me? by dada
in thread bug in threads::shared or is it just me? by dada

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.