in reply to Threads, DBI, Shared Variables problem

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

Replies are listed 'Best First'.
Re^2: Threads, DBI, Shared Variables problem
by xajin25 (Initiate) on May 26, 2013 at 16:29 UTC
    Oops, sorry about the mismatched variable names, I was fiddling around to find a fix and thought the solution was to use a reference to "$row". These are only snippets so I understand the confusion. The ultimate goal of my script is to start with an existing hash containing hostnames, and then have threads to go ssh to each hostname, retrieve some information, and then add it to the hash. If this weren't threaded, it would have been simple, but it seems when you spin off a thread, it gets it's own local copy of variables and does not manipulate variables outside of it.
      I've updated the code to make it more clear. Also, would your example work with threads?
        "I've updated the code to make it more clear."

        You've added, modified and deleted code from your original post (OP). This potentially renders responses to your OP either meaningless or, at best, confusing. An example of how to update a post is contained in my original response. Please read "How do I change/delete my post?".

        "Also, would your example work with threads?"

        Why wouldn't it? What did you try? What problems did you encounter? Please read "How do I post a question effectively?".

        -- Ken