diff -Naur Cache-Memcached-1.14.orig/Memcached.pm Cache-Memcached-1.14/Memcached.pm --- Cache-Memcached-1.14.orig/Memcached.pm 2004-07-27 17:07:04.000000000 +0000 +++ Cache-Memcached-1.14/Memcached.pm 2004-07-31 12:16:09.000000000 +0000 @@ -10,7 +10,9 @@ use strict; no strict 'refs'; use Storable (); -use Socket qw( MSG_NOSIGNAL PF_INET IPPROTO_TCP SOCK_STREAM ); +# patch for 5.005_03 +#use Socket qw( MSG_NOSIGNAL PF_INET IPPROTO_TCP SOCK_STREAM ); +use Socket qw( MSG_NOSIGNAL PF_INET SOCK_STREAM ); use IO::Handle (); use Time::HiRes (); use String::CRC32; @@ -45,11 +47,20 @@ my $PROTO_TCP; -our $SOCK_TIMEOUT = 2.6; # default timeout in seconds +# patch for 5.005_03 +#our $SOCK_TIMEOUT = 2.6; # default timeout in seconds +use vars qw($SOCK_TIMEOUT); +$SOCK_TIMEOUT = 2.6; # default timeout in seconds sub new { - my Cache::Memcached $self = shift; - $self = fields::new( $self ) unless ref $self; + # patch for 5.005_03 + #my Cache::Memcached $self = shift; + #$self = fields::new( $self ) unless ref $self; + my Cache::Memcached $self = shift; + { + no strict 'refs'; + $self = bless [\%{"$self\::FIELDS"}] unless ref $self; + } my ($args) = @_; @@ -165,9 +176,13 @@ # non-blocking at the end of this function if ($timeout) { - IO::Handle::blocking($sock, 0); + # patch for 5.005_03 + #IO::Handle::blocking($sock, 0); + my_blocking($sock, 0); } else { - IO::Handle::blocking($sock, 1); + # patch for 5.005_03 + #IO::Handle::blocking($sock, 1); + my_blocking($sock, 1); } my $ret = connect($sock, $sin); @@ -185,7 +200,9 @@ } unless ($timeout) { # socket was temporarily blocking, now revert - IO::Handle::blocking($sock, 0); + # patch for 5.005_03 + #IO::Handle::blocking($sock, 0); + my_blocking($sock, 0); } # from here on, we use non-blocking (async) IO for the duration @@ -392,7 +409,8 @@ my $sock = $self->get_sock($key); return 0 unless $sock; - use bytes; # return bytes from length() + # patch for 5.005_03 + #use bytes; # return bytes from length() $self->{'stats'}->{$cmdname}++; my $flags = 0; @@ -436,7 +454,9 @@ $self->{'stat_callback'}->($stime, $etime, $sock, $cmdname); } - return $res eq "STORED\r\n"; + # patch for 5.005_03 + #return $res eq "STORED\r\n"; + return defined($res) && $res eq "STORED\r\n"; } sub incr { @@ -512,7 +532,8 @@ } sub _load_multi { - use bytes; # return bytes from length() + # patch for 5.005_03 + #use bytes; # return bytes from length() my Cache::Memcached $self = shift; my ($sock_keys, $ret) = @_; @@ -628,14 +649,19 @@ } # do we have a complete VALUE line? - if ($buf{$sock} =~ /^VALUE (\S+) (\d+) (\d+)\r\n/) { + # patch for 5.005_03 + #if ($buf{$sock} =~ /^VALUE (\S+) (\d+) (\d+)\r\n/) { + if ($buf{$sock} =~ /^VALUE (\S+) (\d+) (\d+)\r\n/g) { ($key{$sock}, $flags{$sock}, $state{$sock}) = (substr($1, $self->{namespace_len}), int($2), $3+2); # Note: we use $+[0] and not pos($buf{$sock}) because pos() # seems to have problems under perl's taint mode. nobody # on the list discovered why, but this seems a reasonable # work-around: - my $p = $+[0]; + # patch for 5.005_03 + #my $p = $+[0]; + my $p = pos($buf{$sock}); + pos($buf{$sock}) = 0; my $len = length($buf{$sock}); my $copy = $len-$p > $state{$sock} ? $state{$sock} : $len-$p; $ret->{$key{$sock}} = substr($buf{$sock}, $p, $copy) @@ -886,6 +912,25 @@ return 1; } +# patch for 5.005_03 +use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK); +sub my_blocking { + my ($sock, $bool) = @_; + + my $old_flags = fcntl($sock, F_GETFL, 0) + or die "Can't get flags for the socket: $!\n"; + + if ($bool) { + my $flags = fcntl($sock, F_SETFL, $old_flags | O_NONBLOCK) + or die "Can't set flags for the socket: $!\n"; + } + else { + my $new_flag = $old_flags & ~O_NONBLOCK; + my $flags = fcntl($sock, F_SETFL, $new_flag) + or die "Can't set flags for the socket: $!\n"; + } + return $old_flags & O_NONBLOCK; +} 1;