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

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.