2
votes

I am trying to use Redis::Client::Hash per the instructions, but keep getting "Can't locate object method "TIEHASH" via package "Redis::Client::Hash" at ./redishasttest.pl line 8." Here's the code:

#!/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};

Seems straightforward enough -- Perl version 5.10.1, Redis 2.6.14. I am thinking it's a Moose thing or something, as the module has a TIEHASH sub. Redis::Client::Hash is actually installed when you install Redis::Client, so everything there looks good. The same sort of thing happens with Redis::Client::String so can't TIESCALAR. Am I missing something?

After friedo's answer, the solution to check that a hash key is set in redis is:

#!/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";
}

Thanks again for the nice group of modules!

1

1 Answers

2
votes

Redis::Client doesn't load the tie modules directly, so you just have to use them first.

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};

It looks like I need to clarify that in the docs. I can probably do a new release this weekend.