Version 1.31 of the threads module has been uploaded to CPAN. This version adds the capability to specify the context to be used for the ->join() call.
THREAD CONTEXT

As with subroutines, the type of value returned from a thread's entry point function may be determined by the thread's context: list, scalar or void. The thread's context is determined at thread creation. This is necessary so that the context is available to the entry point function via wantarray(). The thread may then specify a value of the appropriate type to be returned from ->join().

Explicit context

Because thread creation and thread joining may occur in different contexts, it may be desirable to state the context explicitly to the thread's entry point function. This may be done by calling ->create() with a parameter hash as the first argument:
my $thr = threads->create({'context' => 'list'}, \&foo); ... my @results = $thr->join();
In the above, the threads object is returned to the parent thread in scalar context, and the thread's entry point function foo will be called in list context such that the parent thread can receive a list from the ->join() call. Similarly, if you need the threads object, but your thread will not be returning a value (i.e., void context), you would do the following:
my $thr = threads->create({'context' => 'void'}, \&foo); ... $thr->join();
The context type may also be used as the key in the parameter hash followed by a true value:
threads->create({'scalar' => 1}, \&foo); ... my ($thr) = threads->list(); my $result = $thr->join();

Implicit context

If not explicitly stated, the thread's context is implied from the context of the ->create() call:
# Create thread in list context my ($thr) = threads->create(...); # Create thread in scalar context my $thr = threads->create(...); # Create thread in void context threads->create(...);

Remember: There's always one more bug.

In reply to Explicit thread context by jdhedden

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.