I am just beginning to experiment with threads. The following code works fine:
#!/usr/bin/perl -w use strict; my $argument=10; my %hash = sub_returning_hash($argument); print "a = "; print $hash{"a"}; print "\n"; print "b = "; print $hash{"b"}; print "\n"; print "c = "; print $hash{"c"}; print "\n"; sub sub_returning_hash { my $argument=shift; my $a=$argument+1 ; my $b=$argument+2 ; my $c=$argument+3 ; my %output_record = ( "a" => "$a", "b" => "$b", "c" => "$c" ); return %output_record; } # sub_returning_hash
and returns:
a = 11 b = 12 c = 13
Now I want to execute the subroutine as a thread, so I try:
#!/usr/bin/perl -w use strict; use threads; my $argument=10; my $thr = threads->new(\&sub_returning_hash, $argument); my %hash = $thr->join; print "a = "; print $hash{"a"}; print "\n"; print "b = "; print $hash{"b"}; print "\n"; print "c = "; print $hash{"c"}; print "\n"; sub sub_returning_hash { my $argument=shift; my $a=$argument+1 ; my $b=$argument+2 ; my $c=$argument+3 ; my %output_record = ( "a" => "$a", "b" => "$b", "c" => "$c" ); return %output_record; } # sub_returning_hash
but I receive:
Odd number of elements in hash assignment at test.pl line 8. Use of uninitialized value in print at test.pl line 9. a = Use of uninitialized value in print at test.pl line 10. b = Use of uninitialized value in print at test.pl line 11. c =
I tried the same returning a scalar instead of an hash, and it works fine. Any suggestion to be able to access the hash returned by the subroutine when running as a thread? Thanks!

In reply to Threads that return hash by hilbert

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.