Author image Grzegorz Rożniecki
and 1 contributors


Search::Binary - generic binary search (DEPRECATED)


  use Search::Binary;
  my $pos = binary_search($min, $max, $val, $read, $handle, [$size]);


Instead of using Search:Binary, for most cases List::BinarySearch offers same functionality with simpler, more robust API and thus the latter should be preferred and this module should be considered deprecated.

binary_search subroutine (which is exported by default) implements a generic binary search algorithm returning the position of the first record which index value is greater than or equal to $val. The search routine does not define any of the terms position, record or index value, but leaves their interpretation and implementation to the user supplied function &$read(). The only restriction is that positions must be integer scalars.

During the search the read function will be called with three arguments: the input parameters $handle and $val, and a position. If the position is not undef, the read function should read the first whole record starting at or after the position; otherwise, the read function should read the record immediately following the last record it read. The search algorithm will guarantee that the first call to the read function will not be with a position of undef. The read function needs to return a two element array consisting of the result of comparing $val with the index value of the read record and the position of the read record. The comparison value must be positive if $val is strictly greater than the index value of the read record, 0 if equal, and negative if strictly less. Furthermore, the returned position value must be greater than or equal to the position the read function was called with.

The input parameters $min and $max are positions and represents the extent of the search. Only records which begin at positions within this range (inclusive) will be searched. Moreover, $min must be the starting position of a record. If present $size is a difference between positions and determines when the algorithms switches to a sequential search. $val is an index value. The value of $handle is of no consequence to the binary search algorithm; it is merely passed as a convenience to the read function.


For simple case of binary search in array of numbers, one can use Search::Binary with following closure accepting array reference and returning reader function:

  sub make_numeric_array_reader {
      my ( $array ) = @_;
      my $current_pos = 0;
      return sub {
          my ( $self, $value, $pos ) = @_;
          $pos = $current_pos + 1 unless defined $pos;
          $current_pos = $pos;
          return ( $pos < scalar @{$array}
                   ? $value <=> $array->[$pos]
                   : -1, # see RT #52326
                   $pos );
  # search $value position in non-empty @array of numbers
  binary_search 0, @array - 1, $value, make_numeric_array_reader(\@array);

Using List::BinarySearch, equivaluent of above code would be:

  binsearch_pos { $a <=> $b } $value, @array;

so unless one wants to use more generic algorithm, List::BinarySearch functions should be preferred. There's also List::BinarySearch::XS which is faster alternative to pure Perl solutions, if C compiler is available.


Prior to version 0.98, binary_search returned array of three elements in list context, but it was undocumented and in newer versions this behavior was removed.



Copyright 1998, Erik Rantapaa

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.