use overload q("") => 'to_string', '0+' => 'to_num', '+' => 'add', '*' => 'mult', '-' => 'subtract', '/' => 'div', '<=>' => 'compare', 'cmp' => 'compare', fallback => 1; =head2 compare Compare a fraction object with a value. Return -1, 0, 1 depending on if object is less than, equal to, or greater than the value. The first parameter is a fraction object. The second parameter is either another fraction object or a number (eg 0.25) or a string (eg '1/4'). =cut sub compare { my ($l, $r, $rev) = @_; my $x = $l->to_num; my $y; if( UNIVERSAL::isa($r, ref $l) ){ $y = $r->to_num; }elsif( my $r2 = (ref $l)->new($r) ){ $y = $r2->to_num; }else{ $y = $r; } return 0 if $x == $y; return ($rev?-1:1) * ($x < $y ? -1 : 1); }