Hi Perl Community, I am trying to write a script that retrieves a list of hostnames from a mySql database, and then retrieve some information from them simultaneously with threads. The list of hostnames will be stored in a hash which will have the information about the host stored as a hash corresponding to each hosts. So the desired structure of my hash would be like so:
'1' => { 'hostname' => 'somehost@somewhere.com', 'host_info' => { 'info' => 'valueA', 'info2' => 'valueB', 'info3' => 'valueA' }, }, '2' => { 'hostname' => 'someotherhost@somewhere.com', 'host_info' => { 'info' => 'valueA', 'info2' => 'valueB', 'info3' => 'valueA' }, }, }
Here is my code that returns, "Invalid value for shared scalar" erros:
my %host_info : shared; my $host_info_REF = \%host_info; my $dbc= DBI-connect("DBI:mysql:database=mydb;IPADDRESS, 'user, 'passo +wrd"); my $SQL = "SELECT * FROM mydb.HOSTNAMES;"; my $st = dbc->prepare($SQL); $st->execute(); my $row = 0; while (my $tmp = $st->fetchrow_hashref()){ for my $key (keys %$tmp){ $host_info_REF->{$row}->{$key} = $tmp->{$key}; } $row++; } my @threads; my %hostHash; my $hostHashRef = \%hostHash; foreach my $current_host( keys %host_info ){ $hostHashRef->{"curHost"}=$current_host; $hostHashRef->{"href"}=$host_info_REF; push (@threads, threads -> create (\&get_info, $hostHashRef)); } sub get_info[ #Get some info and add to hash }

It crashes at the point where I am trying to assign the results from my query to my hash. "$host_info_REF->{$row}->{$key} = $tmp->{$key};". I believe it has something to do with my "$row" counter.


In reply to 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.