http://qs321.pair.com?node_id=964347


in reply to Re: bug in threads::shared or is it just me?
in thread bug in threads::shared or is it just me?

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