Patch for Cache/Memcached.pm

Jamie McCarthy jamie at mccarthy.vg
Sat Aug 11 19:38:12 UTC 2007


While chasing down what I think might be a bug in the perl client
library, I found another couple of things.  Here's a patch that
fixes those things.

The first part corrects a bug where _single_sock would not work if
new() were called with its 'servers' arrayref consisting of only one
element, itself an arrayref specifying both an IP/port and a weight,
i.e.:

worked: Cache::Memcached->new({ servers => [   '1.2.3.4:5'      ] })
failed: Cache::Memcached->new({ servers => [ [ '1.2.3.4:5', 1 ] ] })

Giving a weight when there's only a single server is a slightly
silly thing to do but it's legal and should work.

The second part just eliminates a "Use of uninitialized value"
warning which pops up during normal operation.

--- Memcached.pm.orig   2007-07-17 13:47:40.000000000 -0400
+++ Memcached.pm        2007-08-10 12:32:37.000000000 -0400
@@ -103,7 +103,13 @@
 
     $self->{'_single_sock'} = undef;
     if (@{$self->{'servers'}} == 1) {
-        $self->{'_single_sock'} = $self->{'servers'}[0];
+        # Set the single server correctly, whether the list is an
+        # arrayref with one scalar (one server with implied weight
+        # of 1) or an arrayref with one arrayref (one server and
+        # one explicit, though useless, weight).
+        my $server = $self->{'servers'}[0];
+        $server = $server->[0] if ref($server) eq 'ARRAY';
+        $self->{'_single_sock'} = $server;
     }
 
     return $self;
@@ -242,7 +248,7 @@
     if ( index($host, '/') != 0 )
     {
         # if a preferred IP is known, try that first.
-        if ($self && $self->{pref_ip}{$ip}) {
+        if ($self && $ip && $self->{pref_ip}{$ip}) {
             socket($sock, PF_INET, SOCK_STREAM, $proto);
             my $prefip = $self->{pref_ip}{$ip};
             $sin = Socket::sockaddr_in($port,Socket::inet_aton($prefip));

-- 
  Jamie McCarthy
 http://mccarthy.vg/
  jamie at mccarthy.vg



More information about the memcached mailing list