Re: Perl threads - capturing value returned from sub



On Feb 27, 7:21 am, zentara <zent...@xxxxxxxxxxxxxx> wrote:
On 26 Feb 2007 17:24:40 -0800, "Eric" <ecarl...@xxxxxxxxxx> wrote:



Cancel this one; I found the answer, which was to just assign a
variable to the join:

my $result = $t->join;

Seems like I always have a breakthrough immediately after I post to
this site! :)

Eric

Just to amplify slightly, you can return arrays too.
#!/usr/bin/perl
use warnings;
use strict;
use threads;
use threads::shared;

# join() does three things: it waits for a thread to exit,
# cleans up after it, and returns any data the thread may
# have produced.

my $thr = threads->new(\&sub1);

my $return = $thr->join;

print "Thread returned @$return\n";

#hold for key input
<>;
##########################################################
sub sub1 {
my @values = ('1',2, 'ten');
print "@values\n";

while(1){sleep 1}

return \@values;

}

__END__

--
I'm not really a human, but I play one on earth.http://zentara.net/japh.html

Thanks - this is useful information.

Eric

.



Relevant Pages

  • Re: Convert Arrays into hashes
    ... How can i convert both of these arrays into %hash, ... but in practice if @array2 held literally numbers from 0 ... Also you should always use warnings and strict to catch errors. ...
    (perl.beginners)
  • Re: Convert Arrays into hashes
    ... How can i convert both of these arrays into %hash, ... but in practice if @array2 held literally numbers from 0 ... Also you should always use warnings and strict to catch errors. ...
    (perl.beginners)
  • Re: Invisible Array Loop Counter?
    ... > within the loop: ... > use strict; ... > use warnings; ... defined operation on arrays). ...
    (comp.lang.perl.misc)
  • Re: simple pointer operations (newbe)
    ... I know that it is very critical to 'play' with pointers on this way;) ... use strict; ... use warnings; ...
    (comp.lang.perl.misc)
  • Re: Sharing variables between modules
    ... use strict; ... use warnings; ... # Note that this must also come *before* the use Foo line. ... package Foo; ...
    (comp.lang.perl.misc)