<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Redis;

use warnings;
use strict;

use IO::Socket::INET;
use Data::Dump qw/dump/;
use Carp qw/confess/;

=head1 NAME

Redis - perl binding for Redis database

=cut

our $VERSION = '0.08';


=head1 DESCRIPTION

Pure perl bindings for L&lt;http://code.google.com/p/redis/&gt;

This version support git version 0.08 of Redis available at

L&lt;git://github.com/antirez/redis&gt;

This documentation
lists commands which are exercised in test suite, but
additinal commands will work correctly since protocol
specifies enough information to support almost all commands
with same peace of code with a little help of C&lt;AUTOLOAD&gt;.

=head1 FUNCTIONS

=head2 new

  my $r = Redis-&gt;new;

=cut

our $debug = $ENV{REDIS} || 0;

our $sock;
my $server = '127.0.0.1:6379';

sub new {
	my $class = shift;
	my $self = {};
	bless($self, $class);

	warn "# opening socket to $server";

	$sock ||= IO::Socket::INET-&gt;new(
		PeerAddr =&gt; $server,
		Proto =&gt; 'tcp',
	) || die $!;

	$self;
}

my $bulk_command = {
	set =&gt; 1,	setnx =&gt; 1,
	rpush =&gt; 1,	lpush =&gt; 1,
	lset =&gt; 1,	lrem =&gt; 1,
	sadd =&gt; 1,	srem =&gt; 1,
	sismember =&gt; 1,
	echo =&gt; 1,
};

# we don't want DESTROY to fallback into AUTOLOAD
sub DESTROY {}

our $AUTOLOAD;
sub AUTOLOAD {
	my $self = shift;

	my $command = $AUTOLOAD;
	$command =~ s/.*://;

	warn "## $command ",dump(@_) if $debug;

	my $send;

	if ( defined $bulk_command-&gt;{$command} ) {
		my $value = pop;
		$value = '' if ! defined $value;
		$send
			= uc($command)
			. ' '
			. join(' ', @_)
			. ' ' 
			. length( $value )
			. "\r\n$value\r\n"
			;
	} else {
		$send
			= uc($command)
			. ' '
			. join(' ', @_)
			. "\r\n"
			;
	}

	warn "&gt;&gt; $send" if $debug;
	print $sock $send;

	if ( $command eq 'quit' ) {
		close( $sock ) || die "can't close socket: $!";
		return 1;
	}

	my $result = &lt;$sock&gt; || die "can't read socket: $!";
	warn "&lt;&lt; $result" if $debug;
	my $type = substr($result,0,1);
	$result = substr($result,1,-2);

	if ( $command eq 'info' ) {
		my $hash;
		foreach my $l ( split(/\r\n/, __sock_read_bulk($result) ) ) {
			my ($n,$v) = split(/:/, $l, 2);
			$hash-&gt;{$n} = $v;
		}
		return $hash;
	} elsif ( $command eq 'keys' ) {
		my $keys = __sock_read_bulk($result);
		return split(/\s/, $keys) if $keys;
		return;
	}

	if ( $type eq '-' ) {
		confess $result;
	} elsif ( $type eq '+' ) {
		return $result;
	} elsif ( $type eq '$' ) {
		return __sock_read_bulk($result);
	} elsif ( $type eq '*' ) {
		return __sock_read_multi_bulk($result);
	} elsif ( $type eq ':' ) {
		return $result; # FIXME check if int?
	} else {
		confess "unknown type: $type", __sock_read_line();
	}
}

sub __sock_read_bulk {
	my $len = shift;
	return undef if $len &lt; 0;

	my $v;
	if ( $len &gt; 0 ) {
		read($sock, $v, $len) || die $!;
		warn "&lt;&lt; ",dump($v),$/ if $debug;
	}
	my $crlf;
	read($sock, $crlf, 2); # skip cr/lf
	return $v;
}

sub __sock_read_multi_bulk {
	my $size = shift;
	return undef if $size &lt; 0;

	$size--;

	my @list = ( 0 .. $size );
	foreach ( 0 .. $size ) {
		$list[ $_ ] = __sock_read_bulk( substr(&lt;$sock&gt;,1,-2) );
	}

	warn "## list = ", dump( @list ) if $debug;
	return @list;
}

1;

__END__

=head1 Connection Handling

=head2 quit

  $r-&gt;quit;

=head2 ping

  $r-&gt;ping || die "no server?";

