Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Perl Integer Division Module
package Math::IntegerDivision;
use Carp;
use Moose;
has 'numerator' => (
is => 'rw',
isa => 'Int'
);
has 'denominator' => (
is => 'rw',
isa => 'Int'
);
sub divide {
my ( $self ) = @_;
unless( $self->denominator ) {
carp("Division by Zero");
return;
}
my $answer = ( $self->numerator - $self->numerator % $self->denominator ) / $self->denominator;
# When only one is negative, the result is out by one
# when it's not a perfect divisor.
return (( $self->numerator < 0 ^ $self->denominator < 0 ) && ( $self->numerator % $self->denominator )) ?
$answer + 1 :
$answer;
}
1;
@nicdoye
Copy link
Author

nicdoye commented Jul 2, 2017

The check_int is removed in revision 3 as it is unnecessary, because of the Moose type constraints.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment