Listing 5 The getIconLocationsByCoords() subroutine
sub getIconLocationsByCoords {
   # Get the bounding box top-left and bottom-right coordinates.
   #
   my ( $x_left, $y_top, $x_right, $y_bottom ) = @_;

   # $Dbh is a DBI database handle; it is created elsewhere.
   #
   my $sth  = $Dbh->prepare (
     qq{ SELECT X, Y, Name, Code
         FROM   Map
         WHERE X BETWEEN ? AND ? 
           AND Y BETWEEN ? AND ?
           AND EXISTS ( SELECT 'x'
                         FROM   ProjectsList
                         WHERE  Active=(1)
                           AND  Map.Code = ProjectsList.MapCode
                       )
       } ) or die "Unable to prepare SQL Statement";

   # We actually adjust the cropped coordinates by the icon
   # "dimension" so that we avoid presenting "half" icons
   # on the screen.
   #
   $sth->execute( $x_left + ICON_DIM,  $x_right  - ICON_DIM,
                  $y_top  + ICON_DIM,  $y_bottom - ICON_DIM )
     or die "Unable to execute statement handle";

   # @locations is used to store the location information returned
   # from the database.
   #
   my @locations = ();

   while ( my $ref = $sth->fetchrow_hashref ) {
       push @locations, $ref;
   }

   $sth->finish();

   return @locations;
}

