title: Perl threading of object method
body: <p>Perl rookie here, so please be gentle :)</p>
  <p>I've written following code to keep track of my dogs when I'm hunting (not really). Every time a dog finds a duck, it signals the main thread, which then collects information from each of the dogs in the pack.</p>
  <pre><code>#!/usr/bin/env perl
  
  use strict;
  use warnings;
  use v5.14;
  
  use threads;
  
  {
      package Dog;
  
      sub new {
          my ($class, $name, $dt) = @_;
          my $self = {
              dt =&gt; $dt,      # will find a duck every $dt seconds
              name =&gt; $name,
              ducksfound =&gt; 0
          };
          bless $self, $class;
      }
  
      sub hunt {
          #
          # the &quot;thread&quot; method -- the dog will hang around for $dt seconds,
          # then alert the main thread by sending SIGUSR1
          #
          my $self = shift;
          while (1) {
              sleep $self-&gt;{dt};
              $self-&gt;{ducksfound} += 1;
              kill USR1 =&gt; $$;
          }
      }
  
      sub bark {
          my $self = shift;
          sprintf &quot;%s: found %d ducks!&quot;, ($self-&gt;{name}, $self-&gt;{ducksfound});
      }
  
      1;
  }
  
  my @dogs;
  
  $SIG{USR1} = sub {
      say join &quot;, &quot;, map { $_-&gt;bark } @dogs;
  };
  
  
  push @dogs, Dog-&gt;new(&quot;Labrador&quot;, 1);
  push @dogs, Dog-&gt;new(&quot;Retriever&quot;, 2);
  push @dogs, Dog-&gt;new(&quot;Shepherd&quot;, 3);
  
  threads-&gt;create( sub { $_-&gt;hunt } ) for @dogs;
  $_-&gt;join for threads-&gt;list;
  </code></pre>
  <p><strong>Expected output of the above code would be something like:</strong></p>
  <blockquote>
  <p>Labrador: found 1 ducks!, Retriever: found 0 ducks!, Shepherd: found 0 ducks!</p>
  <p>Labrador: found 2 ducks!, Retriever: found 0 ducks!, Shepherd: found 0 ducks!</p>
  <p>Labrador: found 3 ducks!, Retriever: found 0 ducks!, Shepherd: found 0 ducks!</p>
  <p>Labrador: found 3 ducks!, Retriever: found 1 ducks!, Shepherd: found 0 ducks!</p>
  <p>Labrador: found 4 ducks!, Retriever: found 1 ducks!, Shepherd: found 0 ducks!</p>
  <p>Labrador: found 5 ducks!, Retriever: found 1 ducks!, Shepherd: found 0 ducks!</p>
  <p>Labrador: found 6 ducks!, Retriever: found 1 ducks!, Shepherd: found 0 ducks!</p>
  <p>Labrador: found 6 ducks!, Retriever: found 1 ducks!, Shepherd: found 0 ducks!</p>
  <p>Labrador: found 6 ducks!, Retriever: found 1 ducks!, Shepherd: found 1 ducks!</p>
  </blockquote>
  <p><strong>Instead, what I get is the following:</strong></p>
  <blockquote>
  <p>Labrador: found 1 ducks!, Retriever: found 0 ducks!, Shepherd: found 0 ducks!</p>
  <p>Labrador: found 2 ducks!, Retriever: found 0 ducks!, Shepherd: found 0 ducks!</p>
  <p>Labrador: found 3 ducks!, Retriever: found 0 ducks!, Shepherd: found 0 ducks!</p>
  <p>Labrador: found 0 ducks!, Retriever: found 1 ducks!, Shepherd: found 0 ducks!</p>
  <p>Labrador: found 4 ducks!, Retriever: found 0 ducks!, Shepherd: found 0 ducks!</p>
  <p>Labrador: found 5 ducks!, Retriever: found 0 ducks!, Shepherd: found 0 ducks!</p>
  <p>Labrador: found 0 ducks!, Retriever: found 2 ducks!, Shepherd: found 0 ducks!</p>
  <p>Labrador: found 0 ducks!, Retriever: found 0 ducks!, Shepherd: found 1 ducks!</p>
  </blockquote>
  <p>Notice how every dog's duck count resets to zero whan another dog is speaking.</p>
  <p>Any insights as to which particular footnote I must have glossed over while reading the Llama?</p>