Your problem is that the return context (scalar/array/void) of the thread subroutine, is defined by the context in which that thread is created.
In your example, that means here:
my $thr = threads->new(\&sub_returning_hash, $argument);
Which is a scalar context. Hence, when the return is invoked here:
return %output_record;
It is the equivalent of
return scalar %output_record;
Which returns:
%h = qw[ a 11 b 12 c 13 ];; print scalar %h;; 3/8
And when that string is returned from the join and assigned to the hash in the calling code:
%r = scalar %h;; Odd number of elements in hash assignment
A complicated explanation, but thankfully, the solution is simple. Change the thread creation line to:
my( $thr ) = threads->new(\&sub_returning_hash, $argument);
Which puts the creation of the thread into an array (list) context.
Now the code will run warnings free and produce the desired result:
[11:51:36.25]C:\test>junk a = 11 b = 12 c = 13
Alternatively, you can mark the return context of the thread sub explicitly at creation time:
my $thr = threads->new( {'context' => 'list'}, \&sub_returning_hash, $ +argument );
which will achieve the same result. See the section labelled "THREAD CONTEXT" in the threads pod for details.
In reply to Re: Threads that return hash
by BrowserUk
in thread Threads that return hash
by hilbert
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |