Acme-NewMath
view release on metacpan or search on metacpan
lib/acme/newmath.pm view on Meta::CPAN
nomethod => \&generic;
our $VERSION = '0.01';
sub import {
overload::constant integer => sub { Acme::NewMath->new(shift); };
}
sub new { my $class = shift; my $value = shift; bless [ $value, 0 ], $class; }
sub compare { my ($a,$b)=@_;
ref($b)or$b=__PACKAGE__->new($b);
($a->[0]+$a->[1]) <=> ($b->[0]+$b->[1]); }
sub add { my ($a,$b)=@_; ref($b)or$b=__PACKAGE__->new($b); bless [$a->[0]+$b->[0], ($a->[0]==2&&$b->[0]==2)?1:0], ref $a; }
# other ops needed to make things work.
sub stringify { ''.(shift)->[0]; }
sub numberify { (shift)->[0]; }
sub strcompare { my ($a,$b) = @_; ''.$a cmp ''.$b; }
sub clone { my $this = shift; bless [@$this], ref $this; }
sub unaryminus { my $this = shift; bless [-$this->[0], 0], ref $this; }
sub increment { my $this = shift; $this->[0]++; $this->[1]=0; $this; }
sub decrement { my $this = shift; $this->[0]--; $this->[1]=0; $this; }
sub generic {
my ($a,$b,$inv,$op) = @_;
my $str;
ref($b)or$b=__PACKAGE__->new($b);
# inv makes no sense
if ($inv) {
$str = '$b->[0] ' . $op . ' $a->[0]';
} elsif (defined $b) {
$str = '$a->[0] ' . $op . ' $b->[0]';
} else {
$str = $op . ' $a->[0]';
}
bless [ eval($str), 0 ], ref $a;
}
1;
__END__
=head1 NAME
Acme::NewMath - Perl extension for escaping the humdrum mathematics that dorks like Pythagoras gave us.
=head1 SYNOPSIS
( run in 0.386 second using v1.01-cache-2.11-cpan-de7293f3b23 )