Paul Sobey wrote:

> Ended up writing this, which seems to work. Not sure it's perfect but I get 
> copies of the hash that look identical with Data::Dumper...
> 
> Does anyone know a better way of doing this? Seems awfully long winded...

I'm wondering if it will make a difference if you quit using the compile time
syntax :
        my $copy : shared;
and switch to using just the run time share calls.

> $TestHash2 = ShareRecurse($TestHash);

This gets the same result (assuming you know what $TestHash is) :

my $TestHash2 = &share({});
$TestHash2 = $TestHash;

> sub ShareRecurse {
>       my $orig = shift;
>       my $ref = ref($orig);
>       if ($ref eq "SCALAR") {
>               my $copy : shared;
>               $copy = &share($orig);
>               return \$copy;
>       } elsif ($ref eq "ARRAY") {
>               my @copy : shared;
>               foreach my $value (@{$orig}) {
>                       my $ref = ref($value);
>                       if ($ref eq "SCALAR") {
>                               push @copy, ShareRecurse($value);
>                       } elsif ($ref eq "ARRAY") {
>                               push @copy, ShareRecurse($value);
>                       } elsif ($ref eq "HASH") {
>                               push @copy, ShareRecurse($value);
>                       } else {
>                               push @copy, $value;
>                       }
>               }
>               return [EMAIL PROTECTED];
>       } elsif ($ref eq "HASH") {
>               my %copy : shared;
>               foreach my $key (keys %{$orig}) {
>                       my $ref = ref($orig->{$key});
>                       if ($ref eq "SCALAR") {
>                               $copy{$key} = ShareRecurse($orig->{$key});
>                       } elsif ($ref eq "ARRAY") {
>                               $copy{$key} = ShareRecurse($orig->{$key});
>                       } elsif ($ref eq "HASH") {
>                               $copy{$key} = ShareRecurse($orig->{$key});
>                       } else {
>                               $copy{$key} = $orig->{$key};
>                       }
>               }
>               return \%copy;
>       }
> }
> 
>  
> 
> -----Original Message-----
> From: $Bill Luebkert [mailto:[EMAIL PROTECTED] 
> Sent: 12 November 2004 03:00
> To: Paul Sobey
> Cc: [EMAIL PROTECTED]
> Subject: Re: threads::shared
> 
> Paul Sobey wrote:
> 
> 
>>I'm using XML::Simple to read in two files, and generate two hashrefs. I
>>want to combine these two into a big hashref, like this:
>> 
>>my $combined = { %{$a}, %{$b} };
>> 
>>This works fine, until I want to share the $combined hash to make it
>>visible across several threads.
>> 
>>As an example, consider the following:
>> 
>>use threads;
>>use threads::shared;
>> 
>>
>>my $TestHash = {
>> bla => "bla",
>> wibble => [ "wibble", "wibble" ],
>> blargh => {
>>  I => "Really",
>>  Wish => "This",
>>  Would => "Work"
>> }
>>};
>> 
>>my $TestHash2 : shared;
>> 
>>$TestHash2 = $TestHash;
> 
> 
> I tried reading the docs for shared and they are cryptic at best - try :
> 
>       my $TestHash2 = &share({});
>       $TestHash2 = $TestHash;
> 
> in place of the above two lines.  I don't understand the syntax at all.
> 
> 
>>This fails in my system with the error "Invalid value for shared scalar
>>at D:\scripts\thread2.pl line 17.". I can understand why this should
>>cause problems, since none of the nested references are themselves
>>marked as shared. How can I generate a shared version of the hashes that
>>I can pass around between the threads? I was considering some sort of
>>recursion through the structure, creating new :shared copies of each value?
> 
> 


-- 
  ,-/-  __      _  _         $Bill Luebkert    Mailto:[EMAIL PROTECTED]
 (_/   /  )    // //       DBE Collectibles    Mailto:[EMAIL PROTECTED]
  / ) /--<  o // //      Castle of Medieval Myth & Magic http://www.todbe.com/
-/-' /___/_<_</_</_    http://dbecoll.tripod.com/ (My Perl/Lakers stuff)
_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to