#!/usr/bin/perl -w

#############################################################################
# Misc - miscellanous helper routines
#
# Copyright (C) 1999-2000 by Tels. All rights reserved.
#############################################################################

package CoW::Misc;
use vars qw($VERSION);
$VERSION = 1.00;    # Current version of this package
require  5.005;     # requires this Perl version or later

use base Exporter;
@EXPORT_OK = qw( range min max);
use strict;
use vars qw($AUTOLOAD);

#############################################################################
# public

sub range
  {
  # make sure given arguments are in the given range
  my $min = shift || 0;
  my $max = shift || 0;

  my @a = @_;
  foreach my $v (@a)
    {
    if (ref($v) eq 'SCALAR')
      {
      $$v = $min if ($$v < $min); 
      $$v = $max if ($$v > $max); 
      }
    else
      {
      $v = $min if ($v < $min); 
      $v = $max if ($v > $max); 
      }
    }
  return wantarray ? @a : $a[0];
  }

sub max
  {
  # return maximum element
  my $max = -1000000;
  my $m;
  foreach $m (@_) { $max = $m if ($m > $max); }
  return $max;
  }

sub min
  {
  # return minimum element
  my $min = 1000000;
  my $m;
  foreach $m (@_) { $min = $m if ($m < $min); }
  return $min;
  }

1; 

__END__

#############################################################################

=pod

=head1 NAME

Misc -- a package that gives you various misc routines

=head1 SYNOPSIS

	use CoW::Misc qw/max min range/;

	print max (1,2,3,4,5);			# print 5
	@values = range (0,5,-1,0,1,2,3,4,5,6);	# return 0,0,1,2,3,4,5,5
	print scalar range (0,255,300);		# print 255

	$a = range (0,255,$light);		# make sure a is 0..255
	range(0,255,\$a);			# the same

=head1 REQUIRES

perl5.005, Exporter

=head1 EXPORTS

Exports nothing on default.

=head1 DESCRIPTION

Various misc helper routines.

=head1 METHODS

=head2 range

Given a minimum and maximum value, makes sure that each of the given
additional parameters is between min and max. If the given parameter is a
reference to an scalar, the actual value referenced will be modified in place. 

Returns in scalar context the first value, in list context the entire list.

=head2 min

Return the smallest value from the given list.

=head2 max

Return the greatest value from the given list.

=head1 BUGS

None known yet.

=head1 AUTHOR

Tels http://bloodgate.com 1999-2000.

=cut

