hilbert has asked for the wisdom of the Perl Monks concerning the following question:
and returns:#!/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
Now I want to execute the subroutine as a thread, so I try:a = 11 b = 12 c = 13
but I receive:#!/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
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!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 =
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Threads that return hash
by BrowserUk (Patriarch) on Apr 24, 2012 at 10:55 UTC | |
|
Re: Threads that return hash
by Anonymous Monk on Apr 24, 2012 at 10:48 UTC | |
|
Re: Threads that return hash
by Khen1950fx (Canon) on Apr 24, 2012 at 11:32 UTC | |
|
Re: Threads that return hash
by Anonymous Monk on Apr 24, 2012 at 12:00 UTC | |
by BrowserUk (Patriarch) on Apr 24, 2012 at 12:03 UTC | |
by locked_user sundialsvc4 (Abbot) on Apr 24, 2012 at 12:52 UTC |