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