Basically, I have an array called @host_array that contains information about the hosts that I want to see whether they are alive or dead. I need to check their current status against their old status and send an alert if something changes. Unfortunately, I can't seem to get the reference right. That means that when I update a value in the $task variable within sub 'pinger', it doesn't actually update the original array; just its local version of the data.

I want to pass a reference to the entire array to run_threaded and then a reference to a slice of that array from run_threaded to pinger.

populateHosts(1); while (1) { run_threaded(10,\&pinger,\@host_array); } sub run_threaded { my ($threads,$func_to_thread, $hosts) = @_; my (%threads); my $n = 0; my $debug = 1; my $m = $threads-1 > $#{$hosts} ? $#{$hosts} : $threads-1; while (1) { last if $n >= $#{$hosts}+1; foreach my $host (@$hosts[$n..$m]) { #Still a reference here! I can change the value of $host[ +x] and it will stick. $threads{$host} = new threads $func_to_thread, $host; } map { $threads{$_}->join if $threads{$_}; } @{$hosts}[$n..$m]; # work out the next range of instances to work on $n = $m == 0 ? 1 : $m+1; $m = $n+$threads-1 < $#{$hosts} ? $n+$threads-1 : $#{$hosts}; } } sub pinger{ $task = shift; # Create pinger $p = Net::Ping->new("tcp"); $p->port_number($task->[4]); if( $p->ping($task->[3], 6) ) { print " PASS: @$task[2] "; $task->[5] = 1; #I want to update @host_array, but it's not a reference! } else { print " FAIL: @$task[2] "; $task->[5] = 0; #I want to update @host_array, but it's not a reference! } $task->[2] .= "-2"; $p->close(); } sub populateHosts { ($init) = @_; # DEFINE A MySQL QUERY $query = "SELECT * FROM `sites`"; # EXECUTE THE QUERY $db->query($query); my $set = $db->create_record_iterator; $i=0; while (my $rec = $set->each) { $host_array[$i][0] = $i; $host_array[$i][1] = $rec->[0]; $host_array[$i][2] = $rec->[1]; $host_array[$i][3] = $rec->[2]; $host_array[$i][4] = $rec->[3]; if ($init) { $host_array[$i][5] = $rec->[4]; $host_array[$i][8] = $rec->[5]; $host_array[$i][6] = 1; $host_array[$i][7] = time; } $i++; } }

There are some lines that I omitted for brevity, but the function should still be similar. Thanks!

By the way, the great thread function 'run_threaded' came from http://greg-techblog.blogspot.com/2009/06/threading-in-perl.html


In reply to Trouble passing an array reference to my threads by dh

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.