| 1 | # -*- mode: perl; -*- | ||
| 2 | |||
| 3 | package Math::BigRat::Trace; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use Exporter; | ||
| 9 | use Math::BigRat; | ||
| 10 | |||
| 11 | our @ISA = qw(Exporter Math::BigRat); | ||
| 12 | |||
| 13 | our $VERSION = '0.67'; | ||
| 14 | |||
| 15 | use overload; # inherit overload from Math::BigRat | ||
| 16 | |||
| 17 | # Globals | ||
| 18 | our $accuracy = undef; | ||
| 19 | our $precision = undef; | ||
| 20 | our $round_mode = 'even'; | ||
| 21 | our $div_scale = 40; | ||
| 22 | |||
| 23 | sub new { | ||
| 24 | my $proto = shift; | ||
| 25 | my $class = ref($proto) || $proto; | ||
| 26 | |||
| 27 | my $value = shift; | ||
| 28 | |||
| 29 | my $a = $accuracy; | ||
| 30 | $a = $_[0] if defined $_[0]; | ||
| 31 | |||
| 32 | my $p = $precision; | ||
| 33 | $p = $_[1] if defined $_[1]; | ||
| 34 | |||
| 35 | my $self = $class -> SUPER::new($value, $a, $p, $round_mode); | ||
| 36 | |||
| 37 | printf "Math::BigRat new '%s' => '%s' (%s)\n", | ||
| 38 | $value, $self, ref($self); | ||
| 39 | |||
| 40 | return $self; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub import { | ||
| 44 | my $class = shift; | ||
| 45 | |||
| 46 | printf "%s -> import(%s)\n", $class, join(", ", @_); | ||
| 47 | |||
| 48 | # we catch the constants, the rest goes to parent | ||
| 49 | |||
| 50 | my $constant = grep { $_ eq ':constant' } @_; | ||
| 51 | my @a = grep { $_ ne ':constant' } @_; | ||
| 52 | |||
| 53 | if ($constant) { | ||
| 54 | overload::constant | ||
| 55 | |||
| 56 | integer => sub { | ||
| 57 | $class -> new(shift); | ||
| 58 | }, | ||
| 59 | |||
| 60 | float => sub { | ||
| 61 | $class -> new(shift); | ||
| 62 | }, | ||
| 63 | |||
| 64 | binary => sub { | ||
| 65 | # E.g., a literal 0377 shall result in an object whose value | ||
| 66 | # is decimal 255, but new("0377") returns decimal 377. | ||
| 67 | return $class -> from_oct($_[0]) if $_[0] =~ /^0_*[0-7]/; | ||
| 68 | $class -> new(shift); | ||
| 69 | }; | ||
| 70 | } | ||
| 71 | |||
| 72 | $class -> SUPER::import(@a); # need it for subclasses | ||
| 73 | #$self -> export_to_level(1, $class, @_); # need this ? | ||
| 74 | } | ||
| 75 | |||
| 76 | 1; |