You can try accessing the dictionary file directly using the
Search::Dict core module, assuming your dictionary is sorted. It performs a binary search through the file. Here, I've wrapped its functionality into an OO-module for convenience:
use Data::Dumper;
use Search::Dict::Object;
my $d = Search::Dict::Object->new(
file => "/tmp/dict.txt",
keyval_xfrm => sub { split /\t/ },
comp => sub { $_[0] cmp $_[1] }, # should correspond to file sort
+order
);
print Dumper {
aaa => $d->get('aaa'),
foo => $d->get('foo'),
bar => $d->get('bar'),
baz => $d->get('baz'),
zzz => $d->get('zzz'),
};
__END__
$VAR1 = {
'bar' => '789',
'baz' => '456',
'aaa' => undef,
'foo' => '123',
'zzz' => undef
};
The dictionary file:
$ cat /tmp/dict.txt
aho 234
bar 789
bat 567
baz 456
cut 678
foo 123
yyy 000
The
Search::Dict::Object package:
package Search::Dict::Object;
use warnings;
use strict;
use Search::Dict ();
sub new {
my $class = shift;
my $self = bless { }, $class;
$self->_init(@_);
return $self;
}
sub _init {
my $self = shift;
%{ $self } = @_;
unless (defined($self->{FH}))
{
my $file = $self->{file} || "<unspecified file>";
open($self->{FH}, "<", $file)
or die("Cannot open: $file", "\n ", $!);
}
}
sub get {
my ($self, $key) = @_;
return undef unless defined($key);
my $FH = $self->{FH};
my $comp = $self->{comp} || sub { $_[0] cmp $_[1] };
my $keyval_xfrm = $self->{keyval_xfrm} || sub { $_ => $_ };
my $opts = {
comp => $comp,
$self->{keyval_xfrm}
? (xfrm => sub { chomp($_[0]); (map $keyval_xfrm->(), $_[0])
+[0] })
: (),
};
if (Search::Dict::look($FH, $key, $opts) != -1)
{
my $entry = <$FH>;
return undef unless defined($entry);
chomp($entry);
my ($k, $v) = map $keyval_xfrm->(), $entry;
return $v if $comp->($k, $key) == 0;
}
return undef;
}
sub DESTROY {
my $self = shift;
close($self->{FH}) if $self->{FH};
}
1;