Class::Comparable

Class::Comparable is a base class for comparable objects.
Download

Class::Comparable Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Stevan Little
  • Publisher web site:
  • http://search.cpan.org/~stevan/

Class::Comparable Tags


Class::Comparable Description

Class::Comparable is a base class for comparable objects. SYNOPSIS # an example subclass package Currency::USD; use base 'Class::Comparable'; sub new { my $class = shift; bless { value => shift }, $class; } sub value { (shift)->{value} } sub compare { my ($left, $right) = @_; # if we are comparing against another # currency object, then compare values if (ref($right) && $right->isa('Currency::USD')) { return $left->value $right->value; } # otherwise assume we are comparing # against a numeric value of some kind else { return $left->value $right; } } # an example usage of Class::Comparable object my $buck_fifty = Currency::USD->new(1.50); my $dollar_n_half = Currenty::USD->new(1.50); ($buck_fifty == $dollar_n_half) # these are equal (1.75 > $buck_fifty) # 1.75 is more than a buck fifty my $two_bits = Currency::USD->new(0.25); ($two_bits < $dollar_n_half) # 2 bits is less than a dollar and a half ($two_bits == 0.25) # two bits is equal to 25 cents Requirements: · Perl


Class::Comparable Related Software