G'day xajin25,
Welcome to the monastery.
You declare and initialise:
my $row = 0;
You then increment $rowRef which is neither declared nor initialised. Did you mean: ++$row? use strict; would have picked that up; I also recommend you add use warnings; (see strict and warnings). I don't think that's causing your error, though.
You also have "my $keyRef = \$key;" but you don't subsequently make any use of $keyRef. What was your intent with this piece of code?
Your code is incomplete and I don't know what you haven't included; however, I would draw your attention to this from perlthrtut - Shared And Unshared Data:
In the case of a shared array, all the array's elements are shared, and
for a shared hash, all the keys and values are shared. This places
restrictions on what may be assigned to shared array and hash elements:
only simple values or references to shared variables are allowed - this
is so that a private variable can't accidentally become shared. A bad
assignment will cause the thread to die.
Update: This was a poor guess and has been stricken:
I'm wondering if instead of
$host_info_REF->{$row}->{$key} = $tmp->{$key};
you perhaps want
$host_info{$row}{$key} = $tmp->{$key};
Here's the type of behaviour I think you're probably after:
#!/usr/bin/env perl use strict; use warnings; use threads::shared; use Data::Dumper; my @db_data = ( { hostname => 'h0', hinfo0 => 'A', hinfo1 => 'B', hinfo2 => 'C' }, { hostname => 'h1', hinfo0 => 'X', hinfo1 => 'Y', hinfo2 => 'Z' }, ); my %host_info : shared; my $row = 0; for my $db_data (@db_data) { $host_info{$row} = shared_clone({ hostname => delete $db_data->{hostname}, host_info => $db_data }); ++$row; } print Dumper \%host_info;
Output:
$ pm_thr_compdat.pl $VAR1 = { '1' => { 'hostname' => 'h1', 'host_info' => { 'hinfo0' => 'X', 'hinfo1' => 'Y', 'hinfo2' => 'Z' } }, '0' => { 'hostname' => 'h0', 'host_info' => { 'hinfo0' => 'A', 'hinfo1' => 'B', 'hinfo2' => 'C' } } };
You might also find useful information in threads and threads::shared.
-- Ken
In reply to Re: Threads, DBI, Shared Variables problem
by kcott
in thread Threads, DBI, Shared Variables problem
by xajin25
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |