I want to fire off a dozen threads, and keep track of which one is doing what in case one of them hangs. (They are all running diagnostic commands on networking equipment.) Unfortunately, I can't seem to save the thread ids in any useful way. Here's sample code:
#! perl -slw use 5.012_000; use strict; use threads; use Data::Dumper; my %toc; for my $i (1..3) { my $id = async { sleep 3*$i+5; $i }; print Dumper($id); $toc{$id} = "starting thread $id, with \$i = $i"; } print Dumper(\%toc); sub joinable { threads->list( threads::joinable ) }; sub running { threads->list( threads::running ) }; while (my $count = scalar running) { sleep 1 until joinable; print "$toc{$_} => ", $_->join, "\n" for joinable; } print "all post-commands are complete\n";
When I run the above, I get back this, which looks like my thread ids are getting converted to "threads=XXX" when I try to use them as hash keys:
$VAR1 = bless( do{\(my $o = '25730180')}, 'threads' ); $VAR1 = bless( do{\(my $o = '26709332')}, 'threads' ); $VAR1 = bless( do{\(my $o = '27250780')}, 'threads' ); $VAR1 = { 'threads=SCALAR(0x187b454)' => 'starting thread threads=SCAL +AR(0x187b4 54), with $i = 3', 'threads=SCALAR(0x193d404)' => 'starting thread threads=SCAL +AR(0x193d4 04), with $i = 1', 'threads=SCALAR(0x193d5b4)' => 'starting thread threads=SCAL +AR(0x193d5 b4), with $i = 2' }; Use of uninitialized value within %toc in concatenation (.) or string +at test.pl line 25. => 1 Use of uninitialized value within %toc in concatenation (.) or string +at test.pl line 25. => 2 Use of uninitialized value within %toc in concatenation (.) or string +at test.pl line 25. => 3 all post-commands are complete
Anyone got any ideas? Thanks!

In reply to thread ids stringifying wrong in v5.12.4? by samwyse

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.