

use DB_File;
tie %password, 'DB_File', "./pw_aux", O_CREAT|O_RDWR, 0666
  or die $!;
open PASSWD, "<", "/etc/passwd" or die $!;

make_index(\*PASSWD, \%password, 
           sub { (split /:/, $_[0], 2)[0] },
          );

print find(\*PASSWD, \%password, "mjd");
exit 0;

use Fcntl ':seek';

sub make_index {
  my ($fh, $dbm, $key_function) = @_;
  seek $fh, 0, SEEK_SET;
  %$dbm = (); 
  my $pos = 0;
  while (<$fh>) {
    chomp;
    my $key = $key_function->($_);
    $dbm->{$key} = $pos;
    $pos = tell $fh;
  }
}

sub find {
  my ($fh, $dbm, $key) = @_;
  my $offset = $dbm->{$key};
  seek $fh, $offset, SEEK_SET;
  my $rec = <$fh>;
  return $rec;
}

