This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
In Perl_sv_gets(), shortbuffered is always 0 when rslen is 0.
[perl5.git] / lib / look.pl
1 warn "Legacy library @{[(caller(0))[6]]} will be removed from the Perl core distribution in the next major release. Please install it from the CPAN distribution Perl4::CoreLibs. It is being used at @{[(caller)[1]]}, line @{[(caller)[2]]}.\n";
2
3 ;# Usage: &look(*FILEHANDLE,$key,$dict,$fold)
4 #
5 # This library is no longer being maintained, and is included for backward
6 # compatibility with Perl 4 programs which may require it.
7 # This legacy library is deprecated and will be removed in a future
8 # release of perl.
9 #
10 # In particular, this should not be used as an example of modern Perl
11 # programming techniques.
12
13 ;# Sets file position in FILEHANDLE to be first line greater than or equal
14 ;# (stringwise) to $key.  Pass flags for dictionary order and case folding.
15
16 sub look {
17     local(*FH,$key,$dict,$fold) = @_;
18     local($max,$min,$mid,$_);
19     local($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
20        $blksize,$blocks) = stat(FH);
21     $blksize = 8192 unless $blksize;
22     $key =~ s/[^\w\s]//g if $dict;
23     $key = lc $key if $fold;
24     $max = int($size / $blksize);
25     while ($max - $min > 1) {
26         $mid = int(($max + $min) / 2);
27         seek(FH,$mid * $blksize,0);
28         $_ = <FH> if $mid;              # probably a partial line
29         $_ = <FH>;
30         chop;
31         s/[^\w\s]//g if $dict;
32         $_ = lc $_ if $fold;
33         if ($_ lt $key) {
34             $min = $mid;
35         }
36         else {
37             $max = $mid;
38         }
39     }
40     $min *= $blksize;
41     seek(FH,$min,0);
42     <FH> if $min;
43     while (<FH>) {
44         chop;
45         s/[^\w\s]//g if $dict;
46         $_ = lc $_ if $fold;
47         last if $_ ge $key;
48         $min = tell(FH);
49     }
50     seek(FH,$min,0);
51     $min;
52 }
53
54 1;