#!/usr/bin/perl
use strict;
use warnings;

use WWW::Curl::Easy;
use WWW::Curl::Share;
use Storable qw(freeze  thaw);
use Data::Dumper;
use IPC::ShareLite;


#my $count = 5;
#while ($count--) {
#    if (my $pid = fork) {
#	&doloop;
#    }
#    else {
	&doloop;
#   }
#}
exit;

INIT {
    my $curlsh = new WWW::Curl::Share;
    $curlsh->setopt(CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
    print "Share is $curlsh\n";

    # put it in IPC

    my $shm = new IPC::ShareLite( -key     => 1971,
				  -create  => 'yes',
				  -destroy => 'yes' ) or die $!;

    $shm->store( Data::Dumper->Dump([$curlsh],['curlsh']));

    sub doloop
      {
	  my $curl;
#	  unless ($curl) {
	      $curl = new WWW::Curl::Easy;
	      print "Thawed share is ".($shm->fetch)."\n";
	  eval($shm->fetch);
#	      $curl->setopt(CURLOPT_SHARE, (thaw($shm->fetch))[0]);

	      $curl->setopt(CURLOPT_VERBOSE, 1);
	      $curl->setopt(CURLOPT_DNS_CACHE_TIMEOUT, -1);
	      $curl->setopt(CURLOPT_CONNECT_ONLY, 1);
	      $curl->setopt(CURLOPT_FORBID_REUSE, 1);
	      $curl->setopt(CURLOPT_URL, 'http://ftp.us.debian.org');
#	  }
	  $curl->perform;
      }
}


