Yeah, you are making the assumption that a thread sub code block returns automatically, you need to join it, to get the return value.
#!/usr/bin/perl use warnings; use threads; use strict; $|=1; #turn off buffering my $returnn = ""; my $thread = threads->new(sub { print "Run the thread! Variable should be set\n"; sleep(3); $returnn = "bla ble bli"; }); my $result = $thread->join; print $result,"\n";
Notice that the join statement, will block the main program until the thread finishes.

There are other alternatives,i if you want to detect when the thread finishes, using threads::shared. Here I also detach the thread, so you don't need to join it.

#!/usr/bin/perl use warnings; use threads; use threads::shared; use strict; $|=1; #turn off buffering my $returnn : shared; #declare as shared before setting value $returnn = ''; my $thread = threads->new(sub { print "Run the thread! Variable should be set\n"; sleep(3); $returnn = "bla ble bli"; })->detach(); while(1){ if($returnn eq ''){ print "No return yet\n"; sleep 1; }else{ print "return->$returnn\n"; last; } } print "Hit enter to exit\n"; <>;

I'm not really a human, but I play one on earth. flash japh

In reply to Re: How do I get a result from a thread? by zentara
in thread How do I get a result from a thread? by tadeufae

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.