8 our @ISA = qw(Exporter);
9 our @EXPORT = qw(look);
13 Search::Dict, look - search for key in dictionary file
18 look *FILEHANDLE, $key, $dict, $fold;
21 look *FILEHANDLE, $params;
25 Sets file position in FILEHANDLE to be first line greater than or equal
26 (stringwise) to I<$key>. Returns the new file position, or -1 if an error
29 The flags specify dictionary order and case folding:
31 If I<$dict> is true, search by dictionary order (ignore anything but word
32 characters and whitespace). The default is honour all characters.
34 If I<$fold> is true, ignore case. The default is to honour case.
36 If there are only three arguments and the third argument is a hash
37 reference, the keys of that hash can have values C<dict>, C<fold>, and
38 C<comp> or C<xfrm> (see below), and their correponding values will be
39 used as the parameters.
41 If a comparison subroutine (comp) is defined, it must return less than zero,
42 zero, or greater than zero, if the first comparand is less than,
43 equal, or greater than the second comparand.
45 If a transformation subroutine (xfrm) is defined, its value is used to
46 transform the lines read from the filehandle before their comparison.
51 my($fh,$key,$dict,$fold) = @_;
53 if (@_ == 3 && ref $dict eq 'HASH') {
56 $dict = $params->{dict} if exists $params->{dict};
57 $fold = $params->{fold} if exists $params->{fold};
58 $comp = $params->{comp} if exists $params->{comp};
59 $xfrm = $params->{xfrm} if exists $params->{xfrm};
61 $comp = sub { $_[0] cmp $_[1] } unless defined $comp;
65 my($size, $blksize) = @stat[7,11];
67 $key =~ s/[^\w\s]//g if $dict;
68 $key = lc $key if $fold;
69 # find the right block
70 my($min, $max) = (0, int($size / $blksize));
72 while ($max - $min > 1) {
73 $mid = int(($max + $min) / 2);
74 seek($fh, $mid * $blksize, 0)
76 <$fh> if $mid; # probably a partial line
78 $_ = $xfrm->($_) if defined $xfrm;
80 s/[^\w\s]//g if $dict;
82 if (defined($_) && $comp->($_, $key) < 0) {
98 $_ = $xfrm->($_) if defined $xfrm;
100 s/[^\w\s]//g if $dict;
102 last if $comp->($_, $key) >= 0;