title: perl threading problem
body: <p>I'm writing a multithreaded website uptime checker in perl, and here is the basic code so far (includes only threading part):</p>
  
  <pre><code>#!/usr/bin/perl
  
  use LWP::UserAgent; 
  use Getopt::Std; 
  use threads; 
  use threads::shared; 
  
  my $maxthreads :shared = 50;
  my $threads :shared = 0;
  
  print "Website Uptime Checker\n";
  my $infilename = $ARGV[0];
  chomp($infilename);
  open(INFILE, $infilename);
  my $outfilename = $ARGV[1];
  chomp($outfilename);
  open(OUTFILE, "&gt;" . $outfilename);
  OUTFILE-&gt;autoflush(1);
  while ($site = &lt;INFILE&gt;)
  {
      chomp($site);
      while (1)
      {
          if ($threads &lt; $maxthreads)
          {
              $threads++;
              my $thr = threads-&gt;create(\&amp;check_site, $site);
              $thr-&gt;detach();
              last;
          }
          else
          {
              sleep(1);
          }
      }
  }
  while ($threads &gt; 0)
  {
      sleep(1);
  }
  
  sub check_site
  {
      $server = $_[0];
      print "$server\n";
      $threads--;
  }
  </code></pre>
  
  <p>It gives an error after a while:</p>
  
  <blockquote>
    <p>Can't call method "detach" on an undefined value at C:\perl\webchecker.pl line 28,  line 245.</p>
  </blockquote>
  
  <p>What causes this error? I know it is at detach, but what am I doing wrong in my code? Windows shows lots of free memory, so it should not be the computer running out of memory, this error occurs even if I set $maxthreads as low as 10 or possibly even lower.</p>