我试图按照说明使用Redis :: Client :: Hash,但不断获取 “无法在./redishasttest.pl第8行通过软件包”Redis :: Client :: Hash“找到对象方法”TIEHASH“。” 这是代码:
#!/usr/bin/perl -w
use strict;
use Redis::Client;
my $client = Redis::Client->new;
tie( my %hash, "Redis::Client::Hash", key => 'hello', client => $client);
my @keys = keys %hash;
$hash{foo} = 42;
print 1 if exists $hash{foo};
看起来很简单 - Perl版本5.10.1,Redis 2.6.14。我认为这是一个Moose的东西,因为该模块有一个TIEHASH子。 Redis :: Client :: Hash实际上是在安装Redis :: Client时安装的,所以一切看起来都不错。 Redis :: Client :: String也发生同样的事情,所以不能TIESCALAR。我错过了什么吗?
在friedo的回答之后,检查是否在redis中设置散列键的解决方案是:
#!/usr/bin/perl -w
use strict;
use Redis::Client;
use Redis::Client::Hash;
my $key = 'hello';
my $client = Redis::Client->new;
# first make sure hash with key exists
if ($client->type($key) ne "hash") {
print "$key not a hash\n";
$client->hmset($key, dummy => 1);
}
tie( my %hash, "Redis::Client::Hash", key => $key, client => $client);
print "KEY VALUE\n" if %hash > 0;
foreach my $k (keys %hash) {
print "$k $hash{$k}\n";
}
再次感谢这组精美的模块!
答案 0 :(得分:2)
Redis::Client
无法直接加载领带模块,因此您必须先use
。
use strict;
use Redis::Client;
use Redis::Client::Hash; # <---- add this
my $client = Redis::Client->new;
# first create something
$client->hset( 'hello', some => 'thing' );
tie( my %hash, "Redis::Client::Hash", key => 'hello', client => $client);
my @keys = keys %hash;
$hash{foo} = 42;
print 1 if exists $hash{foo};
看起来我需要在文档中澄清这一点。我本周末可能会发布新版本。