File-FDkeeper

 view release on metacpan or  search on metacpan

FDkeeper/Client.pm  view on Meta::CPAN

package File::FDkeeper::Client ;
@ISA = qw(File::FDkeeper) ;

use strict ;
use File::FDkeeper ;
use File::FDpasser ;
use Carp ;


sub new {
	my $class = shift ;
	my $path = shift ;
	my %args = @_ ;

	my $this = {} ;
	$this->{path} = $path ;
	bless($this, $class) ;

	while (my ($k, $v) = each %args){
		croak("Invalid attribute '$k'") ;
	}

	my $client = endp_connect($path) ;
	croak("Error connecting to server endpoint '$path': $!") unless $client ;
	$client->autoflush(1) ;
	$this->{client} = $client ;

	return $this ;
}


sub put {
	my $this = shift ;
	my $fh = shift ;

	if (UNIVERSAL::isa($fh, 'GLOB')){
		$fh = *{$fh}{IO} ;
	}

	$this->_init_cmd('put') ;
	if (! send_file($this->{client}, $fh)){
		close($this->{client}) ;
		croak("Error sending filehandle: $!") ;
	}
	my @ret = $this->_wrap_up() ;

	return undef unless $ret[0] ;

	# For some reason, the filehandle needs to be closed now
	# if we want to get it back later.
	close($fh) ;
	
	return $ret[1] ;
}


sub get {
	my $this = shift ;
	my $fhid = shift ;

	$this->_init_cmd("get$fhid\n") ;
	my @ret = $this->_wrap_up() ;

	return ($ret[0] ? $ret[2] : undef) ;
}


sub del {
	my $this = shift ;
	my $fhid = shift ;

	$this->_init_cmd("del$fhid\n") ;
	my @ret = $this->_wrap_up() ;

	return $ret[0] ;
}


sub cnt {
	my $this = shift ;
	my $fhid = shift ;

	$this->_init_cmd("cnt") ;
	my @ret = $this->_wrap_up() ;



( run in 0.619 second using v1.01-cache-2.11-cpan-e1769b4cff6 )