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, $comp;
22 Sets file position in FILEHANDLE to be first line greater than or equal
23 (stringwise) to I<$key>. Returns the new file position, or -1 if an error
26 The flags specify dictionary order and case folding:
28 If I<$dict> is true, search by dictionary order (ignore anything but word
29 characters and whitespace). The default is honour all characters.
31 If I<$fold> is true, ignore case. The default is to honour case.
33 If I<$comp> is defined, use that as a reference to the comparison subroutine,
34 which must return less than zero, zero, or greater than zero, if the
35 first comparand is less than, equal, or greater than the second comparand.
37 If there are only three arguments and the third argument is a hash
38 reference, the keys of that hash can have values C<dict>, C<fold>, and
39 C<comp>, and their correponding values will be used as the parameters.
44 my($fh,$key,$dict,$fold,$comp) = @_;
45 if (@_ == 3 && ref $dict eq 'HASH') {
48 $dict = $opt->{dict} if exists $opt->{dict};
49 $fold = $opt->{fold} if exists $opt->{fold};
50 $comp = $opt->{comp} if exists $opt->{comp};
52 $comp = sub { $_[0] cmp $_[1] } unless defined $comp;
56 my($size, $blksize) = @stat[7,11];
58 $key =~ s/[^\w\s]//g if $dict;
59 $key = lc $key if $fold;
60 # find the right block
61 my($min, $max) = (0, int($size / $blksize));
63 while ($max - $min > 1) {
64 $mid = int(($max + $min) / 2);
65 seek($fh, $mid * $blksize, 0)
67 <$fh> if $mid; # probably a partial line
70 s/[^\w\s]//g if $dict;
72 if (defined($_) && $comp->($_, $key) < 0) {
89 s/[^\w\s]//g if $dict;
91 last if $comp->($_, $key) >= 0;