=head1 Commands operating on string values

=head2 set

  $r-&gt;set( foo =&gt; 'bar' );

  $r-&gt;setnx( foo =&gt; 42 );

=head2 get

  my $value = $r-&gt;get( 'foo' );

=head2 mget

  my @values = $r-&gt;mget( 'foo', 'bar', 'baz' );

=head2 incr

  $r-&gt;incr('counter');

  $r-&gt;incrby('tripplets', 3);

=head2 decr

  $r-&gt;decr('counter');

  $r-&gt;decrby('tripplets', 3);

=head2 exists

  $r-&gt;exists( 'key' ) &amp;&amp; print "got key!";

=head2 del

  $r-&gt;del( 'key' ) || warn "key doesn't exist";

=head2 type

  $r-&gt;type( 'key' ); # = string

=head1 Commands operating on the key space

=head2 keys

  my @keys = $r-&gt;keys( '*glob_pattern*' );

=head2 randomkey

  my $key = $r-&gt;randomkey;

=head2 rename

  my $ok = $r-&gt;rename( 'old-key', 'new-key', $new );

=head2 dbsize

  my $nr_keys = $r-&gt;dbsize;

=head1 Commands operating on lists

See also L&lt;Redis::List&gt; for tie interface.

=head2 rpush

  $r-&gt;rpush( $key, $value );

=head2 lpush

  $r-&gt;lpush( $key, $value );

=head2 llen

  $r-&gt;llen( $key );

=head2 lrange

  my @list = $r-&gt;lrange( $key, $start, $end );

=head2 ltrim

  my $ok = $r-&gt;ltrim( $key, $start, $end );

=head2 lindex

  $r-&gt;lindex( $key, $index );

=head2 lset

  $r-&gt;lset( $key, $index, $value );

=head2 lrem

  my $modified_count = $r-&gt;lrem( $key, $count, $value );

=head2 lpop

  my $value = $r-&gt;lpop( $key );

=head2 rpop

  my $value = $r-&gt;rpop( $key );

=head1 Commands operating on sets

=head2 sadd

  $r-&gt;sadd( $key, $member );

=head2 srem

  $r-&gt;srem( $key, $member );

=head2 scard

  my $elements = $r-&gt;scard( $key );

=head2 sismember

  $r-&gt;sismember( $key, $member );

=head2 sinter

  $r-&gt;sinter( $key1, $key2, ... );

=head2 sinterstore

  my $ok = $r-&gt;sinterstore( $dstkey, $key1, $key2, ... );

=head1 Multiple databases handling commands

=head2 select

  $r-&gt;select( $dbindex ); # 0 for new clients

=head2 move

  $r-&gt;move( $key, $dbindex );

=head2 flushdb

  $r-&gt;flushdb;

=head2 flushall

  $r-&gt;flushall;

=head1 Sorting

=head2 sort

  $r-&gt;sort("key BY pattern LIMIT start end GET pattern ASC|DESC ALPHA');

=head1 Persistence control commands

=head2 save

  $r-&gt;save;

=head2 bgsave

  $r-&gt;bgsave;

=head2 lastsave

  $r-&gt;lastsave;

=head2 shutdown

  $r-&gt;shutdown;

=head1 Remote server control commands

=head2 info

  my $info_hash = $r-&gt;info;

=head1 AUTHOR

Dobrica Pavlinusic, C&lt;&lt; &lt;dpavlin at rot13.org&gt; &gt;&gt;

=head1 BUGS

Please report any bugs or feature requests to C&lt;bug-redis at rt.cpan.org&gt;, or through
the web interface at L&lt;http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Redis&gt;.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.




=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Redis
	perldoc Redis::List
	perldoc Redis::Hash


You can also look for information at:

=over 4

=item * RT: CPAN's request tracker

L&lt;http://rt.cpan.org/NoAuth/Bugs.html?Dist=Redis&gt;

=item * AnnoCPAN: Annotated CPAN documentation

L&lt;http://annocpan.org/dist/Redis&gt;

=item * CPAN Ratings

L&lt;http://cpanratings.perl.org/d/Redis&gt;

=item * Search CPAN

L&lt;http://search.cpan.org/dist/Redis&gt;

=back


=head1 ACKNOWLEDGEMENTS


=head1 COPYRIGHT &amp; LICENSE

Copyright 2009 Dobrica Pavlinusic, all rights reserved.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.


=cut

1; # End of Redis
</pre></body></html>