around BUILDARGS => sub { my $orig = shift; my $class = shift; my $self; if (@_ == 2) { return $class->$orig({ @_ }) unless ($_[0] =~ $isint and $_[1] =~ $isint); $self->{num} = $_[0]; unless ($_[1]) { croak "Can't make a $class with demominator 0"; } $self->{den} = $_[1]; } elsif (@_ == 1) { if (ref $_[0]) { # Number::Fraction->new($x) y $x hashref. Lo copiamos if (reftype($_[0]) eq 'HASH') { %$self = %{$_[0]} } elsif (reftype($_[0]) eq 'ARRAY') { %$self = @{$_[0]} } else { carp "Can't make a $class from a ", ref $_[0]; } } else { # Es una cadena 'num/num' o 'num' carp "(@_) is not a fraction" unless $_[0] =~ $isfrac; $self->{num} = $1; $self->{den} = $3 || 1; } } # @_ == 1 elsif (!@_) { # no arguments $self->{num} = 0; $self->{den} = 1; } else { $self = { @_ }; } return $class->$orig($self); }; sub BUILD { my $self = shift; $self->normalise; } }
Casiano Rodríguez León